Python's pyserial with interrupt mode
Python's pyserial with interrupt mode
我有一个可以在 serial communication
上运行的设备。我正在编写 python 代码,它将发送一些命令以从设备获取数据。
共有三个命令。
1.COMMAND - sop
Device does its internal calculation and sends below data
Response - "b'SOP,0,921,34,40,207,0,x9A\r\n'"
2.COMMAND - time
This gives a date time values which normally do not change untill the device is restarted
3.START - "\r\r" or (<cr><cr>)
This command puts the device in responsive mode after which it responds to above commands. This command is basically entering <enter> twice & only have to do once at the start.
现在我面临的问题是,从sop
命令接收到的数据频率不固定,因此随时都可以接收到数据。此命令一旦启动也无法停止,因此如果我 运行 另一个命令,如 time
,并读取数据,我不会收到时间值,它们会与 sop
数据合并有时。下面是代码,我正在使用:
port = serial.Serial('/dev/ttyS0',115200) #Init serial port
port.write(("\r\r".encode())) #Sending the start command
bytesToRead = port.in_waiting #Checking data bytesize
res = port.read(bytesToRead) #Reading the data which is normally a welcome msg
port.reset_input_buffer() #Clearing the input serial buffer
port.reset_output_buffer() #Clearing the output serial buffer
port.write(("sop\r".encode())) #Sending the command sop
while True:
time.sleep(5)
bytesToRead = port.in_waiting
print(bytesToRead)
res = port.read(bytesToRead)
print(res)
port.reset_input_buffer()
port.write(("time\r".encode()))
res = port.readline()
print(res)
使用上面的命令,我有时在执行它的命令后没有收到时间值,或者有时它与 sop
命令合并。同样使用 sop 命令,我在 sleep(5)
期间收到了大量数据,我需要从中获取最新数据。如果我不包含 sleep(5)
,我会错过 sop
数据,然后在执行 time
命令后接收到。
我希望是否有人能指出正确的方向以更好地设计它。另外,我认为这可以使用中断处理程序轻松完成,但我没有找到任何关于 pyserial 中断的代码。谁能推荐一些在 pyserial 中使用中断的好代码。
谢谢
我要试一试:您的 time.sleep(5) 可能太长了。您是否尝试过让睡眠变得非常短,例如 time.sleep(.300)?如果时间数据在 sop returns 之间被写回,你会在它与 sop 合并之前捕获它,但我在这里假设它会发回时间数据,否则你无能为力在服务器端(python)代码。我相信少睡一点也没什么坏处,反正就是坐在那里等(轮询)沟通。
我身边没有相同的环境,所以很难回答,因为我无法测试我的答案,所以我希望这可能会有所帮助。
与其使用 time.sleep(),不如使用 serialport.in_waiting,这有助于检查 rcv 缓冲区中可用的字节数。
所以一旦有一些数据是 rcv 缓冲区然后只使用读取函数读取数据。
因此可以在没有任何延迟的情况下遵循以下代码序列
while True:
bytesToRead = port.in_waiting
print(bytesToRead)
if(bytestoRead > 0):
res = port.read(bytesToRead)
print(res)
port.reset_input_buffer()
# put some check or filter then write data on serial port
port.write(("time\r".encode()))
res = port.readline()
print(res)
我有一个可以在 serial communication
上运行的设备。我正在编写 python 代码,它将发送一些命令以从设备获取数据。
共有三个命令。
1.COMMAND - sop
Device does its internal calculation and sends below data
Response - "b'SOP,0,921,34,40,207,0,x9A\r\n'"
2.COMMAND - time
This gives a date time values which normally do not change untill the device is restarted
3.START - "\r\r" or (<cr><cr>)
This command puts the device in responsive mode after which it responds to above commands. This command is basically entering <enter> twice & only have to do once at the start.
现在我面临的问题是,从sop
命令接收到的数据频率不固定,因此随时都可以接收到数据。此命令一旦启动也无法停止,因此如果我 运行 另一个命令,如 time
,并读取数据,我不会收到时间值,它们会与 sop
数据合并有时。下面是代码,我正在使用:
port = serial.Serial('/dev/ttyS0',115200) #Init serial port
port.write(("\r\r".encode())) #Sending the start command
bytesToRead = port.in_waiting #Checking data bytesize
res = port.read(bytesToRead) #Reading the data which is normally a welcome msg
port.reset_input_buffer() #Clearing the input serial buffer
port.reset_output_buffer() #Clearing the output serial buffer
port.write(("sop\r".encode())) #Sending the command sop
while True:
time.sleep(5)
bytesToRead = port.in_waiting
print(bytesToRead)
res = port.read(bytesToRead)
print(res)
port.reset_input_buffer()
port.write(("time\r".encode()))
res = port.readline()
print(res)
使用上面的命令,我有时在执行它的命令后没有收到时间值,或者有时它与 sop
命令合并。同样使用 sop 命令,我在 sleep(5)
期间收到了大量数据,我需要从中获取最新数据。如果我不包含 sleep(5)
,我会错过 sop
数据,然后在执行 time
命令后接收到。
我希望是否有人能指出正确的方向以更好地设计它。另外,我认为这可以使用中断处理程序轻松完成,但我没有找到任何关于 pyserial 中断的代码。谁能推荐一些在 pyserial 中使用中断的好代码。
谢谢
我要试一试:您的 time.sleep(5) 可能太长了。您是否尝试过让睡眠变得非常短,例如 time.sleep(.300)?如果时间数据在 sop returns 之间被写回,你会在它与 sop 合并之前捕获它,但我在这里假设它会发回时间数据,否则你无能为力在服务器端(python)代码。我相信少睡一点也没什么坏处,反正就是坐在那里等(轮询)沟通。
我身边没有相同的环境,所以很难回答,因为我无法测试我的答案,所以我希望这可能会有所帮助。
与其使用 time.sleep(),不如使用 serialport.in_waiting,这有助于检查 rcv 缓冲区中可用的字节数。 所以一旦有一些数据是 rcv 缓冲区然后只使用读取函数读取数据。 因此可以在没有任何延迟的情况下遵循以下代码序列
while True:
bytesToRead = port.in_waiting
print(bytesToRead)
if(bytestoRead > 0):
res = port.read(bytesToRead)
print(res)
port.reset_input_buffer()
# put some check or filter then write data on serial port
port.write(("time\r".encode()))
res = port.readline()
print(res)