How to realize Serial Communication in C++
This article shows you how to achieve serial communication in C++, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
# include # include int set_serial (int fd,int nSpeed,int nBits,char nEvent,int nStop) {struct termios newttys1,oldttys1; / * Save the original serial configuration * / if (tcgetattr (fd,&oldttys1)! = 0) {perror ("Setupserial 1"); return-1;} bzero (& newttys1,sizeof (newttys1)); newttys1.c_cflag | = (CLOCAL | CREAD) / * CREAD enables serial data receiving, CLOCAL and local connection mode * / newttys1.c_cflag & = ~ CSIZE;/* sets data bit * / / * data bit selection * / switch (nBits) {case 7: newttys1.c_cflag | = CS7; break; case 8: newttys1.c_cflag | = CS8; break } / * set parity bit * / switch (nEvent) {case '0parity: / * parity * / newttys1.c_cflag | = PARENB;/* enable parity * / newttys1.c_iflag | = (INPCK | ISTRIP); / * INPCK opens and inputs parity ISTRIP removes the eighth bit of the character * / newttys1.c_cflag | = PARODD;/* enables parity (the default is parity) * / break; case 'Elevanghanza * parity * / newttys1.c_cflag | = PARENB; / * enable parity * / newttys1.c_iflag | = (INPCK | ISTRIP) / * turn on input parity and remove the eighth bit of characters * / newttys1.c_cflag & = ~ PARODD;/* enable parity * / break; case'Number: / * No parity * / newttys1.c_cflag & = ~ PARENB; break } / * set baud rate * / switch (nSpeed) {case 2400: cfsetispeed (& newttys1, B2400); cfsetospeed (& newttys1, B2400); break; case 4800: cfsetispeed (& newttys1, B4800); cfsetospeed (& newttys1, B4800); break Case 9600: cfsetispeed (& newttys1, B9600); cfsetospeed (& newttys1, B9600); break; case 115200: cfsetispeed (& newttys1, B115200); cfsetospeed (& newttys1, B115200); break; default: cfsetispeed (& newttys1, B9600); cfsetospeed (& newttys1, B9600); break } / * set stop bit * / if (nStop = = 1) / * set stop bit If the stop bit is 1, clear CSTOPB, and if the stop bit is 2, activate CSTOPB*/ {newttys1.c_cflag & = ~ CSTOPB;/* default to one stop bit * /} else if (nStop = = 2) {newttys1.c_cflag | = CSTOPB;/*CSTOPB means to send two stop bits * /} / * to set the minimum character and wait time. When there are no special requirements for receiving characters and waiting time, * / newttys1.c_ cc [VTIME] = 0bot * timeout for reading in irregular mode * / newttys1.c_ cc [VMIN] = 0; / * minimum number of characters read in irregular mode * / tcflush (fd, TCIFLUSH); / * tcflush clears outstanding input / output requests and data from the terminal TCIFLUSH means to clear the data being received and not to read it out * / / * activate the configuration to make it effective * / if ((tcsetattr (fd, TCSANOW,&newttys1))! = 0) {perror ("com set error"); return-1;} return 0;}
SINGAL signal form
# include#include#include#include#include#include#include#include#include#include # define FALSE-1#define TRUE 0#define flag 1#define noflag 0int wait_flag = noflag;int STOP = 0tint 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)) {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) } 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) {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'nails: case'Nables: options.c_cflag & = ~ PARENB; / * Clear parity enable * / options.c_iflag & = ~ INPCK / * Enable parity checking * / break; case'ographies: case'oaks: options.c_cflag | = (PARODD | PARENB); options.c_iflag | = INPCK; / * Disnable parity checking * / break; case'ebacks: case'Estates: options.c_cflag | = PARENB; / * Enable parity * / options.c_cflag & = ~ PARODD; options.c_iflag | = INPCK / * Disnable parity checking * / break; case's servers: case's website: / * 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 stopbits\ 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 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, 9600); if (set_Parity (fd, 8,1,'N') = = FALSE) {printf ("Set Parity Error\ n"); exit (0) } char buf [0] = {0}; while (STOP = = 0) {usleep (100000); / * after receving SIGIO, wait_flag = FALSE,input is availabe and can be read * / if (wait_flag = 0) {memset (buf, 0, sizeof (buf)); res = read (fd, buf, 254) If ((int) buf [0] = 85) {for (int I = 0; I < 254; iTunes +) {std::cout