串口读取不完整 [英] Serial port read is not complete
问题描述
因此,以下功能用于在Linux下从串行端口读取数据.调试时可以读取完整的数据,但是启动程序时, read_buffer 似乎不完整.我可以正确接收一小部分数据,但缓冲区的其余部分完全为 zero .可能是什么问题?
int8_t __serial_port_open(uint8_t *port)
{
mode_t perms = S_IRWXU;
fd = open(port, O_RDWR | O_NOCTTY | O_SYNC, perms);
if (fd < 0)
{
return -1;
}
if (__serial_port_configure() != 0)
return -1;
return 0;
}
static int8_t __serial_port_configure(void)
{
struct termios attr;
if (tcgetattr(fd, &attr) == -1)
{
return -1;
}
if (cfsetispeed(&attr, B115200) == -1)
{
return -1;
}
if (cfsetospeed(&attr, B115200) == -1)
{
return -1;
}
attr.c_cflag |= (CLOCAL | CREAD);
attr.c_cflag &= ~PARENB;
attr.c_cflag &= ~CSTOPB;
attr.c_cflag &= ~CSIZE;
attr.c_cflag |= (CS8);
attr.c_cflag |= CRTSCTS;
attr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
attr.c_iflag &= ~(IXON | IXOFF | IXANY);
attr.c_oflag &= ~OPOST;
if (tcsetattr(fd, TCSANOW, &attr) == -1)
{
return -1;
}
return 0;
}
int8_t __serial_port_read(uint8_t *read_buffer, uint32_t nbytes_to_read, uint32_t *nbytes_read)
{
do
{
*nbytes_read = read(fd, read_buffer, nbytes_to_read);
if (*nbytes_read == -1)
{
return -1;
}
} while (*nbytes_read == 0);
return 0;
}
来自该人
read()尝试从文件描述符fd读取最多计数字节到从buf开始的缓冲区.
返回值
成功后,返回读取的字节数(零表示文件末尾),
换句话说,count
参数是您要读取的最大字节,但是读取可以返回不同数量的字节.
返回值使您可以从FD读取字节数.
要简单地解决它,您可以执行一个循环来接收字节,直到达到预期的长度为止,每次读取1个字节.
其他解决方案可以使用读取超时 >
The functions below are used consequently to read data from serial port under Linux. I can read the complete data when I debug, but when I launch the program, read_buffer seems not to be complete. I receive the small part of the data correctly but the rest of the buffer is completely zero. What could be the problem?
int8_t __serial_port_open(uint8_t *port)
{
mode_t perms = S_IRWXU;
fd = open(port, O_RDWR | O_NOCTTY | O_SYNC, perms);
if (fd < 0)
{
return -1;
}
if (__serial_port_configure() != 0)
return -1;
return 0;
}
static int8_t __serial_port_configure(void)
{
struct termios attr;
if (tcgetattr(fd, &attr) == -1)
{
return -1;
}
if (cfsetispeed(&attr, B115200) == -1)
{
return -1;
}
if (cfsetospeed(&attr, B115200) == -1)
{
return -1;
}
attr.c_cflag |= (CLOCAL | CREAD);
attr.c_cflag &= ~PARENB;
attr.c_cflag &= ~CSTOPB;
attr.c_cflag &= ~CSIZE;
attr.c_cflag |= (CS8);
attr.c_cflag |= CRTSCTS;
attr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
attr.c_iflag &= ~(IXON | IXOFF | IXANY);
attr.c_oflag &= ~OPOST;
if (tcsetattr(fd, TCSANOW, &attr) == -1)
{
return -1;
}
return 0;
}
int8_t __serial_port_read(uint8_t *read_buffer, uint32_t nbytes_to_read, uint32_t *nbytes_read)
{
do
{
*nbytes_read = read(fd, read_buffer, nbytes_to_read);
if (*nbytes_read == -1)
{
return -1;
}
} while (*nbytes_read == 0);
return 0;
}
From the man
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
Return Value
On success, the number of bytes read is returned (zero indicates end of file),
In other words count
parameter is the maximum bytes you want to read, but read can return a different number of bytes.
Returned value gives you the number of read bytes from FD.
To simply solve it you can do a loop receiving bytes until the expected length is reached, reading 1 byte a time.
Other solution can be implemented using Read Timeouts
这篇关于串口读取不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!