[英]Serial AT commands not working in python script but working in Minicom
[英]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)
注意,我还更改了每个子命令的开头以指示它是一个十六进制值。
======== ========编辑
查看问题中的命令列表,我想知道您是否已使用“打开”命令来确保系统已打开?
我还看到可以发送一个字符串来查询设备。 你尝试过吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.