Previously based on the IBM Deveplopworks Community Code, do a serial port initialization and send the program, today on this basis to add a reading serial data program. The first is the simplest loop-reading program, the second is through the soft interrupt mode, using the signal signal mechanism to read the serial port, where attention is the hardware interrupt is the device driver level, and read-write serial is user-level behavior, only through the signal mechanism to simulate the interruption, signaling mechanism of the occurrence and processing in fact, the same as hardware interruption, The third is a call through the select system that blocks the process when there is no data, and the serial port has data that needs to be awakened when it is read. Both the second and third examples can be used to read data backstage, which is worth learning.
Code one: looping through data reading
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #define FALSE-1 #define TRUE 0 int speed_arr[] = {B38400, B19200, B9600, B4800, B2400, B1200, b300,b38400, B19200, B9600, B4800, B2400, B1200
};
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 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);
an int set_parity (int fd,int databits,int stopbits,int Parity) {struct options;
if (tcgetattr (fd,&options)!= 0) {perror ("Setupserial 1");
return (FALSE);
} options.c_cflag &= ~csize;
Switch (databits) {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); Options.c_iflag |= INPCK;
/* disnable Parity checking * * break; Case ' E ': Case ' e ': Options.c_cflag |= Parenb;
/* Enable Parity * * Options.c_cflag &= ~parodd; Options.c_iflag |= INPCK; /* disnable Parity checking * * 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);
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; Options.c_cc[vmin] = 0;
/* Update the options and do it now/if (tcsetattr (fd,tcsanow,&options)!= 0) {perror ("Setupserial 3");
return (FALSE);
return (TRUE);
int main () {printf ("This program updates the last time at%s%s\n", __time__,__date__);
printf ("STDIO com1\n");
int FD;
FD = open ("/dev/ttys0", O_RDWR);
if (fd = = 1) {perror ("SerialPort error\n"); } else {PRintf ("open");
printf ("%s", Ttyname (FD));
printf ("succesfully\n");
} set_speed (fd,115200);
if (set_parity (fd,8,1, ' N ') = = FALSE) {printf ("Set Parity error\n");
Exit (0);
} char buf[] = "fe55aa07bc010203040506073d";
Write (fd,&buf,26);
Char buff[512];
int nread;
while (1) {if (nread = Read (FD, buff, >0)) {printf ("\nlen:%d\n", nread);
Buff[nread+1] = ' the ';
printf ("%s", buff);
} close (FD);
return 0; }
Code Listing Two: reading data through the signal mechanism
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <sys/signal.h> #include <fcntl.h> #include <termios.h> #include <errno.h
> #define FALSE-1 #define TRUE 0 #define FLAG 1 #define NOFLAG 0 int wait_flag = noflag;
int STOP = 0;
int res; 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,};
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, TCIOFL
USH);
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);
int set_parity (int fd, int databits, int stopbits, int Parity) {struct options;
if (Tcgetattr (FD, &options)!= 0) {perror ("Setupserial 1");
return (FALSE);
} options.c_cflag &= ~csize;
Switch (databits) {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); Options.c_iflag |= INPCK;
/* disnable Parity checking * * break; Case ' E ': Case ' e ': Options.c_cflag |= Parenb;
/* Enable Parity * * Options.c_cflag &= ~parodd;Options.c_iflag |= INPCK;
/* disnable Parity checking * * 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);
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; Options.c_cc[vmin] = 0; /* Update the options and do it now/if (tcsetattr (FD, Tcsanow, &options)!= 0) {perror ("setupserial
3 ");
return (FALSE);
return (TRUE);
} void Signal_handler_io (int status) {printf ("received Sigio signale.\n");
Wait_flag = Noflag; int main () {printf ("This program updates the last time at%s%s\n", __time__, __date__);
printf ("STDIO com1\n");
int FD;
struct Sigaction Saio;
FD = open ("/dev/ttyusb0", O_RDWR);
if (fd = = 1) {perror ("SerialPort error\n");
else {printf ("open");
printf ("%s", Ttyname (FD));
printf ("succesfully\n");
} Saio.sa_handler = Signal_handler_io;
Sigemptyset (&saio.sa_mask);
saio.sa_flags = 0;
Saio.sa_restorer = NULL;
Sigaction (Sigio, &saio, NULL);
Allow the process to receive Sigio fcntl (FD, F_setown, Getpid ());
Make the file descriptor asynchronous Fcntl (FD, F_SETFL, Fasync);
Set_speed (FD, 115200);
if (Set_parity (FD, 8, 1, ' N ') = = FALSE) {printf ("Set Parity error\n");
Exit (0);
} Char buf[255];
while (STOP = 0) {usleep (100000); /* After receving Sigio, Wait_flag = False,input are availabe and can be read */if (Wait_flag = 0) {memset (bu F, 0, sizeof (BUF));
res = read (FD, buf, 255);
printf ("nread=%d,%s\n", res, BUF); if (res ==1)//STOP = 1; /*stop Loop If only a CR is input */Wait_flag = flag;
/*wait for new input */}} close (FD);
return 0; }
Code three: Through the select system call for IO multi-channel switching, asynchronous read serial data
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <sys/signal.h> #include <fcntl.h> #include <termios.h> #include <errno.h
> #define FALSE-1 #define TRUE 0 #define FLAG 1 #define NOFLAG 0 int wait_flag = noflag;
int STOP = 0;
int res; 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,};
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, TCIOFL
USH);
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);
int set_parity (int fd, int databits, int stopbits, int Parity) {struct options;
if (Tcgetattr (FD, &options)!= 0) {perror ("Setupserial 1");
return (FALSE);
} options.c_cflag &= ~csize;
Switch (databits) {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); Options.c_iflag |= INPCK;
/* disnable Parity checking * * break; Case ' E ': Case ' e ': Options.c_cflag |= Parenb;
/* Enable Parity * * Options.c_cflag &= ~parodd;Options.c_iflag |= INPCK;
/* disnable Parity checking * * 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);
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; Options.c_cc[vmin] = 0; /* Update the options and do it now/if (tcsetattr (FD, Tcsanow, &options)!= 0) {perror ("setupserial
3 ");
return (FALSE);
return (TRUE);
} void Signal_handler_io (int status) {printf ("received Sigio signale.\n");
Wait_flag = Noflag; int main () {printf ("This program updates the last time at%s%s\n", __time__, __date__);
printf ("STDIO com1\n");
int FD;
FD = open ("/dev/ttyusb0", O_RDWR);
if (fd = = 1) {perror ("SerialPort error\n");
else {printf ("open");
printf ("%s", Ttyname (FD));
printf ("succesfully\n");
} set_speed (FD, 115200);
if (Set_parity (FD, 8, 1, ' N ') = = FALSE) {printf ("Set Parity error\n");
Exit (0);
} Char buf[255];
Fd_set Rd;
int nread = 0;
while (1) {Fd_zero (&RD);
Fd_set (FD, &RD);
while (Fd_isset (FD, &rd)) {if (select (Fd+1, &rd, Null,null,null) < 0) {perror ("select error\n");
else {while (nread = Read (fd, buf, sizeof (BUF)) > 0) {printf ("nread =%d,%s\n", nread, BUF);
printf ("test\n");
memset (buf, 0, sizeof (BUF));
Close (FD)}}};
return 0; }