Python - 不工作的串行命令

时间:2017-11-22 08:54:52

标签: python macos window byte pyserial

尝试向硬件发送命令:激光打开和关闭。但是Python失败了,硬件永远不会得到命令。

enter image description here enter image description here enter image description here

命令列表:

enter image description here

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'

1 个答案:

答案 0 :(得分:1)

我怀疑你需要一次发送一个命令,而不是单个字符串。

for command in [b'0xA0h', b'0xC1h', b'0x01', b'0x00h', b'0xSt', b'0xAFh']:
    ser.write(command)

请注意,我还更改了每个子命令的开头,以指示它是十六进制值。

====== EDIT ======

查看问题中的命令列表,我想知道您是否使用过&#34;开启&#34;命令以确保系统已启用?

我还看到可以发送一个字符串来查询设备。你试过了吗?