我正在使用串口从设备接收数据 . 通信工作正常,但读取数据存在问题 . 我正在使用Linux(Ubuntu) .
这是我的代码:
int OpenPort(char *PortName, int *FileDesc) {
*FileDesc = open (PortName, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
perror("fakap");
return (P_OPEN_ERROR);
}
else return(P_OPEN_SUCCESS);
};
int SetPortAtributes (int fd, int speed, int parity) {
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
perror("error %d from tcgetattr");
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag |= (IGNBRK | IGNPAR);,
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_iflag &= ~(ISTRIP);
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD | HUPCL); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_lflag = 0;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
perror("error %d from tcsetattr");
return -1;
}
return 0;
}
阅读部分:
void PortRead(int *FileDesc) {
memset(&recBuff[0], 0, sizeof(recBuff)); //clear buff
int n = read (*FileDesc, recBuff, sizeof(recBuff)); // read
printf("n: %d \n", n);
int i = 0;
for(i = 0;i<n;i++) {
recData.buf[i].b_int = recBuff[i]; // put rec buff to ANS_u type variable
}
};
一切正常,直到我收到大于8字节的消息 . read()读取的字节数不超过8个,所以我必须使用read()第二次读取读取所有数据 . 当我使用GtkTerm时,一切正常,但是在C实现期间存在问题 .
3 回答
例如,可能会发生这种情况,因为现在实际可用的字节数较少(可能是因为我们接近文件结尾,或者因为我们正在从管道或终端读取),或者因为read()被中断了信号 .
只要有可用数据,就重复读取 .
此外,由于另一端是一个微控制器,它可能比你的工作站大很多,所以当你执行read()时,数据可能还没有 . 当然,这是再次尝试的另一个原因 .
所以看起来你可以试试
你必须准备好在任何情况下拨打
read()
第二,第三,第四等时间 . Always . 只是因为你知道对方已经发送了16个字节,你不能假设在一次操作中所有16个字节都将来自read()
. 它可能是8 8,16 * 1,3 3 10,或任何其他总和达16的组合(尽管大多数这些组合极不可能) .从阅读手册页(2):
你也应该像已经评论的那样准备好处理EINTR .
在函数OpenPort中,open的结果被分配给* FileDesc,但是检查了一些其他fd变量,这也应该被修复 .
我不确定,但看起来'recBuff'是一个指向intput缓冲区的指针 .
如果这是真的,则sizeof(recBuff)等于8.(指针大小为8) . 可以理解的是,最多8个字节是愿意读取的 .