gpt4 book ai didi

c - 从串口读取超过8个字节

转载 作者:行者123 更新时间:2023-11-30 15:32:16 26 4
gpt4 key购买 nike

我需要从串口读取数据,但是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);

我尝试使用 do while,但结果仍然相同。

最佳答案

[评论太长]

试试这个代码:

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);

关于c - 从串口读取超过8个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24310402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com