求助关于Linux下串口收数据的问题

lishi_1991 2014-07-17 11:13:00
现在想做一个项目从windows通过串口向arm-linux发几字节的小数据,写完程序调试时候发现Linux下串口默认行缓冲,收到数据先放在缓冲区内,只有当收到回车符的才会发出SIGIO信号,这是才能read出数据。
但是项目中windows下发数据不是由我控制的,发送数据无法自动添加回车符,这样windows下发送小数据的时候,我这边Linux程序就read不出数据,这该怎么解决呢?
下面贴上我串口设置及收数据的代码:

串口设置代码:
int OpenDev(char *Dev)
{
//int fd = open( Dev, O_RDWR | O_NOCTTY | O_NDELAY ); //| O_NOCTTY | O_NDELAY
int fd = open( Dev, O_RDWR); //| O_NOCTTY | O_NDELAY
if (fd == -1)
{
perror("Can't Open Serial Port");
return -1;
}
else
{
return fd;
}
}
/**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size/n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(stderr,"Unsupported parity/n");
return (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits/n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
//tcflush(fd,TCIFLUSH);
tcflush(fd,TCIOFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}


读取数据代码:
void *uart_recv(void *args)
{
int fd_uart;
int nread;
char buff[512];
char command[128];
int fp_save = 0;
int dev_num;

dev_num = (int) args;
sprintf(command, "stty -F /dev/ttyO%d 115200", dev_num);
printf("set tty: %s\n", command);
system(command);

sprintf(command, "uart%d.data", dev_num);
fp_save = open(command, O_WRONLY|O_CREAT|O_APPEND);
if(fp_save == -1)
{
printf("open file err\n");
}

sprintf(command, "/dev/ttyO%d", dev_num);
fd_uart = OpenDev(command);
set_speed(fd_uart, 115200);
if (set_Parity(fd_uart, 8, 1, 'N') == FALSE)
{
printf("Set Parity Error/n");
return 0;
}

while(1)
{
memset(buff, 0, sizeof(buff));
nread = read(fd_uart, buff, 16);
if(nread > 0)
{
if(strstr(buff, FINISH_FLAG) != NULL )
{
printf("recv FINISH_FLAG so exit \n");
break;
}
else
{
buff[nread] = '\0';
write(fp_save, buff, nread);
printf("receive ascii:%s receive byte:%d\n", buff, nread);
}
}
}
close(fp_save);
close(fd_uart);
return NULL;
}

望各位高手不吝赐教
...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lishi_1991 2015-09-05
  • 打赏
  • 举报
回复
结贴其实就是简单的串口设置问题
lishi_1991 2014-07-18
  • 打赏
  • 举报
回复
引用 1 楼 subfate 的回复:
没遇到过接收回车后才能读出的情况。 我直接用read来读取,可以读到上位机发来的数据。——而不管是否是回车。
那能不能把你的设置和接收这一段代码贴出来看一下呢 不胜感激啊
李迟 2014-07-18
  • 打赏
  • 举报
回复
上网找一下,资料很多的。 我最近参与了一个实例:libvisca,你下载源码来看看。
李迟 2014-07-17
  • 打赏
  • 举报
回复
没遇到过接收回车后才能读出的情况。 我直接用read来读取,可以读到上位机发来的数据。——而不管是否是回车。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧