c语言 串口通讯

本文主要内容包含:

 1.接收串口数据程序的编程逻辑示意图;

 2.接收串口数据程序要用到的通用函数模块(可直接引用,无需更改);

 3.接收串口数据程序的示例。


1.接收串口数据程序的编程逻辑示意图:

2.与串口有关的函数模块及数组(可直接引用到自己的程序中):

[cpp]  view plain  copy
  1. <span style="font-size:18px;">//设置波特率函数会用到的数组  
  2. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,  
  3.         B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
  4. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,  
  5.         38400,  19200,  9600, 4800, 2400, 1200,  300, };  
  6.   
[cpp]  view plain  copy
  1. <span style="font-size:18px;">int OpenDev(char *Dev) //打开串口  
  2. {  
  3.     int fd = open(Dev,O_RDWR | O_NOCTTY | O_NONBLOCK);  
  4.     if(-1 == fd)  
  5.     {  
  6.         perror("Can't Open Serial Port");  
  7.         return -1;  
  8.     }  
  9.     else  
  10.     {  
  11.         printf("Open com success!!!!!!!!!!!");  
  12.         return fd;  
  13.     }  
  14. }  
  15.   

[cpp]  view plain  copy
  1. <span style="font-size:18px;">void set_speed(int fd, int speed)  //设置波特率  
  2. {  
  3.   int   i;  
  4.   int   status;  
  5.   struct termios   Opt;  
  6.   tcgetattr(fd, &Opt);  
  7.   for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)  
  8.    {  
  9.     if  (speed == name_arr[i])  
  10.     {  
  11.         tcflush(fd, TCIOFLUSH);  
  12.         cfsetispeed(&Opt, speed_arr[i]);  
  13.         cfsetospeed(&Opt, speed_arr[i]);  
  14.         status = tcsetattr(fd, TCSANOW, &Opt);  
  15.         if  (status != 0)  
  16.             perror("tcsetattr fd1");  
  17.         return;  
  18.         }  
  19.    tcflush(fd,TCIOFLUSH);  
  20.    }  
  21. }  
  22.   

[cpp]  view plain  copy
  1. <span style="font-size:18px;">int set_Parity(int fd,int databits,int stopbits,int parity)  //设置数据位、奇偶位、停止位等  
  2. {  
  3.    struct termios options;  
  4.  if  ( tcgetattr( fd,&options)  !=  0)  
  5.   {  
  6.     perror("SetupSerial 1");  
  7.     return(0);  
  8.   }  
  9.   bzero(&options,sizeof(options));  
  10.   options.c_cflag |= CLOCAL | CREAD;  
  11.   options.c_cflag &= ~CSIZE;  
  12.   switch (databits) /*设置数据位*/  
  13.   {  
  14.     case 7:  
  15.         options.c_cflag |= CS7;  
  16.         break;  
  17.     case 8:  
  18.         options.c_cflag |= CS8;  
  19.         break;  
  20.     default:  
  21.         fprintf(stderr,"Unsupported data size\n");  
  22.         return (0);  
  23.     }  
  24.   switch (parity)/*设置校验位*/  
  25.     {  
  26.     case 'n':  
  27.     case 'N':  
  28.         options.c_cflag &= ~PARENB;    
  29.         //options.c_iflag &= ~INPCK;      
  30.         break;  
  31.     case 'o':  
  32.     case 'O':  
  33.         options.c_cflag |= (PARODD | PARENB);    
  34.         options.c_iflag |= (INPCK | ISTRIP);              
  35.         break;  
  36.     case 'e':  
  37.     case 'E':  
  38.         options.c_cflag |= PARENB;      
  39.         options.c_cflag &= ~PARODD;    
  40.         options.c_iflag |= (INPCK | ISTRIP);         
  41.         break;  
  42.     case 'S':  
  43.     case 's':    
  44.         options.c_cflag &= ~PARENB;  
  45.         options.c_cflag &= ~CSTOPB;  
  46.         break;  
  47.     default:  
  48.         fprintf(stderr,"Unsupported parity\n");  
  49.         return (0);  
  50.         }  
  51.   switch (stopbits)/*设置停止位*/  
  52.     {  
  53.     case 1:  
  54.         options.c_cflag &= ~CSTOPB;  
  55.         break;  
  56.     case 2:  
  57.         options.c_cflag |= CSTOPB;  
  58.         break;  
  59.     default:  
  60.         fprintf(stderr,"Unsupported stop bits\n");  
  61.         return (FALSE);  
  62.     }  
  63.   if (parity != 'n')  
  64.     options.c_iflag |= INPCK;  
  65.     options.c_cc[VTIME] = 0;   
  66.     options.c_cc[VMIN] = 0;  
  67.   tcflush(fd,TCIFLUSH);   
  68.   if (tcsetattr(fd,TCSANOW,&options) != 0)  
  69.     {  
  70.         perror("SetupSerial 3");  
  71.         return (0);  
  72.     }  
  73.   return (1);  
  74.  }  
  75.   

3.示例:
[cpp]  view plain  copy
  1. <span style="font-size:18px;">#include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10.   
  11. #define TRUE 1  
  12. #define FALSE 0  
  13.   
  14. int analysis(char *buff);  
  15. int OpenDev(char *Dev);  
  16. void set_speed(int fd, int speed);  
  17. int set_Parity(int fd,int databits,int stopbits,int parity);  
  18.   
  19. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
  20. int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };  
  21. int OpenDev(char *Dev)  
  22. {  
  23.   int fd = open(Dev,O_RDWR | O_NOCTTY | O_NONBLOCK);  
  24.   if(-1 == fd)  
  25.     {  
  26.       perror("Can't Open Serial Port");  
  27.       return -1;  
  28.     }   
  29.   else   
  30.     {  
  31.       printf("Open com success!!!!!!!!!!!");  
  32.       return fd;  
  33.     }  
  34. }   
  35. void set_speed(int fd, int speed)  
  36. {   
  37.   int i;   
  38.   int status;   
  39.   struct termios Opt;  
  40.  tcgetattr(fd, &Opt);   
  41.   for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)  
  42.    {   
  43.       if (speed == name_arr[i])   
  44.         {   
  45.           tcflush(fd, TCIOFLUSH);   
  46.           cfsetispeed(&Opt, speed_arr[i]);  
  47.          cfsetospeed(&Opt, speed_arr[i]);  
  48.          status = tcsetattr(fd, TCSANOW, &Opt);   
  49.           if (status != 0) perror("tcsetattr fd1");  
  50.          return;  
  51.        }   
  52.     tcflush(fd,TCIOFLUSH);  
  53.     }  
  54. }  
  55. int set_Parity(int fd,int databits,int stopbits,int parity)   
  56. {   
  57.   struct termios options;   
  58.   if ( tcgetattr( fd,&options) != 0)   
  59.   {  
  60.    perror("SetupSerial 1");  
  61.    return(FALSE);  
  62.  }   
  63.   bzero(&options,sizeof(options));   
  64.   options.c_cflag |= CLOCAL | CREAD;  
  65.  options.c_cflag &= ~CSIZE;   
  66.   switch (databits)   
  67.   {   
  68.     case 7:   
  69.     options.c_cflag |= CS7;  
  70.    break;  
  71.    case 8:  
  72.    options.c_cflag |= CS8;  
  73.    break;   
  74.     default: fprintf(stderr,"Unsupported data size\n");  
  75.    return (FALSE);   
  76.   }   
  77.   switch (parity)   
  78.   {  
  79.    case 'n':   
  80.     case 'N':  
  81.    options.c_cflag &= ~PARENB;  
  82.    options.c_iflag &= ~INPCK;   
  83.     break;   
  84.     case 'o':  
  85.    case 'O':   
  86.     options.c_cflag |= (PARODD | PARENB);  
  87.    options.c_iflag |= (INPCK | ISTRIP);   
  88.     break;   
  89.     case 'e':   
  90.     case 'E':   
  91.     options.c_cflag |= PARENB;  
  92.    options.c_cflag &= ~PARODD;   
  93.     options.c_iflag |= (INPCK | ISTRIP);   
  94.     break;   
  95.     case 'S':   
  96.     case 's':   
  97.     options.c_cflag &= ~PARENB;   
  98.     options.c_cflag &= ~CSTOPB;  
  99.    break;  
  100.    default: fprintf(stderr,"Unsupported parity\n");   
  101.    return (FALSE);   
  102.   }   
  103.   switch (stopbits)  
  104.  {   
  105.     case 1:  
  106.    options.c_cflag &= ~CSTOPB;   
  107.     break;   
  108.     case 2:   
  109.     options.c_cflag |= CSTOPB;  
  110.    break;   
  111.     default: fprintf(stderr,"Unsupported stop bits\n");   
  112.     return (FALSE);   
  113.     }   
  114.     if (parity != 'n')   
  115.     options.c_iflag |= INPCK;   
  116.     options.c_cc[VTIME] = 0;  
  117.    options.c_cc[VMIN] = 0;  
  118.    tcflush(fd,TCIFLUSH);   
  119.     if (tcsetattr(fd,TCSANOW,&options) != 0)  
  120.    {    
  121.         perror("SetupSerial 3");   
  122.         return (FALSE);  
  123.    }   
  124.     return (TRUE);  
  125. }  
  126.   
  127. int analysis (char *buff)  
  128. {  
  129.   int i;  
  130.   char *p;  
  131.   p=buff;  
  132.   for(i=0;i<255,i++)  
  133.     {  
  134.       printf("%s ",p[i]);  
  135.     }  
  136.   return 0;  
  137. }  
  138.   
  139. void main(void)  
  140. {  
  141.   int fd;  
  142.   int nread;  
  143.   char buff[255];  
  144.   char *dev_name = "/dev/ttymxc4";//根据实际情况选择串口  
  145.   while(1)   
  146.     {    
  147.       fd = OpenDev(dev_name); //打开串口   
  148.   
  149.       if(fd>0)   
  150.       set_speed(fd,9600); //设置波特率   
  151.       else   
  152.       {   
  153.          printf("Can't Open Serial Port!\n");   
  154.          sleep(1);  
  155.         continue;   
  156.       }   
  157.   break;  
  158. }  
  159.   
  160. if(set_Parity(fd,8,1,'N')==FALSE) //设置校验位   
  161. {  
  162.   printf("Set Parity Error\n");   
  163.   exit(1);  
  164. }  
  165.   
  166. while(1)   
  167.   {   
  168.     sleep(3);   
  169.     nread = read(fd,buff,sizeof(buff));  
  170.     if((nread>0))  
  171.       {       
  172.     printf("Success!\n");   
  173.        }  
  174.     analysis(buff);  
  175.   }  
  176. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值