带中断模式的 Python pyserial [英] Python's pyserial with interrupt mode

查看:73
本文介绍了带中断模式的 Python pyserial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于串行通信的设备.我正在编写 python 代码,它将发送一些命令以从设备获取数据.

I have a device which works on serial communication. I am writing python code which will send some commands to get the data from the device.

共有三个命令.

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 数据.下面是代码,我正在使用:

Now the problem which I am facing is that, frequency of data received from sop command is not fixed and hence the data is received anytime. This command can also not be stopped once started, so if I run another command like time, and read the data, I do not receive time values and they are merged with the sop data sometime. Below is the code, I am using:

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 命令后收到它.

Using the above command I sometimes do not receive the value of time after executing its command or sometimes it is merged with the sop command. Also with the sop command, I received a lot of data during the sleep(5) out of which I need to get the latest data. If I do not include sleep(5), I miss the sop data and it is then received after executing the time command.

我希望有人能指出我如何以更好的方式设计它的正确方向.另外,我认为这可以使用中断处理程序轻松完成,但我没有找到任何关于 pyserial 中断的代码.任何人都可以建议一些在pyserial中使用中断的好代码.

I was hoping if anyone can point me to right direction of how to design it in a better way. Also, I think this can easily be done using interrupt handler but I didn't found any code about pyserial interrupts. Can anyone please suggest some good code for using interrupts in pyserial.

谢谢

推荐答案

我在这里试探一下:你的 time.sleep(5) 可能太长了.你有没有试过让睡眠真的很短,例如 time.sleep(.300)?如果时间数据在 sop 返回之间被写回,您将在它与 sop 合并之前捕获它,但我在这里假设它将发送时间数据,否则在服务器端您无能为力(蟒蛇)代码.我确实相信减少睡眠不会有什么坏处,反正只是坐在那里等待(轮询)交流.

I am taking a stab here: Your time.sleep(5) might be too long. Have you tried making the sleep really short, for example time.sleep(.300)? If the time data gets written back between the sop returns you will catch it before it gets merged with sop, but I am making an assumption here that it will send time data back, else there is anyway nothing more you can do on the server side (the python) code. I do believe it won't hurt to make the sleep less, it is anyway just sitting there waiting (polling) for communication.

由于我身边没有相同的环境,所以很难回答,因为我无法测试我的答案,所以我希望这可能会有所帮助.

Not having the having the same environment on my side, makes it difficult to answer, because I can't test my answer, so I hope this might help.

这篇关于带中断模式的 Python pyserial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆