RS232通讯,通用定时

发布于 2024-08-31 01:25:09 字数 407 浏览 2 评论 0原文

我有一个硬件,它通过串行端口发送一个字节的数据,表示频率为 100Hz 的电压信号。

我想编写一个程序来读取数据,以便我可以绘制它。我知道我需要打开串行端口并打开输入流。但下一部分让我感到困惑,我无法从概念上理解该过程:

我创建了一个 while 循环,每次从输入流读取 1 个字节的数据。如何获得 while 循环计时,以便在到达 readbyte 行时始终有一个字节可供读取?我猜我不能只是在 while 循环中放置一个 sleep 函数来尝试将其与硬件采样率相匹配。是否只是在 while 循环中继续读取输入流的问题,如果它太快那么它不会做任何事情(因为没有新数据),如果它太慢那么它会累积在输入流缓冲区中?

就像我说的,我只是想从概念上理解这一点,所以任何指导将不胜感激!我猜这个想法与我使用的编程语言无关,但如果不是,则假设它用于 Java。

I have a piece of hardware which sends out a byte of data representing a voltage signal at a frequency of 100Hz over the serial port.

I want to write a program that will read in the data so I can plot it. I know I need to open the serial port and open an inputstream. But this next part is confusing me and I'm having trouble understanding the process conceptually:

I create a while loop that reads in the data from the inputstream 1 byte at a time. How do I get the while loop timing so that there is always a byte available to be read whenever it reaches the readbyte line? I'm guessing that I can't just put a sleep function inside the while loop to try and match it to the hardware sample rate. Is it just a matter of continuing reading the inputstream in the while loop, and if it's too fast then it won't do anything (since there's no new data), and if it's too slow then it will accumulate in the inputstream buffer?

Like I said, i'm only trying to understand this conceptually so any guidance would be much appreciated! I'm guessing the idea is independent of which programming language I'm using, but if not, assume it is for use in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

潜移默化 2024-09-07 01:25:09

如果您使用的是 Java 通信 API,那么您根本不会进行轮询。
相反,您将实现 SerialPortEventListener,并在端口有可用数据时收到回调。

public class SerialConnection implements SerialPortEventListener
{
      private SerialPort         sPort;

...
    // Add this object as an event listener for the serial port.
       try
      {
          sPort.addEventListener(this);
       }
      catch (TooManyListenersException e)
      {
          sPort.close();
          throw new SerialConnectionException("too many listeners added");
       }

...

If you are using the Java communications API then you will not be polling at all.
Instead you will implement a SerialPortEventListener and will receive a callback when there is data available from the port.

public class SerialConnection implements SerialPortEventListener
{
      private SerialPort         sPort;

...
    // Add this object as an event listener for the serial port.
       try
      {
          sPort.addEventListener(this);
       }
      catch (TooManyListenersException e)
      {
          sPort.close();
          throw new SerialConnectionException("too many listeners added");
       }

...
天暗了我发光 2024-09-07 01:25:09

你等到端口有一个字节(如果是Windows,那么有一个API可以知道rs232是否有一个字节等待,或者进行阻塞读取)...理想情况下,你将读取代码放在一个单独的线程中...等待字节并将它们泵入一些有意义的数据结构。

You wait till the port has a byte (if its windows, then there is a api to know whether rs232 has a byte waiting, or do a blocking read)... ideally you put your reading code in a separate thread...wait for the bytes and pump them into some meaningful datastructure.

余生共白头 2024-09-07 01:25:09

100Hz 相当慢 - 比如说 9 毫秒“睡眠”不会有问题...

但正如 Hamish 所说,可能有一个事件通知缓冲区中有数据 - 使用它并记住,如果您的读数是在 ASCII 或多个字节中,您需要缓冲获得的字节,直到在实际处理之前完整读取(或 ASCII 行??)。

希望这有帮助...

100Hz is pretty slow - there would be no issue 'sleeping' for say 9 mSecs...

But as Hamish says there is likely an event that notifies that there is data in the buffer - use that and bare in mind that if your readings are in ASCII or over multiple bytes you will need to buffer the bytes you get until you have a full reading (or line of ASCII ??) before actually processing it.

Hope this helps...

稚气少女 2024-09-07 01:25:09

教程: http://devdot.wikispaces.com/Iphone+Serial+Port+Tutorial< /a>

回到概念。

字节通过串行端口作为流出现。因此,如果您的程序太慢而无法获取字节,则会丢失一些字节。有一些方法可以最大限度地减少问题:

  • 串行芯片中的

    缓冲区(可能高达 16 字节或更多)。当有任何数据传入时,系统会中断您的程序并告诉您缓冲区中有一些新字节。这样程序就可以批量获取它们。此外,中断驱动的程序不会通过循环字节来使CPU繁忙。

  • 协议级别。串口只是通道。您的程序和串行端口另一端的程序可能具有更高级别的协议,以简化流量并最大限度地减少数据丢失。典型的机制是

希望有帮助

tutorial: http://devdot.wikispaces.com/Iphone+Serial+Port+Tutorial

back to concept.

bytes come as a stream via the serial port. so if you program is too slow to pick up the bytes, some bytes are lost. there are some ways to minimize the problem:

  • buffer (probably up to 16 bytes or more) in the serial chip. when there is any data coming in, the system interrupt your program and tell you there is some new bytes in the buffer. so the program can get them in a batch. also, an interrupt-driven program does not make the cpu busy by looping for bytes.

  • at protocol level. serial port is only the channel. your program and the program at other side of the serial port may have a higher level protocol to streamline traffic and minimize data lost. a typical mechanism is xon-xoff.

hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文