Linux下实现应用层串口库函数

来源:互联网 发布:落叶能知易水寒下句 编辑:程序博客网 时间:2024/05/18 22:11

//serialport.h

[cpp] view plaincopy
  1. /*************************************************************     
  2.     FileName : serialport.h 
  3.     FileFunc : 定义头文件    
  4.     Version  : V0.1     
  5.     Author   : Sunrier     
  6.     Date     : 2012-06-13 
  7.     Descp    : Linux下实现串口库     
  8. *************************************************************/  
  9. #ifndef   _SERIALPORT_H_      
  10. #define   _SERIALPORT_H_   
  11.   
  12. #ifdef __cplusplus  
  13. extern "C" {  
  14. #endif  
  15.   
  16. int open_port(int iPortNumber);  
  17. int set_port(int fd,int iBaudRate,int iDataSize,char cParity,int iStopBit);  
  18. int read_port(int fd,void *buf,int iByte);  
  19. int write_port(int fd,void *buf,int iByte);  
  20. int close_port(int fd);  
  21.   
  22. #ifdef __cplusplus  
  23. }  
  24. #endif  
  25.   
  26. #endif   


 

 

//serialport.c

[cpp] view plaincopy
  1. /*************************************************************      
  2.     FileName : serialport.c  
  3.     FileFunc : 定义实现文件    
  4.     Version  : V0.1      
  5.     Author   : Sunrier      
  6.     Date     : 2012-06-13 
  7.     Descp    : Linux下实现串口库    
  8. *************************************************************/   
  9. /*#include "serialport.h"*/  
  10. #include <stdio.h>  
  11. #include <strings.h>  
  12. #include <unistd.h>  
  13. #include <fcntl.h>  
  14. #include <termios.h>  
  15.   
  16. int open_port(int iPortNumber)  
  17. {  
  18.     int fd = -1;  
  19.     char *pDev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3",  
  20.                                 "/dev/ttyS4","/dev/ttyS5","/dev/ttyS6","/dev/ttyS7",  
  21.                                 "/dev/ttyS8","/dev/ttyS9","/dev/ttyS10","/dev/ttyS11"};  
  22.       
  23.     switch( iPortNumber )  
  24.     {  
  25.         case    1:  
  26.         case    2:  
  27.         case    3:    
  28.         case    4:    
  29.         case    5:    
  30.         case  6:      
  31.         case  7:  
  32.         case  8:  
  33.         case  9:  
  34.         case  10:  
  35.         case  11:     
  36.         case  12:                         
  37.                         fd = open(pDev[iPortNumber-1],O_RDWR|O_NOCTTY|O_NDELAY);  
  38.                         if( fd<0 )  
  39.                         {  
  40.                             perror("Can't Open Serial Port !");  
  41.                             return (-1);  
  42.                         }  
  43.                         else  
  44.                         {  
  45.                             printf("Open ttyS%d ......\n",iPortNumber-1);  
  46.                         }  
  47.                         break;  
  48.         default:  
  49.                         /*perror("Don't exist iPortNumber !");*/  
  50.                         printf("Don't exist iPortNumber%d under /dev/? !\n",iPortNumber);  
  51.                         return (-1);                                                      
  52.     }  
  53.       
  54.     if( fcntl(fd,F_SETFL,0)<0 )/*恢复串口的状态为阻塞状态,用于等待串口数据的读入*/  
  55.     {  
  56.         printf("fcntl failed !\n");  
  57.         return (-1);  
  58.     }  
  59.     else  
  60.     {  
  61.         printf("fcntl = %d !\n",fcntl(fd,F_SETFL,0));  
  62.     }  
  63.       
  64.     /*测试打开的文件描述符是否应用一个终端设备,以进一步确认串口是否正确打开*/  
  65.     if( !isatty(STDIN_FILENO) )  
  66.     {  
  67.         printf("Standard input isn't a terminal device !\n");  
  68.         return (-1);  
  69.     }  
  70.     else  
  71.     {  
  72.         printf("It's a serial terminal device!\n");  
  73.     }  
  74.       
  75.     printf("open_port file ID= %d !\n",fd);  
  76.       
  77.     return fd;  
  78.       
  79. }  
  80.   
  81. int set_port(int fd,int iBaudRate,int iDataSize,char cParity,int iStopBit)  
  82. {  
  83.     int iResult = 0;  
  84.     struct termios oldtio,newtio;  
  85.       
  86.       
  87.     iResult = tcgetattr(fd,&oldtio);/*保存原先串口配置*/  
  88.     if( iResult )  
  89.     {  
  90.         perror("Can't get old terminal description !");  
  91.         return (-1);  
  92.     }  
  93.       
  94.       
  95.     bzero(&newtio,sizeof(newtio));  
  96.     newtio.c_cflag |= CLOCAL | CREAD;/*设置本地连接和接收使用*/  
  97.       
  98.     /*设置输入输出波特率*/  
  99.     switch( iBaudRate )  
  100.     {  
  101.         case 2400:  
  102.                             cfsetispeed(&newtio,B2400);  
  103.                             cfsetospeed(&newtio,B2400);  
  104.                             break;  
  105.         case 4800:  
  106.                             cfsetispeed(&newtio,B4800);  
  107.                             cfsetospeed(&newtio,B4800);  
  108.                             break;    
  109.         case 9600:  
  110.                             cfsetispeed(&newtio,B9600);  
  111.                             cfsetospeed(&newtio,B9600);  
  112.                             break;    
  113.         case 19200:  
  114.                             cfsetispeed(&newtio,B19200);  
  115.                             cfsetospeed(&newtio,B19200);  
  116.                             break;  
  117.         case 38400:  
  118.                             cfsetispeed(&newtio,B38400);  
  119.                             cfsetospeed(&newtio,B38400);  
  120.                             break;    
  121.         case 57600:  
  122.                             cfsetispeed(&newtio,B57600);  
  123.                             cfsetospeed(&newtio,B57600);  
  124.                             break;                                                        
  125.         case 115200:  
  126.                             cfsetispeed(&newtio,B115200);  
  127.                             cfsetospeed(&newtio,B115200);  
  128.                             break;    
  129.         case 460800:  
  130.                             cfsetispeed(&newtio,B460800);  
  131.                             cfsetospeed(&newtio,B460800);  
  132.                             break;                        
  133.         default     :  
  134.                             /*perror("Don't exist iBaudRate !");*/  
  135.                             printf("Don't exist iBaudRate %d !\n",iBaudRate);  
  136.                             return (-1);                                                                      
  137.     }  
  138.       
  139.     /*设置数据位*/  
  140.     newtio.c_cflag &= (~CSIZE);  
  141.     switch( iDataSize )  
  142.     {  
  143.         case    7:  
  144.                         newtio.c_cflag |= CS7;  
  145.                         break;  
  146.         case    8:  
  147.                         newtio.c_cflag |= CS8;  
  148.                         break;  
  149.         default:  
  150.                         /*perror("Don't exist iDataSize !");*/  
  151.                         printf("Don't exist iDataSize %d !\n",iDataSize);  
  152.                         return (-1);                                  
  153.     }  
  154.       
  155.     /*设置校验位*/  
  156.     switch( cParity )  
  157.     {  
  158.         case    'N':                    /*无校验*/  
  159.                             newtio.c_cflag &= (~PARENB);  
  160.                             break;  
  161.         case    'O':                    /*奇校验*/  
  162.                             newtio.c_cflag |= PARENB;  
  163.                             newtio.c_cflag |= PARODD;  
  164.                             newtio.c_iflag |= (INPCK | ISTRIP);  
  165.                             break;  
  166.         case    'E':                    /*偶校验*/  
  167.                             newtio.c_cflag |= PARENB;  
  168.                             newtio.c_cflag &= (~PARODD);  
  169.                             newtio.c_iflag |= (INPCK | ISTRIP);  
  170.                             break;                
  171.         default:  
  172.                             /*perror("Don't exist cParity  !");*/  
  173.                             printf("Don't exist cParity %c !\n",cParity);  
  174.                             return (-1);                                  
  175.     }  
  176.       
  177.     /*设置停止位*/  
  178.     switch( iStopBit )  
  179.     {  
  180.         case    1:  
  181.                         newtio.c_cflag &= (~CSTOPB);  
  182.                         break;  
  183.         case    2:  
  184.                         newtio.c_cflag |= CSTOPB;  
  185.                         break;  
  186.         default:  
  187.                         /*perror("Don't exist iStopBit !");*/  
  188.                         printf("Don't exist iStopBit %d !\n",iStopBit);  
  189.                         return (-1);                                  
  190.     }  
  191.       
  192.     newtio.c_cc[VTIME] = 0; /*设置等待时间*/  
  193.     newtio.c_cc[VMIN] = 0;  /*设置最小字符*/  
  194.     tcflush(fd,TCIFLUSH);       /*刷新输入队列(TCIOFLUSH为刷新输入输出队列)*/  
  195.     iResult = tcsetattr(fd,TCSANOW,&newtio);    /*激活新的设置使之生效,参数TCSANOW表示更改立即发生*/  
  196.   
  197.     if( iResult )  
  198.     {  
  199.         perror("Set new terminal description error !");  
  200.         return (-1);  
  201.     }     
  202.       
  203.     printf("set_port success !\n");  
  204.       
  205.     return 0;  
  206. }  
  207.   
  208. int read_port(int fd,void *buf,int iByte)  
  209. {  
  210.     int iLen = 0;  
  211.     if( !iByte )  
  212.     {  
  213.         printf("Read byte number error !\n");  
  214.         return iLen;  
  215.     }  
  216.       
  217.     iLen = read(fd,buf,iByte);  
  218.       
  219.     return iLen;  
  220. }  
  221.   
  222. int write_port(int fd,void *buf,int iByte)  
  223. {  
  224.     int iLen = 0;  
  225.     if( !iByte )  
  226.     {  
  227.         printf("Write byte number error !\n");  
  228.         return iLen;  
  229.     }  
  230.       
  231.     iLen = write(fd,buf,iByte);  
  232.       
  233.     return iLen;  
  234. }  
  235.   
  236.   
  237. int close_port(int fd)  
  238. {  
  239.     int iResult = -1;  
  240.       
  241.     iResult = close(fd);  
  242.       
  243.     return iResult;  
  244. }  


 

 

//demo.c

[cpp] view plaincopy
  1. /*************************************************************     
  2.     FileName : demo.c 
  3.     FileFunc : 测试串口应用    
  4.     Version  : V0.1     
  5.     Author   : Sunrier     
  6.     Date     : 2012-06-13 
  7.     Descp    : Linux下实现串口库     
  8. *************************************************************/  
  9. #include <stdio.h>  
  10. #include <string.h>  
  11. #include "serialport.h"  
  12.   
  13. int main(int argc,char *argv[])  
  14. {  
  15.     int iResult = -1;     
  16.     int fd = -1,iCommPort,iBaudRate,iDataSize,iStopBit;  
  17.     char cParity;  
  18.     int iLen;  
  19.     char szBuffer[30];  
  20.       
  21.     iCommPort = 1;  
  22.     fd = open_port(iCommPort);  
  23.     if( fd<0 )  
  24.     {  
  25.         perror("open_port error !");  
  26.         return 1;  
  27.     }  
  28.       
  29.     iBaudRate = 115200;  
  30.     iDataSize = 8;  
  31.     cParity = 'N';  
  32.     iStopBit = 1;  
  33.     iResult = set_port(fd,iBaudRate,iDataSize,cParity,iStopBit);      
  34.     if( iResult<0 )  
  35.     {  
  36.         perror("set_port error !");  
  37.         return 1;  
  38.     }     
  39.       
  40.     printf("fd = %d \n",fd);  
  41.       
  42.     //memset(szBuffer,0,sizeof(szBuffer));  
  43.     //iLen = read_port(fd,szBuffer,5);  
  44.     iLen = write_port(fd,"Hello",5);  
  45.     memset(szBuffer,0,sizeof(szBuffer));  
  46.     iLen = read_port(fd,szBuffer,5);  
  47.     if( iLen>0 )  
  48.         //printf("Write byte success !\n");  
  49.     //szBuffer[iLen+1] = '\0';  
  50.     printf("iLen =  %d ,szBuffer = %s \n",iLen,szBuffer);  
  51.       
  52.     return 0;  
  53. }  
0 0
原创粉丝点击