如何将字节间延迟超时设置为毫秒? [英] How to set Inter-Byte Delay Timeout to milliseconds?

查看:42
本文介绍了如何将字节间延迟超时设置为毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 termios 在 Linux 中进行串行通信.

I'm currently working with termios for serial communication in Linux.

我需要将字符间超时设置为 5 毫秒.

I need to set an intercharacter timeout to 5ms.

我找到了一种使用 VMIN 和 VTIME 设置字符间超时的方法,其中 VMIN 必须为 VMIN > 0 和 VTIME > 0.

I found a way to set intercharacter timeout using VMIN and VTIME where VMIN has to be VMIN > 0 and VTIME > 0.

问题是我需要将 VTIME 设置为 5ms,但 VTIME 以十分之一秒表示.

The problem is that i need to set the VTIME to 5ms, but the VTIME is expressed in tenths of a second.

VTIME 数据类型是无符号字符,所以我不能将它设置为 0.05.

VTIME data type is unsigned char, so i can't just set it to 0.05.

有人知道有没有办法解决这个问题?

Does anyone know if there is some way around this?

推荐答案

我需要将字符间超时设置为 5 毫秒.
...
有谁知道是否有办法解决这个问题?

I need to set an intercharacter timeout to 5ms.
...
Does anyone know if there is some way around this?

不,没有办法设置比 100 毫秒更短的 termios 超时.
根据您的硬件和内核配置,此超时可能根本不可靠,尤其是在您尝试检测时间分隔消息时.
termios 处理至少是 UART 设备驱动程序之上的一个完整层(请参阅Linux 串行驱动程序).
除非您的内核配置为确保 UART 驱动程序的下半部分和 termios 的 kworker 线程具有高优先级和低延迟,否则无法准确或可靠地确定短字符间隔.

No, there is no way to set a shorter termios timeout than 100 ms.
Depending on your hardware and kernel configuration, this timeout may not be reliable at all, especially if you are trying to detect time-separated messages.
The termios handling is at least a full layer above the UART device driver (see Linux serial drivers).
Unless your kernel is configured to ensure that the bottom-half of the UART driver and the kworker threads for termios are high priority and low latency, then short intercharacter intervals cannot be accurately or reliably determined.

如果 UART 使用 FIFO 来缓冲传入的数据,那么该硬件会掩盖软件可以检测到的字符间距.
同样,当UART驱动程序使用DMA来存储接收到的数据时,字符间时序也会被模糊.
使用 DMA,在 DMA 操作完成之前,CPU 不会参与处理接收到的数据,并且有关任何字符间分离的所有时间信息都消失了.
(使用 DMA 时,帧错误和/或奇偶校验错误等关键信息很难/不可能精确定位到特定字节.)
即使没有 DMA,termios 也只能使用基于通过 tty 翻转缓冲区(这是从线上的时序中移除的一层)传输数据的时序.

If the UART utilizes a FIFO to buffer incoming data, then that hardware obscures the intercharacter spacing that the software can detect.
Similarly when the UART driver is using DMA to store the received data, intercharacter timing will be obscured.
With DMA the CPU is not involved with handling the received data until the DMA operation is complete, and all temporal information about any intercharacter separation is gone.
(Crucial information such as framing error and/or parity error is difficult/impossible to pinpoint to a specific byte when using DMA.)
Even without DMA, termios will only be able to use timing based on the transfer of data through the tty flip buffers (which is a layer removed from the timing on the wire).

某些 UART 确实具有硬件,可帮助通过空闲线路检测消息结束.
例如,Atmel/Microchip ATSAMA5 和 AT91SAM9 SoC 具有带接收器超时功能的 USART,可测量每个接收帧后的空闲时间.
当这个空闲线路时间超过指定值时,可以产生中断.
Atmel USART 的 Linux 驱动程序通常使用接收器超时中断(提前)终止当前 DMA 接收操作,并将 DMA 缓冲区的内容复制到 tty 翻转缓冲区.

Some UARTs do have hardware that assist in detecting the end-of-message by idle line.
For example Atmel/Microchip ATSAMA5 and AT91SAM9 SoCs have USARTs with a Receiver Timeout feature that measures the idle time after each received frame.
When this idle line time exceeds a specified value, an interrupt can be generated.
The Linux driver for the Atmel USART typically uses the receiver-timeout interrupt to (prematurely) terminate the current DMA receive operation, and copy the contents of the DMA buffer to the tty flip buffer.

总而言之,您不能或不应仅依赖 VMIN 和 VTIME 设置来检测时间分隔消息.请参阅解析以时间分隔的 UART 数据.
消息包需要有分隔符/哨兵字符/字节,以便能够可靠地解析和验证消息.
请参阅解析来自串行端口的完整消息,了解高效的示例使用带有本地缓冲区的系统调用.

In summary you cannot or should not rely solely on VMIN and VTIME settings to detect time-separated messages. See Parsing time-delimited UART data.
The message packets need to have delimiter/sentinel characters/bytes so that messages can be reliably parsed and validated.
See parsing complete messages from serial port for an example of efficient use of syscalls with a local buffer.

这篇关于如何将字节间延迟超时设置为毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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