遍历所有串行地址以定位硬件
Loop through all serial addresses to locate hardware
我是 python 的新手,我正在尝试遍历所有 255 个地址以通过 RS485 定位硬件的特定位。硬件是附加的,它的地址应该是0x25,但事实并非如此,所以我需要找到它的地址是什么。
到目前为止我有:
def Init_Sync_4 ():
GPIO.output(18,GPIO.HIGH)
ser.write([CID,0x17,0x20,0x01,0x14,0x1c,0x04,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x01,0x00,0x00,0x00,0x32,0x00,0x02,0x00,0x00,0$
time.sleep(0.007)
GPIO.output(18,GPIO.LOW)
和
ser=serial.Serial(port='/dev/serial0',baudrate=38400,stopbits=2,timeout=1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)
for i in range(0xff):
CID = '0x{:02x}'.format(i).encode('ascii')
print "at address %s" % CID
Init_Sync_4()
time.sleep(0.05)
Init_2()
time.sleep(0.05)
这里 CID
是正在构建的地址,Init_Sync_4()
在 ser.write
中使用它的字节数组,但我不断收到错误:
at address 0x00
Traceback (most recent call last):
File "rs485_test_2.py", line 97, in <module>
Init_4()
File "rs485_test_2.py", line 40, in Init_4
ser.write([CID,0x17,0x20,0x01,0x14,0x1c,0x04,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x01,0x00,0x00,0x00,0x32,0x00,0x02,0x00,0x00,0x00,0x00])
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 66, in to_bytes
return bytes(bytearray(seq))
ValueError: string must be of size 1
我假设它仍然作为字符串传递,当它需要是一个字节时,但我不知道转换是如何工作的。我检查了一些 SO 页面,这些页面指定使用 .encode
有或没有参数,但我要么使用不正确,要么不是我想要的。任何帮助是极大的赞赏!谢谢。
ser.write([CID,0x17,0x20,0x01, ...
CID
进入一个包含一堆整数的列表。但是它本身是一个字符串。
您想将 CID 保留为整数。
# CID = '0x{:02x}'.format(i).encode('ascii')
CID = i
我是 python 的新手,我正在尝试遍历所有 255 个地址以通过 RS485 定位硬件的特定位。硬件是附加的,它的地址应该是0x25,但事实并非如此,所以我需要找到它的地址是什么。
到目前为止我有:
def Init_Sync_4 ():
GPIO.output(18,GPIO.HIGH)
ser.write([CID,0x17,0x20,0x01,0x14,0x1c,0x04,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x01,0x00,0x00,0x00,0x32,0x00,0x02,0x00,0x00,0$
time.sleep(0.007)
GPIO.output(18,GPIO.LOW)
和
ser=serial.Serial(port='/dev/serial0',baudrate=38400,stopbits=2,timeout=1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)
for i in range(0xff):
CID = '0x{:02x}'.format(i).encode('ascii')
print "at address %s" % CID
Init_Sync_4()
time.sleep(0.05)
Init_2()
time.sleep(0.05)
这里 CID
是正在构建的地址,Init_Sync_4()
在 ser.write
中使用它的字节数组,但我不断收到错误:
at address 0x00
Traceback (most recent call last):
File "rs485_test_2.py", line 97, in <module>
Init_4()
File "rs485_test_2.py", line 40, in Init_4
ser.write([CID,0x17,0x20,0x01,0x14,0x1c,0x04,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x01,0x00,0x00,0x00,0x32,0x00,0x02,0x00,0x00,0x00,0x00])
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 66, in to_bytes
return bytes(bytearray(seq))
ValueError: string must be of size 1
我假设它仍然作为字符串传递,当它需要是一个字节时,但我不知道转换是如何工作的。我检查了一些 SO 页面,这些页面指定使用 .encode
有或没有参数,但我要么使用不正确,要么不是我想要的。任何帮助是极大的赞赏!谢谢。
ser.write([CID,0x17,0x20,0x01, ...
CID
进入一个包含一堆整数的列表。但是它本身是一个字符串。
您想将 CID 保留为整数。
# CID = '0x{:02x}'.format(i).encode('ascii')
CID = i