免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4064 | 回复: 2
打印 上一主题 下一主题

linux 串口可以接收数据,不能发送 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-26 17:53 |只看该作者 |倒序浏览
如题,经检查Write函数的返回值是不为-1的.
  1. #include     <stdio.h>      /*标准输入输出定义*/
  2. #include     <stdlib.h>     /*标准函数库定义*/
  3. #include     <unistd.h>     /*Unix标准函数定义*/
  4. #include     <sys/types.h>  /**/
  5. #include     <sys/stat.h>   /**/
  6. #include     <fcntl.h>      /*文件控制定义*/
  7. #include     <termios.h>    /*PPSIX终端控制定义*/
  8. #include     <errno.h>      /*错误号定义*/
  9. #define FALSE 0
  10. #define TRUE 1
  11. int set_Parity(int fd,int databits,int stopbits,int parity)
  12. {
  13. struct termios options;
  14. if  ( tcgetattr( fd,&options)  !=  0)
  15. {
  16.   perror("SetupSerial 1");     
  17.   return(FALSE);  
  18. }
  19. options.c_cflag &= ~CSIZE;
  20. options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/
  21. options.c_oflag  &= ~OPOST;     /*Output*/
  22. // options.c_iflag   &= ~IXON;         //0x11
  23. // options.c_iflag   &= ~ICRNL;        //0x0d
  24. //  options.c_cflag|=CLOCAL;
  25. //  options.c_cflag|=CREAD;
  26. //  options.c_cflag&=~CRTSCTS;   /*T*/
  27.   
  28. switch (databits) /*y*/
  29. {   
  30. case 7:           
  31.   options.c_cflag |= CS7;
  32.   break;
  33. case 8:     
  34.   options.c_cflag |= CS8;
  35.   break;   
  36. default:   
  37.   fprintf(stderr,"Unsupported data size\n"); return (FALSE);  
  38. }
  39. switch (parity)
  40. {   
  41. case 'n':
  42. case 'N':   
  43.   options.c_cflag &= ~PARENB;   /* Clear parity enable */
  44.   options.c_iflag &= ~INPCK;     /* Enable parity checking */
  45.   break;  
  46. case 'o':   
  47. case 'O':     
  48.   options.c_cflag |= (PARODD | PARENB); /* éè???a??D§?é*/  
  49.   options.c_iflag |= INPCK;             /* Disnable parity checking */
  50.   break;  
  51. case 'e':  
  52. case 'E':   
  53.   options.c_cflag |= PARENB;     /* Enable parity */   
  54.   options.c_cflag &= ~PARODD;   /* ×a???a??D§?é*/     
  55.   options.c_iflag |= INPCK;       /* Disnable parity checking */
  56.   break;
  57. case 'S':
  58. case 's':  /*as no parity*/   
  59.   options.c_cflag &= ~PARENB;
  60.   options.c_cflag &= ~CSTOPB;
  61.   break;  
  62. default:   
  63.   fprintf(stderr,"Unsupported parity\n");   
  64.   return (FALSE);  
  65. }  
  66. /*stopbits*/  
  67. switch (stopbits)
  68. {   
  69. case 1:   
  70.   options.c_cflag &= ~CSTOPB;  
  71.   break;  
  72. case 2:   
  73.   options.c_cflag |= CSTOPB;  
  74.   break;
  75. default:   
  76.   fprintf(stderr,"Unsupported stop bits\n");  
  77.   return (FALSE);
  78. }
  79. /* Set input parity option */
  80. if (parity != 'n')
  81. {   
  82.   options.c_iflag |= INPCK;
  83. }
  84. tcflush(fd,TCIFLUSH);
  85. options.c_cc[VTIME] = 0; /* seconds*/   
  86. options.c_cc[VMIN] = 1; /* define the minimum bytes data to be readed*/
  87. if (tcsetattr(fd,TCSANOW,&options) != 0)   
  88. {
  89.   perror("SetupSerial 3");   
  90.   return (FALSE);  
  91. }
  92. return (TRUE);  
  93. }
  94. void set_speed(int fd, int speed)
  95. {
  96. int   i;
  97. int   status;
  98. struct termios   Opt;
  99. tcgetattr(fd, &Opt);
  100. int speed_arr[] = { B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
  101.       B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  102. int name_arr[] = { 115200,38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,  
  103.       19200,  9600, 4800, 2400, 1200,  300, };
  104. for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
  105. {
  106.   if  (speed == name_arr[i])
  107.   {
  108.    tcflush(fd, TCIOFLUSH);     
  109.    cfsetispeed(&Opt, speed_arr[i]);  
  110.    cfsetospeed(&Opt, speed_arr[i]);   
  111.    status = tcsetattr(fd, TCSANOW, &Opt);  
  112.    if  (status != 0)
  113.    {
  114.     perror("tcsetattr fd");  
  115.     return;     
  116.    }
  117.    tcflush(fd,TCIOFLUSH);   
  118.   }  
  119. }
  120. }
  121. /**
  122. *@breif 打开串口
  123. */
  124. int OpenDev(char *Dev)
  125. {
  126. int fd = open( Dev, O_RDWR | O_NOCTTY);         // | O_NDELAY
  127. if (-1 == fd)
  128.   { /*设置数据位数*/
  129.    perror("Can't Open Serial Port");
  130.    return -1;
  131.   }
  132. else
  133. return fd;
  134. }
  135. /**
  136. *@breif  main()
  137. */
  138. int main(int argc, char **argv)
  139. {
  140. int fd;
  141. int nread;
  142. int nr;
  143. char buffer[512]="Hello";
  144. char buff[512];
  145. char *dev ="/dev/ttyS1";

  146. fd = OpenDev(dev);
  147. if (fd>0)
  148.   set_speed(fd,9600);
  149. else
  150. {
  151.   printf("Can't Open Serial Port!\n");
  152.   exit(0);
  153. }
  154. if (set_Parity(fd,8,1,'N')== FALSE)
  155. {
  156.   printf("Set Parity Error\n");
  157.   exit(1);
  158. }
  159.   
  160.   nr = write(fd, buffer ,4);
  161.   printf("write data is %d\n", nr);
  162.   while(1)
  163.    {
  164.      while((nread = read(fd,buff,512))>0)
  165.      {
  166.         printf("Len %d,",nread);
  167.   //      nread = write(fd,buff,512);
  168.         buff[nread+1]='\0';
  169.         printf("%s\n",buff);
  170.       }
  171.    }
  172.     //close(fd);
  173.     //exit(0);
  174. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2009-08-27 11:13 |只看该作者
自己顶下.希望知道的能回答下,谢谢!

论坛徽章:
0
3 [报告]
发表于 2009-08-27 12:09 |只看该作者
友情帮顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP