The source code is as follows:
#include <stdio.h>#include <string.h>#include <sys/types.h>#include <errno.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <termios.h>#include <stdlib.h>intTTY_FD;intMaxintAintb) {returnA>B?A:B;}intSet_opt (intFdintNspeed,intNBits,CharNevent,intNstop) {structTermios Newtio,oldtio;if(Tcgetattr (fd,&oldtio)! =0) {perror ("Setupserial 1");return-1; } bzero (&newtio,sizeof(Newtio)); Newtio.c_cflag |= clocal | Cread; Newtio.c_cflag &= ~csize;Switch(nBits) { Case 7: Newtio.c_cflag |= CS7; Break; Case 8: Newtio.c_cflag |= CS8; Break; }Switch(nevent) { Case ' O '://Odd checkNewtio.c_cflag |= Parenb; Newtio.c_cflag |= parodd; Newtio.c_iflag |= (INPCK | Istrip); Break; Case ' E '://Even checkNewtio.c_iflag |= (INPCK | Istrip); Newtio.c_cflag |= Parenb; Newtio.c_cflag &= ~parodd; Break; Case ' N '://No calibrationNewtio.c_cflag &= ~parenb; Break; }Switch(Nspeed) { Case 2400: Cfsetispeed (&newtio, B2400); Cfsetospeed (&newtio, B2400); Break; Case 4800: Cfsetispeed (&newtio, B4800); Cfsetospeed (&newtio, B4800); Break; Case 9600: Cfsetispeed (&newtio, B9600); Cfsetospeed (&newtio, B9600); Break; Case 115200: Cfsetispeed (&newtio, B115200); Cfsetospeed (&newtio, B115200); Break;default: Cfsetispeed (&newtio, B9600); Cfsetospeed (&newtio, B9600); Break; }if(Nstop = =1) {Newtio.c_cflag &= ~CSTOPB; }Else if(Nstop = =2) {Newtio.c_cflag |= CSTOPB; } Newtio.c_cc[vtime] =0; Newtio.c_cc[vmin] =0; Tcflush (Fd,tciflush);if((Tcsetattr (fd,tcsanow,&newtio))! =0) {perror ("com Set error");return-1; }//printf ("Set done!\n"); return 0;}intOpen_port (intFdintComport) {//char *dev[]={"/DEV/TTYS0", "/dev/ttys1", "/dev/ttys2"}; //long vdisable; if(comport==0) {//printf ("open/dev/ttyusb0");FD = open ("/dev/ttyusb0", o_rdwr| o_noctty| O_ndelay);if(-1= = FD) {perror ("Can ' t Open Serial Port");return(-1); }Else{//printf ("Open ttyUSB0 ... \ n");} }Else if(comport==1) {FD = open ("/DEV/TTYUSB1", o_rdwr| o_noctty| O_ndelay);if(-1= = FD) {perror ("Can ' t Open Serial Port");return(-1); }Else{//printf ("Open ttyUSB1 ... \ n");} }Else if(comport==2) {FD = open ("/DEV/TTYUSB2", o_rdwr| o_noctty| O_ndelay);if(-1= = FD) {perror ("Can ' t Open Serial Port");return(-1); }Else{//printf ("Open ttyUSB2 ... \ n");} }if(Fcntl (FD, F_SETFL,0) <0) {printf("Fcntl failed!\n"); }Else{//printf ("fcntl=%d\n", Fcntl (FD, f_setfl,0));}if(Isatty (stdin_fileno) = =0) {printf("Standard input was not a terminal device\n"); }Else{//printf ("Isatty success!\n");}//printf ("fd-open=%d\n", FD); returnFD;}intMainintargcChar*argv[]) {intMAXFD; Fd_set Rd;intNread,i;Chartmp_buff[8];Charbuff[1024x768];Charsend_buf[1024x768];intOffsetCharrecvbuf[1024x768];structTimeval timeout={0,50000};intTIMEOUT_FLG =0;intRECV_FLG =0;//pthread_t check_ack_thread; //pthread_attr_t check_ack_thread_attr; if(Tty_fd=open_port (TTY_FD,1)) <0) {perror ("Open_port Error");return 0; }if(I=set_opt (TTY_FD,115200,8,' N ',1)) <0) {perror ("Set_opt Error");return 0; }//printf ("fd=%d\n", tty_fd);Fd_zero (&RD);strcpy(send_buf,argv[1]);//printf ("UART >>");send_buf[strlen(SEND_BUF)] =' \ r '; send_buf[strlen(SEND_BUF) +1] =' \ n '; send_buf[strlen(SEND_BUF) +2] =0; Write (TTY_FD, Send_buf,strlen(SEND_BUF));memset(Send_buf,0,sizeof(SEND_BUF)); Fd_set (TTY_FD, &RD); Fd_set (Fileno (stdin), &rd); MAXFD = Max (Fileno (stdin), TTY_FD) +1;if(Select (Maxfd, &rd, Null,null,null) <0) {perror ("Select error\n"); }if(Fd_isset (TTY_FD, &RD)) { while(Nread = read (TTY_FD, Buff,sizeof(buff))) >0) {printf('%s ', buff);memset(Buff,1,sizeof(buff)); }Exit(1); } close (TTY_FD);return 0; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A serial communication program for 3g modules to send a simple at command