读取串行端口 - 在一定时间内忽略写入串行端口的部分数据 [英] Reading a Serial Port - Ignore portion of data written to serial port for certain time

查看:38
本文介绍了读取串行端口 - 在一定时间内忽略写入串行端口的部分数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在串行端口上每隔一段时间读取传入的数据和 Arduino.所以本质上类似于

I would like to read data coming and Arduino on a serial port on intervals. So essentially something like

  1. 读一读
  2. 等一下
  3. 读一读
  4. 等一下
  5. 采取...

等等

我面临的问题是端口将缓冲其信息,因此只要我调用等待函数,串行端口上的数据就会开始缓冲.等待函数完成后,我尝试再次读取数据,但我正在从缓冲区的开头读取数据,并且数据不再是最新的,而是在等待函数开始的大致时间读取.

The problem I am facing is that the port will buffer its information so as soon as I call a wait function the data on the serial port will start buffering. Once the wait function finishes I try and read the data again but I am reading from the beginning of the buffer and the data is not current anymore, but instead is the reading taken at roughly the time the wait function began.

我的问题是,是否有一种我不知道的方法可以忽略在该等待期间读取的部分数据,而只读取串行端口上当前正在传递的数据?

My question is whether there is a way that I am unaware of to ignore the portion of data read in during that wait period and only read what is currently being delivered on the serial port?

到目前为止,我有类似的东西:

I have this something analogous to this so far:

import serial

s = serial.Serial(path_to_my_serial_port,9600)

while True:
    print s.readline()
    time.sleep(.5)

出于解释目的,我让 Arduino 输出自循环开始以来的时间.根据python代码,每次调用的时间应该是半秒.通过串行输出,时间在不到一毫秒的时间内增加.无论睡眠时间如何,这些值都不会改变.

For explanation purposes I have the Arduino outputting the time since it began its loop. By the python code, the time of each call should be a half second apart. By the serial output the time is incrementing in less than a millisecond. These values do not change regardless of the sleep timing.

样本输出:

504

504

504

504

505

505

505

...

作为我最终目标的一个想法,我想测量端口的值,等待一段时间,看看值是多少,再等一下,看看值是多少,再等一次.

As an idea of my end goal, I would like to measure the value of the port, wait a time delay, see what the value is then, wait again, see what the value is then, wait again.

我目前为此使用 Python,但对其他语言持开放态度.

I am currently using Python for this but am open to other languages.

推荐答案

如果您想在执行下一次读取之前清除输入缓冲区中可能存在的内容,您可以使用 s.flushInput() 刷新输入缓冲区(您也可以使用 s.inWaiting() 检查输入缓冲区中的字节).

If you want to get rid of what may be in the input buffer before you perform your next read, you can flush your input buffer with s.flushInput() (you could also check the bytes in your input buffer with s.inWaiting()).

因此,添加刷新调用将确保您读取从那一刻到达的任何字节:

So, adding the flush call will make sure that you read whatever bytes arrive from that moment:

while True:
    s.flushInput()
    print s.readline()
    time.sleep(.5)

您可能还需要考虑使用读取超时(当您创建您的 serial.Serial 对象时,使用超时参数).在这种情况下,您需要在超时时处理空读数,但您要保证,如果发送方出于任何原因停止,您的阅读器不会永远等待.

You may also want to consider using a read timeout (when you create your serial.Serial object as, with timeout parameter). In this case you would need to deal with empty readings when there is a timeout, but you would guarantee that if the sender stops for any reason, your reader will not be waiting forever.

这篇关于读取串行端口 - 在一定时间内忽略写入串行端口的部分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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