【发布时间】:2018-09-02 15:06:32
【问题描述】:
我已经在 C 中实现了从串行端口的阻塞读取。我的目标是读取阻塞直到新数据到达。
这是我实现串行伪对象的方式(为了使代码更清晰,我删除了多线程保护)。
typedef struct
{
int fd;
se_serial_speed_t speed;
se_serial_parity_t parity;
bool flow_control;
}se_serial_t;
int se_serial_constructor(se_serial_t** self, char* serial_port)
{
int fd;
if(NULL != *self)
{
return ERR_NNULL_PTR;
}
else if(0 != access(serial_port, F_OK))
{
ERRNO("Serial port is not available");
return ERR_ILLEGAL_PARAM;
}
else
{
if(-1 == (fd = open(serial_port, O_RDWR | O_NOCTTY)))
{
ERRNO("Error opening %s in rw mode", serial_port);
return ERR_OFILE_FAIL;
}
else if(NULL == (*self = malloc(sizeof(se_serial_t))))
{
ERROR("Error allocating memory for Serial");
return ERR_MALLOC_FAIL;
}
(*self)->fd = fd;
}
return ERR_OK;
}
int se_serial_configure_interface(se_serial_t* self, se_serial_speed_t speed, se_serial_parity_t parity, bool flow_control)
{
struct termios options;
if(NULL == self)
{
return ERR_NULL_PTR;
}
else
{
if(0 != tcgetattr(self->fd, &options))
{
ERRNO("Unable to get serial port current configuration");
}
if(0 != cfsetospeed(&options, speed))
{
ERRNO("Unable to set serial port output speed");
}
if(0 != cfsetispeed(&options, speed))
{
ERRNO("Unable to set serial port input speed");
}
switch(parity)
{
case SE_SERIAL_PARITY_8N1:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
break;
case SE_SERIAL_PARITY_7E1:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
break;
case SE_SERIAL_PARITY_7O1:
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
break;
case SE_SERIAL_PARITY_7S1:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
break;
default:
WARNING("Unable to set serial port parity");
break;
}
if(flow_control)
options.c_cflag |= CRTSCTS;
else
options.c_cflag &= ~CRTSCTS;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
if(0 != tcsetattr(self->fd, TCSANOW, &options))
{
ERRNO("Error configuring serial port");
return ERR_SERIAL_CONF_FAIL;
}
self->speed = speed;
self->parity = parity;
self->flow_control = flow_control;
}
return ERR_OK;
}
int se_serial_read(se_serial_t* self, uint8_t* buffer, int size)
{
int bytes_read = 0;
int ret;
if(NULL == self)
{
return ERR_NULL_PTR;
}
else
{
while(bytes_read < size)
{
if(0 > (ret = read(self->fd, &(buffer[bytes_read]), size - bytes_read)))
{
ERROR("Error reading from %s : %d\n", self->serial_port, ret);
return ERR_RFILE_FAIL;
}
bytes_read += ret;
}
size = bytes_read;
}
return size;
}
与我通信的设备在启动后每秒发送 11 个字节的帧。
所以我在初始化串口后无限循环接收帧,然后打印它们。
se_serial_t* serial = NULL;
uint8_t buffer[1024] = {0};
int ret = 0;
int i;
if(0 > (ret = se_serial_constructor(&serial, "/dev/ttyUSB0")))
{
ERROR("Error creating serial : %d", ret);
return ERR_SERIAL_CREATION_FAIL;
}
else if(0 > (ret = se_serial_configure_interface(serial, SE_SERIAL_SPEED_B115200, SE_SERIAL_PARITY_8N1, false)))
{
ERROR("Error configuring serial interface : %d", ret);
return ERR_SERIAL_CONFIG_FAIL;
}
while(1)
{
if(0 > (ret = se_serial_read(serial, buffer, 11)))
{
ERROR("Error reading from serial : %d", ret);
return ret;
}
else
{
for(i=0;i<ret;i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
}
我得到的结果奇怪的是,即使我知道设备正在发送帧,读取也会永远阻塞。
但是,如果我使用 minicom 等其他程序打开端口,我可以接收其中的帧。一旦使用 minicom 打开端口并退出它,我的程序就可以正常运行,直到下次重新启动计算机。
如果我重新启动设备,代码会阻塞,直到它开始发送帧并很好地接收它们。
我还在 Raspberry Pi 3 上进行了尝试,以确保这不是我的笔记本电脑的配置问题,但我得到了相同的结果。
有人知道我为什么会出现这种行为吗?
【问题讨论】:
标签: c linux serial-port blocking termios