Example Use Case
The following demo program including the complete process of opening a UART device,
listening on it, and printing when readable data is detected.
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /*File control definition*/ #include <termios.h> /*PPSIX Terminal operating system definition*/ #include <stdio.h> /*Standard input and output definition*/ #include <unistd.h> /*UNIX standard function definition*/ #define BAUDRATE 115200 #define UART_DEVICE "/dev/ttyS3" #define FALSE -1 #define TRUE 0 /** *@brief Set communication speed for UART *@param fd Type int Open UART file *@param speed Type int UART speed *@return void */ int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, }; int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, 115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, }; void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); 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); if (status != 0) { perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); } } } /** *@brief Set data bit, stop bit and parity check bit *@param fd Type int OPEN UART file *@param databits Type int data bit Value 7 or 8 *@param stopbits Type int stop bit Value 1 or 2 *@param parity Type int parity check Type Value N,E,O,,S */ int set_Parity(int fd,int databits,int stopbits,int parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); } options.c_cflag &= ~CSIZE; switch (databits) /*Set dataa bit count*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return (FALSE); } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* Set as odd parity check*/ options.c_iflag |= INPCK; /* Disable parity check */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* Set as even parity check*/ options.c_iflag |= INPCK; /* Disable parity check */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: fprintf(stderr,"Unsupported parity\n"); return (FALSE); } /* Set stop bit*/ 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); } /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* Set to more than 15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return (FALSE); } options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/ options.c_oflag &= ~OPOST; /*Output*/ return (TRUE); } int main(int argc, char *argv[]) { int fd, c=0, res; char *dev; char buf[256]; printf("Start...\n"); if (argc == 2) dev = argv[1]; else dev = UART_DEVICE; fd = open(dev, O_RDWR); if (fd < 0) { perror(UART_DEVICE); exit(1); } printf("Open...\n"); printf("bandrate %d...\n",BAUDRATE); set_speed(fd,BAUDRATE); if (set_Parity(fd,8,1,'N') == FALSE) { printf("Set Parity Error\n"); exit (0); } printf("Reading...\n"); while(1) { res = read(fd, buf, 255); if(res==0) continue; buf[res]=0; printf("%s", buf); if (buf[0] == 0x0d) printf("\n"); write(fd,buf,res); if (buf[0] == '@') break; } printf("Close...\n"); close(fd); return 0; }