简体   繁体   English

Python-无法使用串行命令

[英]Python - not working serial commands

Trying to send commands to the hardware: Laser on and off. 尝试向硬件发送命令:打开和关闭激光。 But Python is failing and the hardware never getting the command. 但是Python失败了,硬件也无法获得命令。

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

Command list: 命令清单:

在此处输入图片说明

ss.py: 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()

On execute error occure: 执行时发生错误:

  File "ss.py", line 5
    ser.write(\xA0h\xC1h\x01\x00h\xSt.\xAFh)
                                           ^
SyntaxError: unexpected character after line continuation character

or 要么

Error: 错误:

$ 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'

I suspect that you need to send the commands one at a time, rather than as a single string. 我怀疑您需要一次发送一个命令,而不是一个字符串。

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

Notice that I have also altered the beginning of each subcommand to indicate that it is a hex value. 注意,我还更改了每个子命令的开头以指示它是一个十六进制值。

========EDIT======== ======== ========编辑

Looking at the command list in your question, I wonder if you have used the "Switch on" command to make sure the system is on? 查看问题中的命令列表,我想知道您是否已使用“打开”命令来确保系统已打开?

I also see that it is possible to send a string to query the device. 我还看到可以发送一个字符串来查询设备。 Have you tried that? 你尝试过吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM