串行通信 RX 大小限制

Serial communication RX size limitations

我无法接收我发出的所有字节。 (Raspberry Pi 3B+ / Raspbian)

我尝试以 9600bit/s 的速度发送 4000 字节而只接收 960 字节。当我提高速度时,增加了接收到的字节数。我尝试在 c_cc 数组中设置 VTIME 和 VMIN 值,但不更改所有内容。

如何接收所有字节?

transmitter:

char write_buffer[4000];    /* Buffer containing characters to write into port       */

        for(uint32_t i=0;i<4000;i++)
        {
            write_buffer[i] = i;
        }

        int  bytes_written  = 0;    /* Value for storing the number of bytes written to the port */ 

        bytes_written = write(fd,write_buffer,sizeof(write_buffer));/* use write() to send data to port                                            */
                                         /* "fd"                   - file descriptor pointing to the opened serial port */
                                         /* "write_buffer"         - address of the buffer containing data              */
                                         /* "sizeof(write_buffer)" - No of bytes to write                               */  
        printf("\n  %s written to ttyUSB0",write_buffer);
        printf("\n  %d Bytes written to ttyUSB0", bytes_written);
        printf("\n +----------------------------------+\n\n");

receiver:

/*------------------------------- Read data from serial port -----------------------------*/

        char read_buffer[4000];   /* Buffer to store the data received              */
        uint32_t  bytes_read = 0;    /* Number of bytes read by the read() system call */
        int i = 0;

        bytes_read = read(fd,&read_buffer,4000); /* Read the data                   */

        printf("\n\n  Bytes Rxed: %d", bytes_read); /* Print the number of bytes read */
        printf("\n\n  ");

        for(i=0;i<bytes_read;i++)    /*printing only the received characters*/
            printf("%c",read_buffer[i]);

        printf("\n +----------------------------------+\n\n\n");

输出:

 +----------------------------------+
 |        Serial Port Write         |
 +----------------------------------+
  ttyUSB0 Opened Successfully 
  BaudRate = 9600 
  StopBits = 1 
  Parity   = none
   written to ttyUSB0
  4000 Bytes written to ttyUSB0
 +----------------------------------+



  Bytes Rxed: 960




 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~������������������������������������������������������������������������������������������������������������������������������


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~������������������������������������������������������������������������������������������������������������������������������


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~������������������������������������������������������������������������������������������������������������������������������


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~��������������������������������������������������������������
 +----------------------------------+

read 系统调用不保证 return 请求的字节数。有关完整参考,请参阅 this link。

如果 read return 是一个非负值,此值表示读取的字节数,在您的情况下为 960 字节。

为了确保所有字节都被成功读取,您需要将对 read 的调用置于某种形式的循环中。可能与此类似:

char buffer[4000 + 1]; /* +1 to leave space for null-terminator */

memset(buffer, 0, sizeof(buffer)); /* Clear buffer */

size_t bytesToRead = 4000;
size_t bytesRead = 0;
while (bytesToRead > 0)
{
    const ssize_t retVal = read(fd, buffer + bytesRead, bytesToRead);
    if (retVal < 0)
    {
        /* Handle error */
    }

    const size_t bytes = (size_t) retVal;
    bytesRead += bytes;
    bytesToRead -= bytes;
}