简体   繁体   English

读取串口数据 - 逐字节

[英]Reading data on serial port - byte by byte

How to read data on serial port in byte by byte fashion.如何以字节方式读取串行端口上的数据。

I have a source which sends out packets of varying packet size.我有一个发送不同数据包大小的数据包的源。 I am reading the data in raw mode(non-canonical).我正在以原始模式(非规范)读取数据。 When i set VMIN, i am able to get packet of that size or slightly larger.当我设置 VMIN 时,我能够获得该大小或稍大的数据包。 for ex: If the received packet size is 46 bytes, and if i set VMIN to say '1'.例如:如果接收到的数据包大小为 46 字节,并且我将 VMIN 设置为“1”。 I receive the data in 2 chunks(meaning 2 read calls were needed to get the complete data with one fetching first 32 and next fetching the rest 14 bytes).我以 2 个块接收数据(这意味着需要 2 次读取调用来获取完整数据,其中一次获取前 32 个字节,然后获取其余 14 个字节)。 If i set VMIN to 46, complete packet is fetched.如果我将 VMIN 设置为 46,则获取完整的数据包。

But the problem here is varying packet size .但这里的问题是不同的数据包大小 If the data packet size is more(say 70 bytes), it will mess up the buffer and following reads as it reads 60+ bytes in first read and rest in next read.如果数据包大小更大(比如 70 字节),它会弄乱缓冲区和后续读取,因为它在第一次读取时读取 60 多个字节,并在下一次读取时剩余。

So i am thinking to read the data byte by byte and determine the end of the packet.所以我想逐字节读取数据并确定数据包的结尾。

Does anyone know if it is do-able.有谁知道它是否可行。 Or any suggestion on how to read the complete data packet in one read operation.或有关如何在一次读取操作中读取完整数据包的任何建议。

UART setting: Baud: 115200 No parity. UART 设置:波特:115200 无奇偶校验。 1 stop bit. 1 个停止位。 8N1. 8N1。 No flow control.没有流量控制。

Thanks in advance.提前致谢。

A good approach for for processing serial data is to Read chunks of data from port the into a buffer and then pull byte by byte from the buffer.处理串行数据的一个好方法是从端口读取数据块到缓冲区中,然后从缓冲区中逐字节提取。

Serial port reading is affected by the timeout settings and incoming data flow, so the number of bytes per read are not guaranteed to be consistent.串口读取受超时设置和传入数据流的影响,因此无法保证每次读取的字节数一致。 For example, if you knew that packets were always going to be 46 bytes, then you might think to set Vmin to 46 and expect to get 46 bytes per read.例如,如果您知道数据包总是 46 字节,那么您可能会考虑将 Vmin 设置为 46 并期望每次读取获得 46 字节。 However, if the sending source sends multiple packets without delays between, then you might get all of one and part of another packet.但是,如果发送源发送多个数据包而没有延迟,那么您可能会收到另一个数据包的全部和一部分。 If the sending source were to delay during the transmission of a packet for longer than the receiving port's timeout, then you would get fewer than Vmin bytes.如果发送源在数据包传输过程中的延迟时间长于接收端口的超时时间,那么您将获得少于 Vmin 的字节数。

Be sure to code for the possibility of lost data.请务必对丢失数据的可能性进行编码。 For example, let's say that packets start with and ends with .例如,假设数据包以 . You start pulling data from the buffer and the first byte is , but 49 bytes later you encounter meaning a new packet, but without having seen the from the previous packet.您开始从缓冲区中提取数据,第一个字节是 ,但是 49 字节后您遇到了一个新数据包,但没有看到前一个数据包中的 。 There should of course also be a CRC for the packet, or at least a checksum.当然也应该有一个数据包的CRC,或者至少是一个校验和。

Since you are reading data that is structured into packets of variable size, you should add a 2 byte header for each packet and set it to the packet size.由于您正在读取结构化为可变大小数据包的数据,您应该为每个数据包添加一个 2 字节的标头并将其设置为数据包大小。

In the reader you would read 2 bytes first and then decide how many bytes to read to receive the whole packet.在阅读器中,您将首先读取 2 个字节,然后决定读取多少字节以接收整个数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM