In the program, it is easy to configure the properties of the serial port, which are defined in the structure struct Termios the.
Detailed introduction of Termios, you can check the information separately, or reference: detailed Linux serial communication development: http://blog.itpub.net/24790158/viewspace-1041147/
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> #include <string.h> int set_opt (int fd,int nspeed, int nBits, char nevent, int nstop) {struct Termios newtio,oldtio; if (Tcgetattr (fd,& Oldtio)! = 0) {perror ("Setupserial 1"); return-1; } bzero (&newtio, sizeof (Newtio)); Newtio.c_cflag |= clocal | Cread; Clocal: Ignore modem control line cread: Open the recipient Newtio.c_cflag &= ~csize; The character length mask. The value is: CS5,CS6,CS7 or CS8 switch (nBits) {case 7:newtio.c_cflag |= CS7; Break Case 8:newtio.c_cflag |= CS8; Break } switch (nevent) {case ' O ': Newtio.c_cflag |= parenb;//allow output to generate parity information and input to parity newtio.c_cflag |=; Input and output are odd and verified Newtio.c_iflag |= (INPCK | Istrip); Inpack: Enable input parity detection; Istrip: Remove the eighth bit break; Case ' E ': Newtio.c_iflag |= (INPCK | Istrip); Newtio.c_cflag |= Parenb; Newtio.c_cflag &= ~parodd; Break Case ' N ': newtio.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 Case 460800:cfsetispeed (&newtio, B460800); Cfsetospeed (&newtio, B460800); Break Default:cfsetispeed (&newtio, B9600); Cfsetospeed (&newtio, B9600); Break } if (nstop = = 1) newtio.c_cflag &= ~CSTOPB; CSTOPB: Set two stop bits instead of an else if (nstop = = 2) newtio.c_cflag |= CSTOPB; Newtio.c_cc[vtime] = 0; Vtime: Delay in non-cannoical mode reading, with ONE-TENTH second bit unit newtio.c_cc[vmin] = 0; VMIN: Non-canonical mode reads the minimum number of characters tcflush (Fd,tciflush); Changing the output of all objects written to the FD reference takes effect and all accepted but unread inputs are discarded before the change occurs. if ((Tcsetattr (fd,tcsanow,&newtio))!=0)//tcsanow: Change occurs immediately {perror ("com set error"); return-1; } prinTF ("set done!\n\r"); return 0;} int main (void) {int fd1,nset,nread,ret; char buf[100]={"test com data!........... \ n"}; char buf1[100]; fd1 = open ("/dev/t TySAC0 ", O_RDWR); if (fd1 = =-1) exit (1); printf ("Open SAC0 success!! \ n "); Nset = set_opt (FD1, 9600, 8, ' N ', 1); if (Nset = =-1) exit (1); printf ("SET SAC0 success!! \ n "); printf ("Enter the loop!! \ n "); while (1) {memset (buf1, 0, sizeof (BUF1)); ret = Write (FD1, buf, 100); if (Ret > 0) {printf ("Write success! Wait Data receive\n "); } nread = Read (FD1, BUF1, 100); if (Nread > 0) {printf ("Redatad:nread =%s\n\n\r", buf1); } sleep (1); Nread = Read (FD1, buf1,1); if (buf1[0] = = ' Q ')//break; } close (FD1); return 0;}
With ARM-LINUX-GCC cross-compile and run on the Development Board, the 9-port serial 2 3 short-connect:
/#./com Open SAC0 success!! Set done! SET SAC0 success!! Enter the loop!! Write success! Wait Data receivewrite success! Wait Data receivewrite success! Wait Data receivewrite success! Wait Data receivewrite success! Wait Data receivewrite success! Wait data Receiveredatad:nread = Test COM data!........... write success! Wait data Receiveredatad:nread = Test COM data!........... write success! Wait data Receiveredatad:nread = Test COM Data!...........
Linux TTY Serial test program