1,串口操作
串口通信最主要的是通信参数的设置
C语言要设置串口的各个参数需要用到串口的操作API 以及串口的数据结构
#include "uart.h" #define FALSE -1 #define TRUE 0 int uart_set(int fd,int bautrate,int flow_ctl, int databites,int stopbits, int parity) { int i = 0; int baut_rate[]={B115200,B19200,B9600,B4800,B2400,B1200,B300}; int baut_name[]={115200,19200,9600,4800,2400,1200,300}; struct termios options; //get parament of fd,and save in options. if(tcgetattr(fd,&options)!=0) { return FALSE; } //set the baut rate for(i=0;i<sizeof(baut_rate)/sizeof(int);i++) { if(bautrate==baut_name[i]) { cfsetispeed(&options,baut_rate[i]); cfsetospeed(&options,baut_rate[i]); } } options.c_cflag |= CLOCAL; options.c_cflag |= CREAD; switch(flow_ctl) { case 0: options.c_cflag &= ~CRTSCTS; break; case 1: options.c_cflag |= CRTSCTS; break; case 2: options.c_cflag |= IXON | IXOFF | IXANY; break; } options.c_cflag &= ~CSIZE; switch(databites) { case 5: options.c_cflag |= CS5; break; case 6: options.c_cflag |= CS6; break; case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS6; break; } 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; } switch(stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; } options.c_oflag &= ~OPOST; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_cc[VTIME]=1; options.c_cc[VMIN]=1; tcflush(fd,TCIFLUSH); if(tcsetattr(fd,TCSANOW,&options)!=0) { return FALSE; } return TRUE; } int uart_init(int fd) { if (uart_set(fd,9600,0,8,1,'N') == FALSE) { return FALSE; } return TRUE; } int uart_open(char *port) { int fd=0; fd=open(port,O_RDWR|O_NOCTTY); if(fd==FALSE) { printf("Open serial port failed\n"); return FALSE; } if(fcntl(fd,F_SETFL,0)<0) { printf("fcntl failed\n"); return FALSE; } if(isatty(STDIN_FILENO)==0) { printf("Not a standard \n"); return FALSE; } return fd; } void uart_close(int fd) { close(fd); } int UART0_Recv(int fd, char *rcv_buf,int data_len) { int len,fs_sel; fd_set fs_read; struct timeval time; FD_ZERO(&fs_read); FD_SET(fd,&fs_read); time.tv_sec = 10; time.tv_usec = 0; //使用select实现串口的多路通信 fs_sel = select(fd+1,&fs_read,NULL,NULL,&time); if(fs_sel) { len = read(fd,rcv_buf,data_len); printf("I am right!(version1.2) len = %d fs_sel = %d\n",len,fs_sel); return len; } else { printf("Sorry,I am wrong!"); return FALSE; } } int UART0_Send(int fd, char *send_buf,int data_len) { int len = 0; len = write(fd,send_buf,data_len); if (len == data_len ) { return len; } else { tcflush(fd,TCOFLUSH); return FALSE; } }
2,头文件
#ifndef __UART_H_ #define __UART_H_ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <string.h> extern int uart_set(int fd,int bautrate,int flow_ctl, int databites,int stopbits, int parity); extern int uart_init(int fd); extern int uart_open(char *); extern void uart_close(int ); extern void uart_send(); extern void uart_recv(); #endif