Python 中使用 ASCII 命令进行串行通信
大家好,我正在尝试编写 ASCII 命令和作为回报读取串行缓冲区&获取串行数据。我在解析串行数据以仅读取我想要查看和使用的串行数据时遇到问题。
代码:
import serial
# Serial Connection
ser = serial.Serial(
port="COM2", baudrate = 38400,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, timeout=1)
# Write ASCII Commands To TSI 4043 Flow Sensor
ser.write(b'?\r\n') # Ask Sensor if it is getting a signal (Returns "OK")
ser.write(b'SUS\r\n') # Set Flow type to SLPM (Returns "OK")
ser.write(b'SG0\r\n') # Set Flow Gas to Air (Returns "OK")
ser.write(b'SSR0010\r\n') # Set Sample Rate to 10ms (Returns "OK")
ser.write(b'DAFxx0010\r\n') # Read 10 sensor Flow Reading
# Read ASCII From Serial Buffer
output = ser.read(100) # Read 100 bytes from serial buffer
print(output)
输出:
b'OK\r\nOK\r\nOK\r\nOK\r\nOK\r\n80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
我想要发生的事情:
OK
OK
OK
OK
80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28
OK 仅用于调试目的。我真正需要的是将所有流量读数放入一个数组中,这样我就可以对其进行平均,这样我就可以实时观察变化并控制电机。
Hello guys I am attempting to write ASCII commands & in return read the serial buffer & get serial data. I am having an issue parsing the serial data out to only read the serial data I want to see and use.
The Code:
import serial
# Serial Connection
ser = serial.Serial(
port="COM2", baudrate = 38400,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, timeout=1)
# Write ASCII Commands To TSI 4043 Flow Sensor
ser.write(b'?\r\n') # Ask Sensor if it is getting a signal (Returns "OK")
ser.write(b'SUS\r\n') # Set Flow type to SLPM (Returns "OK")
ser.write(b'SG0\r\n') # Set Flow Gas to Air (Returns "OK")
ser.write(b'SSR0010\r\n') # Set Sample Rate to 10ms (Returns "OK")
ser.write(b'DAFxx0010\r\n') # Read 10 sensor Flow Reading
# Read ASCII From Serial Buffer
output = ser.read(100) # Read 100 bytes from serial buffer
print(output)
OUTPUT:
b'OK\r\nOK\r\nOK\r\nOK\r\nOK\r\n80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
What I want to happen:
OK
OK
OK
OK
80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28
The OKs are just for debugging purposes. What I really need is to put all of the flow readings into an array so I can average it so I can watch the change in real time and control a motor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该稍微改变一下你的方法。
每次
ser.write
后从串行端口读取并解析/处理其输出
。检查您是否确实收到来自设备的
b'OK\r\n'
响应,并确保您遵守设备通信协议(您发送的每个命令的可能输出是什么)。您永远不知道设备在处理命令时何时抛出错误。像这样,您已经以 WRITE
command
=> 的方式进行通信。 阅读输出
您发送的每个命令。接下来是流量读数部分。您的
输出
中应该只有b'80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
。您可以处理它并获得平均值:
现在您与设备有了干净的通信,并且您的脚本可以处理可能的通信错误。
You should change Your approach a bit.
Read from serial port after every
ser.write
and parse / process it'soutput
.Check if You actually receive
b'OK\r\n'
response from the device and make sure You comply with Device communication protocol (what are possible outputs for every command You send). You never know when Device throw an error while processing the command.Like this You will already have communication in a way WRITE
command
=> READoutput
for every command You send.Then it comes to the part of flow readout. You should have just
b'80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
in Youroutput
.You can process it and get average with:
Now You have clean communication with Device and Your script can handle possible communication errors.