串口通信,Linux下對wm-02R讀卡器的卡號讀取

 

  1. #ifndef __SERIAL_PORT_H 
  2. #define __SERIAL_PORT_H 
  3.  
  4. #include     <stdio.h>      /*標準輸入輸出定義*/ 
  5. #include     <stdlib.h>     /*標準函數庫定義*/ 
  6. #include     <unistd.h>     /*Unix標準函數定義*/ 
  7. #include     <sys/types.h>  /**/ 
  8. #include     <sys/stat.h>   /**/ 
  9. #include     <fcntl.h>      /*文件控制定義*/ 
  10. #include     <termios.h>    /*PPSIX終端控制定義*/ 
  11. #include     <errno.h>      /*錯誤號定義*/ 
  12.  
  13. #define FALSE -1 
  14. #define TRUE 1 
  15.  
  16.    
  17. /** 
  18. *@brief  設置串口通信速率 
  19. *@param  fd     類型 int  打開串口的文件句柄 
  20. *@param  speed  類型 int  串口速度 
  21. *@return  void 
  22. **/      
  23. void set_speed(int fd, int speed);   
  24.  
  25. /** 
  26. *@brief   設置串口數據位,停止位和效驗位 
  27. *@param  fd     類型  int  打開的串口文件句柄* 
  28. *@param  databits 類型  int 數據位   取值 爲 7 或者8* 
  29. *@param  stopbits 類型  int 停止位   取值爲 1 或者2* 
  30. *@param  parity  類型  char  效驗類型 取值爲N,E,O,,S 
  31. **/  
  32. int set_Parity(int fd,int databits,int stopbits,char parity);    
  33.  
  34. /** 
  35. *@breif 打開串口 
  36. **/ 
  37. int OpenDev(char *Dev); 
  38.  
  39. /** 
  40. *@breif 關閉串口 
  41. **/ 
  42. int CloseDev(int fd); 
  43.  
  44.  
  45. #endif 

 

  1. #include "serial_port.h" 
  2.  
  3. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300}; 
  4. int name_arr[] = {38400, 19200 , 9600 , 4800,  2400,  1200,  300,38400,  19200,  9600, 4800, 2400, 1200,  300 };       
  5.  
  6. int OpenDev(char *Dev) 
  7.     int fd; 
  8.     fd = open(Dev , O_RDWR); 
  9.     if(-1 == fd) 
  10.     { 
  11.         perror("Can't open Serial Port"); 
  12.         return -1; 
  13.     } 
  14.     else 
  15.     { 
  16.         return fd; 
  17.     } 
  18.  
  19. int CloseDev(int fd) 
  20.     close(fd); 
  21.  
  22. void set_speed(int fd, int speed) 
  23.     int i; 
  24.     int status; 
  25.     struct termios opt; 
  26.     tcgetattr(fd , &opt);   /*獲得文件句柄的termios結構*/ 
  27.      
  28.     for(i = 0 ; i< sizeof(speed_arr) / sizeof(int) ; i++) 
  29.     { 
  30.         if(speed == name_arr[i]) 
  31.         { 
  32.             tcflush(fd , TCIOFLUSH); 
  33.             cfsetispeed(&opt , speed_arr[i]);   //設置與串口一致的波特率 
  34.             cfsetospeed(&opt , speed_arr[i]); 
  35.             status = tcsetattr(fd , TCSANOW , &opt); //設置文件句柄的termios結構,TCSANOW爲設置立即生效 
  36.             if(status != 0) 
  37.             { 
  38.                 perror("tcsetattr fd1"); 
  39.                 return ; 
  40.             } 
  41.             tcflush(fd , TCIOFLUSH); 
  42.         } 
  43.          
  44.     } 
  45.      
  46.      
  47.     return ; 
  48.  
  49. int set_Parity(int fd,int databits,int stopbits,char parity) 
  50.     struct termios options; 
  51.     if(tcgetattr(fd , &options) != 0) 
  52.     { 
  53.         perror("Setuo Serial 1"); 
  54.         return FALSE; 
  55.     } 
  56.     options.c_cflag &= ~CSIZE; 
  57.     options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/ 
  58.         options.c_oflag  &= ~OPOST;   /*Output*/ 
  59.      
  60.     switch(databits)  //設置數據位 
  61.     { 
  62.         case 7: 
  63.             options.c_cflag |= CS7; 
  64.             break
  65.         case 8: 
  66.             options.c_cflag |= CS8;     //設置爲8位數據位 
  67.             break
  68.         default
  69.             fprintf(stderr , "Unsupported data size\n"); 
  70.             return FALSE; 
  71.              
  72.     }  
  73.     switch(parity) 
  74.     { 
  75.         case 'n'
  76.         case 'N'
  77.             options.c_cflag &= ~PARENB;     //不設置校驗位使能 
  78.             options.c_iflag &= ~INPCK;  //校驗位檢測 
  79.             break
  80.         case 'o'
  81.         case 'O'
  82.             options.c_cflag |= (PARODD |PARENB);  //設置爲奇校驗 
  83.             options.c_iflag |= INPCK;         //校驗位不檢測 
  84.             break
  85.         case 'e'
  86.         case 'E'
  87.             options.c_cflag |= PARENB;  //設置校驗位使能 
  88.             options.c_cflag &= ~PARODD;     //轉換爲偶校驗 
  89.             options.c_iflag |= INPCK;   //校驗位不檢測 
  90.             break
  91.         case 's'
  92.         case 'S'
  93.             options.c_cflag &= ~PARENB; 
  94.             options.c_cflag &= ~CSTOPB; 
  95.             break
  96.         default
  97.             fprintf(stderr,"Unsupported parity\n"); 
  98.             return FALSE; 
  99.     } 
  100.     /*設置停止位*/ 
  101.     switch(stopbits) 
  102.     { 
  103.         case 1: 
  104.             options.c_cflag &= ~CSTOPB; //一位停止位 
  105.             break
  106.         case 2: 
  107.             options.c_cflag |= CSTOPB;  //兩位停止位 
  108.             break
  109.         default
  110.             fprintf(stderr,"Unsupported stop bits\n"); 
  111.             return FALSE; 
  112.     } 
  113.     if(parity != 'n'
  114.     { 
  115.         options.c_iflag |= INPCK; 
  116.     } 
  117.     tcflush(fd , TCIFLUSH);     //更新termois 
  118.     options.c_cc[VTIME] = 0;    //設置超時 
  119.     options.c_cc[VMIN] = 2; //設置最小發送字節 
  120.      
  121.     if(tcsetattr(fd,TCSANOW,&options) != 0) 
  122.     { 
  123.         perror("SetupSerial 3");     
  124.         return FALSE; 
  125.     } 
  126.     return TRUE; 

 

  1. #include "serial_port.h" 
  2. #include <string.h> 
  3.  
  4. typedef struct info 
  5.     char a; 
  6.     char b; 
  7.     char c; 
  8.     char d; 
  9. }Info; 
  10.  
  11. unsigned int temp = 0; 
  12.  
  13. int getCard(char *dev , int speed) 
  14.     int fd; 
  15.     int nread; 
  16.     int count = 0; 
  17.     char buff[20]; 
  18.     char tempbuff[8]; 
  19.  
  20.      
  21.     if((fd = OpenDev(dev)) > 0) 
  22.     { 
  23.         set_speed(fd,speed); 
  24.     } 
  25.     else 
  26.     { 
  27.         printf("Can't Open Serial Port!\n"); 
  28.         exit(0); 
  29.     } 
  30.     if(set_Parity(fd , 8 , 1 , 'N') == FALSE) 
  31.     { 
  32.         printf("Set Parity Error\n"); 
  33.             exit(1); 
  34.     } 
  35.  
  36.     Info info; 
  37.     while(1) 
  38.     { 
  39.         memset(buff,0,20);  
  40.         while((nread = read(fd , buff , 20)) > 0) 
  41.         { 
  42.             printf("\n %d\n",nread); 
  43.                 buff[nread+1]='\0'
  44.                  
  45.                 info.a = buff[6]; 
  46.                 info.b = buff[5]; 
  47.                 info.c = buff[4]; 
  48.                 info.d = buff[3]; 
  49.                                          
  50.                 memcpy(&temp , &info , 4); 
  51.                      
  52.                 printf("%u\n",temp); 
  53.                      
  54.                 goto loop; 
  55.         } 
  56.             sleep(1000); 
  57.          
  58.     } 
  59.      
  60.      
  61.     loop: 
  62.     CloseDev(fd);    
  63.     return 0; 

 

  1. #include "serial_port.h" 
  2. #include <string.h> 
  3.  
  4.  
  5. int main(int argc , char **argv) 
  6.     char *dev = "/dev/ttyS0"
  7.     getCard(dev,9600); 
  8.  
  9.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章