从PySerial向Arduino发送整数值

tod*_*ish 1 python arduino

我需要发送大于255的整数吗?有谁知道如何做到这一点?

tod*_*ish 5

这是这样的(谢谢你的想法,亚历克斯!):

蟒蛇:

def packIntegerAsULong(value):
    """Packs a python 4 byte unsigned integer to an arduino unsigned long"""
    return struct.pack('I', value)    #should check bounds

# To see what it looks like on python side
val = 15000
print binascii.hexlify(port.packIntegerAsULong(val))

# send and receive via pyserial
ser = serial.Serial(serialport, bps, timeout=1)
ser.write(packIntegerAsULong(val))
line = ser.readLine()
print line
Run Code Online (Sandbox Code Playgroud)

的Arduino:

unsigned long readULongFromBytes() {
  union u_tag {
    byte b[4];
    unsigned long ulval;
  } u;
  u.b[0] = Serial.read();
  u.b[1] = Serial.read();
  u.b[2] = Serial.read();
  u.b[3] = Serial.read();
  return u.ulval;
}
unsigned long val = readULongFromBytes();
Serial.print(val, DEC); // send to python to check
Run Code Online (Sandbox Code Playgroud)