读取串行端口

编程入门 行业动态 更新时间:2024-10-15 02:30:07
本文介绍了读取串行端口 - 在特定时间内忽略写入串行端口的部分数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想每隔一段时间在串行端口上读取数据和 Arduino.所以本质上就像

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

  • 读一读
  • 等一下
  • 读一读
  • 等一下
  • 拿...
  • 我面临的问题是端口将缓冲其信息,因此只要我调用等待函数,串行端口上的数据就会开始缓冲.一旦等待函数完成,我尝试再次读取数据,但我从缓冲区的开头读取数据,数据不再是当前的,而是在等待函数开始时读取的数据.

    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)

    您可能还需要考虑使用读取超时(当您使用 timeout 参数创建 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.

    更多推荐

    读取串行端口

    本文发布于:2023-11-27 13:34:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1638291.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:端口

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!