简体   繁体   English

从串行端口读取超过8个字节

[英]read more than 8 bytes from the serial port

I need to read data from the serial port, but the read() function always return 8 bytes or less, how can I do to get all data the c code: 我需要从串行端口读取数据,但是read()函数总是返回8个字节或更少的字节,我该怎么做才能通过c代码获取所有数据:

readbytes = read(f, bufptr, sizeof(buffer));
buffer[readbytes]='\0';
printf("number of bytes read is %d\n", readbytes);
printf("buffer %s\n", buffer);

I try to use the do while, but i still have the same result. 我尝试使用do while,但是我仍然有相同的结果。

[too long for a comment] [评论太久]

Try this code: 试试这个代码:

char buffer[256];
memset(buffer, 0, 256);

size_t bytes_read= 0, bytes_expected = 11; 

do {
  ssize_t result = read(f, buffer + bytes_read, bytes_expected - bytes_read); 
  if (0 >= result)
  {
    if (0 > result)
    {
      perror("read()");
    }

    break;
  }

  bytes_read+= result;
} while (bytes_read < bytes_expected)

fprintf(stderr, "Read %zu out of %zu expected bytes: `%s`\n", bytes_read, bytes_expected, buffer);

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

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