- #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;
- }