LINUX 简单的串口读写实例

 

http://blog.csdn.net/mochouxiyan/article/details/2839509

  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     "string.h"
  7. #include     <fcntl.h>      /*文件控制定义*/
  8. #include     <termios.h>    /*PPSIX 终端控制定义*/
  9. #include     <errno.h>      /*错误号定义*/
  10. #define FALSE  -1
  11. #define TRUE   0
  12. /*********************************************************************/
  13. int OpenDev(char *Dev)
  14. {
  15.     int fd = open( Dev, O_RDWR | O_NOCTTY );         //| O_NOCTTY | O_NDELAY    
  16.     if (-1 == fd)   
  17.     {           
  18.         perror("Can't Open Serial Port");
  19.         return -1;      
  20.     }   
  21.     else    
  22.         return fd;
  23. }
  24. /**
  25. *@brief  设置串口通信速率
  26. *@param  fd     类型 int  打开串口的文件句柄
  27. *@param  speed  类型 int  串口速度
  28. *@return  void
  29. */
  30. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  31.                     B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  32. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,  
  33.                     19200,  9600, 4800, 2400, 1200,  300, };
  34. void set_speed(int fd, int speed)
  35. {
  36.     int   i; 
  37.     int   status; 
  38.     struct termios   Opt;
  39.     tcgetattr(fd, &Opt); 
  40.     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) { 
  41.         if  (speed == name_arr[i]) {     
  42.             tcflush(fd, TCIOFLUSH);     
  43.             cfsetispeed(&Opt, speed_arr[i]);  
  44.             cfsetospeed(&Opt, speed_arr[i]);   
  45.             status = tcsetattr(fd, TCSANOW, &Opt);  
  46.             if  (status != 0) {        
  47.                 perror("tcsetattr fd1");  
  48.                 return;     
  49.             }    
  50.             tcflush(fd,TCIOFLUSH);   
  51.         }  
  52.     }
  53. }
  54. /**
  55. *@brief   设置串口数据位,停止位和效验位
  56. *@param  fd     类型  int  打开的串口文件句柄
  57. *@param  databits 类型  int 数据位   取值 为 7 或者8
  58. *@param  stopbits 类型  int 停止位   取值为 1 或者2
  59. *@param  parity  类型  int  效验类型 取值为N,E,O,,S
  60. */
  61. int set_Parity(int fd,int databits,int stopbits,int parity)
  62.     struct termios options; 
  63.     options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/
  64.     options.c_oflag  &= ~OPOST;   /*Output*/
  65.     if  ( tcgetattr( fd,&options)  !=  0) { 
  66.         perror("SetupSerial 1");     
  67.         return(FALSE);  
  68.     }
  69.     options.c_cflag &= ~CSIZE; 
  70.     switch (databits) /*设置数据位数*/
  71.     {   
  72.     case 7:     
  73.         options.c_cflag |= CS7; 
  74.         break;
  75.     case 8:     
  76.         options.c_cflag |= CS8;
  77.         break;   
  78.     default:    
  79.         fprintf(stderr,"Unsupported data size/n"); return (FALSE);  
  80.     }
  81.     switch (parity) 
  82.     {   
  83.         case 'n':
  84.         case 'N':    
  85.             options.c_cflag &= ~PARENB;   /* Clear parity enable */
  86.             options.c_iflag &= ~INPCK;     /* Enable parity checking */ 
  87.             break;  
  88.         case 'o':   
  89.         case 'O':     
  90.             options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/  
  91.             options.c_iflag |= INPCK;             /* Disnable parity checking */ 
  92.             break;  
  93.         case 'e':  
  94.         case 'E':   
  95.             options.c_cflag |= PARENB;     /* Enable parity */    
  96.             options.c_cflag &= ~PARODD;   /* 转换为偶效验*/     
  97.             options.c_iflag |= INPCK;       /* Disnable parity checking */
  98.             break;
  99.         case 'S'
  100.         case 's':  /*as no parity*/   
  101.             options.c_cflag &= ~PARENB;
  102.             options.c_cflag &= ~CSTOPB;break;  
  103.         default:   
  104.             fprintf(stderr,"Unsupported parity/n");    
  105.             return (FALSE);  
  106.     }  
  107.     /* 设置停止位*/  
  108.     switch (stopbits)
  109.     {   
  110.         case 1:    
  111.             options.c_cflag &= ~CSTOPB;  
  112.             break;  
  113.         case 2:    
  114.             options.c_cflag |= CSTOPB;  
  115.            break;
  116.         default:    
  117.              fprintf(stderr,"Unsupported stop bits/n");  
  118.              return (FALSE); 
  119.     } 
  120.     /* Set input parity option */ 
  121.     if (parity != 'n')   
  122.         options.c_iflag |= INPCK; 
  123.     tcflush(fd,TCIFLUSH);
  124.     options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/   
  125.     options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
  126.     if (tcsetattr(fd,TCSANOW,&options) != 0)   
  127.     { 
  128.         perror("SetupSerial 3");   
  129.         return (FALSE);  
  130.     } 
  131.     return (TRUE);  
  132. }
  133. int main(int argc, char **argv)
  134. {
  135.     
  136.     int fd;
  137.     int nread;
  138.     char buff[512];
  139.     char *dev  = "/dev/ttyS0"//串口二
  140.     fd = OpenDev(dev);
  141.     set_speed(fd,4800);
  142.     if (set_Parity(fd,8,1,'N') == FALSE)  
  143.     {
  144.         printf("Set Parity Error/n");
  145.         exit (0);
  146.     }
  147.     int i;
  148.     i = getchar();
  149.     if ( i == '1')
  150.     {
  151.         while (1) //循环读取数据
  152.         {   
  153.             while((nread = read(fd, buff, 512))>0)
  154.             { 
  155.                 printf("/nLen %d/n",nread); 
  156.                 buff[nread+1] = '/0';   
  157.                 printf( "/n%s", buff);   
  158.             }
  159.         }
  160.     }
  161.     if ( i == '2')
  162.     {
  163.         while (1) //循环写入数据
  164.         {  
  165.             
  166.             gets(buff);
  167.             
  168.             printf("------buff--->%s<--------/n",buff);
  169.             int num = strlen(buff);
  170.             printf("--------num---->%d<--------------/n",num);
  171.             if ( num  > 0)
  172.             {
  173.                 printf("Wirte num not NULL./r/n");
  174.                 nread = write(fd, buff ,num);
  175.                 if(nread == -1)
  176.                 {
  177.                     printf("Wirte sbuf error./n");
  178.                 }
  179.                 printf("--nread---->%d<-----------/n",nread);
  180.             }
  181.             
  182.         }
  183.     }
  184.     close(fd); 
  185.      //exit (0);
  186. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值