- #ifndef __SERIAL_PORT_H
- #define __SERIAL_PORT_H
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix標準函數定義*/
- #include <sys/types.h> /**/
- #include <sys/stat.h> /**/
- #include <fcntl.h> /*文件控制定義*/
- #include <termios.h> /*PPSIX終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #define FALSE -1
- #define TRUE 1
- /**
- *@brief 設置串口通信速率
- *@param fd 類型 int 打開串口的文件句柄
- *@param speed 類型 int 串口速度
- *@return void
- **/
- void set_speed(int fd, int speed);
- /**
- *@brief 設置串口數據位,停止位和效驗位
- *@param fd 類型 int 打開的串口文件句柄*
- *@param databits 類型 int 數據位 取值 爲 7 或者8*
- *@param stopbits 類型 int 停止位 取值爲 1 或者2*
- *@param parity 類型 char 效驗類型 取值爲N,E,O,,S
- **/
- int set_Parity(int fd,int databits,int stopbits,char parity);
- /**
- *@breif 打開串口
- **/
- int OpenDev(char *Dev);
- /**
- *@breif 關閉串口
- **/
- int CloseDev(int fd);
- #endif
- #include "serial_port.h"
- int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300};
- int name_arr[] = {38400, 19200 , 9600 , 4800, 2400, 1200, 300,38400, 19200, 9600, 4800, 2400, 1200, 300 };
- int OpenDev(char *Dev)
- {
- int fd;
- fd = open(Dev , O_RDWR);
- if(-1 == fd)
- {
- perror("Can't open Serial Port");
- return -1;
- }
- else
- {
- return fd;
- }
- }
- int CloseDev(int fd)
- {
- close(fd);
- }
- void set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios opt;
- tcgetattr(fd , &opt); /*獲得文件句柄的termios結構*/
- for(i = 0 ; i< sizeof(speed_arr) / sizeof(int) ; i++)
- {
- if(speed == name_arr[i])
- {
- tcflush(fd , TCIOFLUSH);
- cfsetispeed(&opt , speed_arr[i]); //設置與串口一致的波特率
- cfsetospeed(&opt , speed_arr[i]);
- status = tcsetattr(fd , TCSANOW , &opt); //設置文件句柄的termios結構,TCSANOW爲設置立即生效
- if(status != 0)
- {
- perror("tcsetattr fd1");
- return ;
- }
- tcflush(fd , TCIOFLUSH);
- }
- }
- return ;
- }
- int set_Parity(int fd,int databits,int stopbits,char parity)
- {
- struct termios options;
- if(tcgetattr(fd , &options) != 0)
- {
- perror("Setuo Serial 1");
- return FALSE;
- }
- options.c_cflag &= ~CSIZE;
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
- options.c_oflag &= ~OPOST; /*Output*/
- switch(databits) //設置數據位
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8; //設置爲8位數據位
- break;
- default:
- fprintf(stderr , "Unsupported data size\n");
- return FALSE;
- }
- switch(parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; //不設置校驗位使能
- options.c_iflag &= ~INPCK; //校驗位檢測
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD |PARENB); //設置爲奇校驗
- options.c_iflag |= INPCK; //校驗位不檢測
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; //設置校驗位使能
- options.c_cflag &= ~PARODD; //轉換爲偶校驗
- options.c_iflag |= INPCK; //校驗位不檢測
- break;
- case 's':
- case 'S':
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return FALSE;
- }
- /*設置停止位*/
- switch(stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB; //一位停止位
- break;
- case 2:
- options.c_cflag |= CSTOPB; //兩位停止位
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return FALSE;
- }
- if(parity != 'n')
- {
- options.c_iflag |= INPCK;
- }
- tcflush(fd , TCIFLUSH); //更新termois
- options.c_cc[VTIME] = 0; //設置超時
- options.c_cc[VMIN] = 2; //設置最小發送字節
- if(tcsetattr(fd,TCSANOW,&options) != 0)
- {
- perror("SetupSerial 3");
- return FALSE;
- }
- return TRUE;
- }
- #include "serial_port.h"
- #include <string.h>
- typedef struct info
- {
- char a;
- char b;
- char c;
- char d;
- }Info;
- unsigned int temp = 0;
- int getCard(char *dev , int speed)
- {
- int fd;
- int nread;
- int count = 0;
- char buff[20];
- char tempbuff[8];
- if((fd = OpenDev(dev)) > 0)
- {
- set_speed(fd,speed);
- }
- else
- {
- printf("Can't Open Serial Port!\n");
- exit(0);
- }
- if(set_Parity(fd , 8 , 1 , 'N') == FALSE)
- {
- printf("Set Parity Error\n");
- exit(1);
- }
- Info info;
- while(1)
- {
- memset(buff,0,20);
- while((nread = read(fd , buff , 20)) > 0)
- {
- printf("\n %d\n",nread);
- buff[nread+1]='\0';
- info.a = buff[6];
- info.b = buff[5];
- info.c = buff[4];
- info.d = buff[3];
- memcpy(&temp , &info , 4);
- printf("%u\n",temp);
- goto loop;
- }
- sleep(1000);
- }
- loop:
- CloseDev(fd);
- return 0;
- }
- #include "serial_port.h"
- #include <string.h>
- int main(int argc , char **argv)
- {
- char *dev = "/dev/ttyS0";
- getCard(dev,9600);
- return 0;
- }