Python - 串行命令无效
Python - not working serial commands
正在尝试向硬件发送命令:打开和关闭激光。但是 Python 失败了,硬件从未收到命令。
命令列表:
ss.py:
import serial
ser = serial.Serial('/dev/tty.usbserial', serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE)
print(ser.name)
# if i add ' then also error
ser.write(\xA0h\xC1h\x01\x00h\xSt.\xAFh)
ser.close()
执行时发生错误:
File "ss.py", line 5
ser.write(\xA0h\xC1h\x01\x00h\xSt.\xAFh)
^
SyntaxError: unexpected character after line continuation character
或
错误:
$ python ss.py
Traceback (most recent call last):
File "ss.py", line 3, in <module>
ser = serial.Serial('/dev/tty.usbserial', serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE)
File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 220, in __init__
self.bytesize = bytesize
File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 306, in bytesize
raise ValueError("Not a valid byte size: {!r}".format(bytesize))
ValueError: Not a valid byte size: 'N'
我怀疑您需要一次发送一个命令,而不是作为一个字符串发送。
for command in [b'0xA0h', b'0xC1h', b'0x01', b'0x00h', b'0xSt', b'0xAFh']:
ser.write(command)
请注意,我还更改了每个子命令的开头,以表明它是一个十六进制值。
========编辑========
看你问题中的命令列表,不知你是否使用过"Switch on"命令来确保系统处于开机状态?
我还看到可以发送一个字符串来查询设备。你试过了吗?
正在尝试向硬件发送命令:打开和关闭激光。但是 Python 失败了,硬件从未收到命令。
命令列表:
ss.py:
import serial
ser = serial.Serial('/dev/tty.usbserial', serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE)
print(ser.name)
# if i add ' then also error
ser.write(\xA0h\xC1h\x01\x00h\xSt.\xAFh)
ser.close()
执行时发生错误:
File "ss.py", line 5
ser.write(\xA0h\xC1h\x01\x00h\xSt.\xAFh)
^
SyntaxError: unexpected character after line continuation character
或
错误:
$ python ss.py
Traceback (most recent call last):
File "ss.py", line 3, in <module>
ser = serial.Serial('/dev/tty.usbserial', serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE)
File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 220, in __init__
self.bytesize = bytesize
File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 306, in bytesize
raise ValueError("Not a valid byte size: {!r}".format(bytesize))
ValueError: Not a valid byte size: 'N'
我怀疑您需要一次发送一个命令,而不是作为一个字符串发送。
for command in [b'0xA0h', b'0xC1h', b'0x01', b'0x00h', b'0xSt', b'0xAFh']:
ser.write(command)
请注意,我还更改了每个子命令的开头,以表明它是一个十六进制值。
========编辑========
看你问题中的命令列表,不知你是否使用过"Switch on"命令来确保系统处于开机状态?
我还看到可以发送一个字符串来查询设备。你试过了吗?