打印
[嵌入式Linux]

关于Linux下的串口使用问题

[复制链接]
894|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
rentian668288|  楼主 | 2014-4-21 17:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
向各位大神问个问题  在Linux下 开辟一个线程 循环向串口read    向串口发送数据时。为什么有时会造成数据读不准确
有没有什么好的方法解决这问题
 
我的串口设置代码如下:
int open_port(int fd,int comport)        //打开串口函数
{
char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC2"};

if(comport==1)         //打开COM0口
{
fd=open(dev[0],O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd){
perror("Can't Open Serial Port");
return (-1);
}
}
else if(comport==2)         //打开COM1口
{
fd=open(dev[1],O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd){
perror("Can't Open Serial Port");
return (-1);
}
}
else if(comport==3)         //打开COM2口
{
fd=open(dev[2],O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("Can't Open Serial port");
return (-1);
}
}

if(fcntl(fd,F_SETFL,0)<0)       //恢复串口为阻塞状态
printf("fcntl failed!\n");

if(isatty(STDIN_FILENO)==0)         //测试是否为终端设备
printf("standard input is not a temrinal device\n");

printf("fd_open=%d\n",fd);

return fd;
}

int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{
struct termios newtio,oldtio;

if(tcgetattr(fd,&oldtio)!=0) //保存测试现有串口参数设置
    {       
perror("SetupSerial 1");
return -1;
}
bzero(&newtio,sizeof(newtio));

newtio.c_cflag |=CLOCAL | CREAD;    //设置字符大小
newtio.c_cflag &= ~CSIZE;

switch(nBits)        //设置停止位
{
case 7:
 newtio.c_cflag |=CS7;
 break;
case 8:
 newtio.c_cflag |=CS8;
 break;
}

switch(nEvent)  //设置奇偶校验位
{
case 'O':        //奇数
 newtio.c_cflag |=PARENB;
 newtio.c_cflag |=PARODD;
 newtio.c_iflag |=(INPCK | ISTRIP);
 break;
case 'E':        //偶数
 newtio.c_iflag |=(INPCK|ISTRIP);
 newtio.c_cflag |=PARENB;
 newtio.c_cflag &=~PARODD;
 break;
case 'N':  //无奇偶校验位
 newtio.c_cflag &=~PARENB;
 break;
}

switch(nSpeed) //设置数据传输率
{
case 2400:
cfsetispeed(&newtio,B2400);
cfsetospeed(&newtio,B2400);
break;
case 4800:
cfsetispeed(&newtio,B4800);
cfsetospeed(&newtio,B4800);
break;
case 9600:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
case 115200:
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
break;
case 460800:
cfsetispeed(&newtio,B460800);
cfsetospeed(&newtio,B460800);
break;
default:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
}

if(nStop==1)        //设置停止位
newtio.c_cflag&=~CSTOPB;
else if(nStop==2)
newtio.c_cflag |=CSTOPB;

/*设置等待时间和最小接收字符*/
newtio.c_cc[VTIME]=1;
newtio.c_cc[VMIN]=255;

/*  原始输入*/
//newtio.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
//newtio.c_lflag|=(ICANON|ECHO|ECHOE);

/*处理未接收字符*/
tcflush(fd,TCIFLUSH);

/*激活新配置*/
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;       
}
void * serial_recv(void *arg)
{
   
       while(1)
       {
                 nreads=read(fd,frame_read_buf,SIZE_BUF);
                if(nreads<=2)
continue;
 
       }   

}


int  main()
{
               if((fd=open_port(fd,3))<0)
{
perror("open_port error");
return 0;
}

if((i=set_opt(fd,115200,8,'N',1))<0)
{
perror("set_opt error");
return 0;
}
ret=pthread_create(&serial_recv_tid,NULL,serial_recv,(void *)fd);//创建线程

}


程序大意是这样,我想问的是为什么每次想串口发送13个字节数据  有时总有1、2个字节错误。 请问是不是我串口设置有问题
,还是这种循环向串口读数据的方法不对。求解呀。大神们

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝