Ограничения размера RX последовательной связи - PullRequest
0 голосов
/ 15 января 2019

Я не могу получить все байты, которые я посылаю. (Raspberry Pi 3B + / Raspbian)

Я попытался отправить 4000 байтов и получить только 960 байтов на скорости 9600 бит / с. Когда я увеличил скорость, увеличил полученные байты. Я пытаюсь установить значения VTIME и VMIN в массиве c_cc, но не изменить все.

Как я могу получить все байты?

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{|}~��������������������������������������������������������������
 +----------------------------------+

1 Ответ

0 голосов
/ 15 января 2019

Системный вызов read не гарантирует возврата запрошенного количества байтов. См. эту ссылку для полной ссылки.

Если read возвращает неотрицательное значение, это значение указывает количество прочитанных байтов, в вашем случае 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;
}
...