免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: js001sdx
打印 上一主题 下一主题

[C] linux多线程问题求助!! [复制链接]

论坛徽章:
0
21 [报告]
发表于 2010-03-16 09:53 |只看该作者
测了几天没什么进展!!不知道为什么!我把串口部分的等待时间和最小接收字符参数设置成零后,我的串口就只能收到一大堆零!!
修改的串口初始化代码如下:请各位高手帮忙看看啥原因!
  1. int set_speed(int fd, int speed)
  2. {
  3.     int   i;
  4.     int   status;
  5.     struct termios   Opt;
  6.    
  7.     tcgetattr(fd, &Opt);


  8.     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
  9.     {
  10.         if (speed == name_arr[i])
  11.         {     
  12.             tcflush(fd, TCIOFLUSH);     
  13.             cfsetispeed(&Opt, speed_arr[i]);  
  14.             cfsetospeed(&Opt, speed_arr[i]);   
  15.             status = tcsetattr(fd, TCSANOW, &Opt);  
  16.             if  (status != 0)
  17.             {        
  18.                 perror("tcsetattr fd");  
  19.                 return -1;     
  20.             }   
  21.             tcflush(fd,TCIOFLUSH);   
  22.             return 0;
  23.         }  
  24.     }
  25.     return -1;
  26. }
  27. int set_other_attribute(int fd, int nbits, char bevent,  int nstop)
  28. {
  29.         struct termios newtio,oldtio;
  30.         if (tcgetattr(fd, &oldtio) != 0)
  31.     {
  32.         perror("SetupSerial 1");     
  33.         return -1;  
  34.     }
  35.         bzero(&newtio,sizeof(newtio));
  36.         //设置字符大小
  37.         newtio.c_cflag |= CLOCAL | CREAD;
  38.         newtio.c_cflag &= CSIZE;
  39.         //设置数据位
  40.         switch(nbits)
  41.         {
  42.         case 7:
  43.                 newtio.c_cflag |= CS7;
  44.                 break;
  45.         case 8:
  46.                 newtio.c_cflag |= CS8;
  47.         }
  48.         //设置奇偶校验位
  49.         switch(bevent)
  50.         {
  51.         case 'O'://奇数
  52.                 newtio.c_cflag |= PARENB;
  53.                 newtio.c_cflag |= PARODD;
  54.                 newtio.c_iflag |= (INPCK | ISTRIP);
  55.                 break;
  56.         case 'E'://偶数
  57.                 newtio.c_iflag |= (INPCK | ISTRIP);
  58.                 newtio.c_cflag |= PARENB;
  59.                 newtio.c_cflag &= ~PARODD;
  60.                 break;
  61.         case 'N'://无奇偶校验
  62.                 newtio.c_cflag &= ~PARENB;
  63.                 break;
  64.         }
  65.         //设置停止位
  66.         if(nstop == 1)
  67.                 newtio.c_cflag &= ~CSTOPB;
  68.         else if(nstop == 2)
  69.                 newtio.c_cflag |= CSTOPB;
  70.         //设置等待时间和最小接收字符
  71.         newtio.c_cc[VTIME] = 0;
  72.         newtio.c_cc[VMIN] = 0;
  73.         //处理未接收字符
  74.         tcflush(fd,TCIFLUSH);
  75.         //激活配置
  76.         if((tcsetattr(fd,TCSANOW,&newtio)) != 0)
  77.         {
  78.                 perror("Com set error");
  79.                 return -1;
  80.         }
  81.         printf("set done!\n");
  82.         return 0;
  83. }
复制代码

论坛徽章:
0
22 [报告]
发表于 2010-03-17 09:52 |只看该作者
今天测试发现个规律!!
就是我的串口在收较长点的数据帧时,发生错误的地方是有规律的!!
接收的错误数据都是在前一个字节的最后位是‘1’,而它本身的第一位也是‘1’的数据,例如数据帧中有0x33 0x80这样的数据,收到就可能是0x33 0x00
以下是我实验时的数据帧:
贴上来大家帮看下:
68--50--1--0--0--0--0--68--91--18--33--32--33--33--89--83--33--33--ac--49--33--33--a9--44--33-
-33--95--4c--33--33--6b--35--33--33--2--16-
这是正确的帧
68--50--1--0--0--0--0--68--91--18--33--32--33--33--9--83--33--33--ac--49--33--33--a9--44--33--33--95--4c--33--33--6b--35--33--33--2--16-
68--50--1--0--0--0--0--68--91--18--33--32--33--33--89--3--33--33--2c--49--33--33--a9--44--33--33--15--4c--33--33--6b--35--33--33--2--16
这是收到的两个异常帧!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP