C ++ code for a serial port operation

Source: Internet
Author: User

The following is a C ++ code for serial port operations. The running environment of this Code is Linux

# Ifndef _ serialcom_h _ # DEFINE _ serialcom_h_typedef struct _ serialconfig _ {long baud;/* baud rate: for example, 38400,115200 */unsigned char databits;/* Data bit 0-8; 1-7; 2-6 bits; 3-5 bits; */unsigned char parity;/* parity bits: 0-No parity; 1-set to odd verify; 2-convert to an even verification */unsigned char stopbits;/* Stop bit: 0--1 stop bit; 1--2 stop bit */} serialconfig_t; Class serialcom {public: serialcom (); serialcom (serialconfig_t serialconf, int delay, int choice );~ Serialcom (); int serialcomopen (); int serialcomclose (); int checkserialcomdata (); int serialcomread (char * pbuffer, int datalen); int serialcomwrite (char * pbuffer, int datalen); protected: int comfd;/* serial port descriptor */INT Stat; long baud;/* baud rate: for example, 38400,115200 */unsigned char databits;/* Data bit 0-8; 1-7 bits; 2-6 bits; 3-5 bits; */unsigned char parity;/* parity bits: 0-No parity; 1-set to odd verify; 2-convert to an even verification */unsigned char stopbits;/* Stop bit: 0-1 stop bit; 1-2 stop bit */int usec; int port;/*, 2; only 3 choice */
#endif
# Include "serialcom. H "# include <stdio. h> # include <stdlib. h> # include <string. h> # include <fcntl. h> # include <unistd. h> # include <sys/types. h> # include <sys/STAT. h> # include <termios. h> # include <errno. h ># include <iostream> using namespace STD; serialcom: serialcom () {long baud = 19200; unsigned char databits = '8'; unsigned char parity = 'n '; unsigned char stopbits = '1'; USEC = 1; Port = 2; stat = 0; comfd =-1; s TD: cout <baud <"\ t" <databits <"\ t" <parity <"\ t" <stopbits <STD :: endl;} serialcom: serialcom (serialconfig_t serialconf, int delay, int choice) {long baud = serialconf. baud; unsigned char databits = serialconf. databits; unsigned char parity = serialconf. parity; unsigned char stopbits = serialconf. stopbits; USEC = delay; Port = choice; stat = 0; comfd =-1;} serialcom ::~ Serialcom () {} int serialcom: serialcomopen () {char Dev [] [13] = {"/dev/tty0", "/dev/tty1 ", "/dev/ttys0"}; // corresponding port here, select the serial port you want to open if (Port <0) | (Port> 2 )) {printf ("the port is out range"); Return-1;}/* Open Port */comfd = open (Dev [port], o_rdwr | o_noctty | o_ndelay ); if (comfd <0) {comfd = open (Dev [port], o_rdwr | o_noctty | o_ndelay); If (comfd <0) {perror ("open serial port "); return-1 ;}} if (Fcntl (comfd, f_setfl, 0) <0) {perror ("fcntl f_setfl");} If (isatty (comfd) = 0) {perror ("isatty is not a terminal device");} stat = 1; struct termios new_cfg, old_cfg; int speed_arry [] = {b2400, b4800, b9600, b19200, b38400, b115200}; int speed [] = {2400,4800, 9600,19200, 38400,115200}; int I = 0;/* Save and test the serial port */If (tcgetattr (comfd, & old_cfg) <0) {perror ("tcgetattr"); Return-1;} ne W_cfg = old_cfg; cfmakeraw (& new_cfg); new_cfg.c_cflag & = ~ Csize; for (I = sizeof (speed_arry)/sizeof (speed_arry [0]); I> 0; I --) {If (baud = speed [I]) {cfsetispeed (& new_cfg, speed_arry [I]); cfsetospeed (& new_cfg, speed_arry [I]) ;}} switch (databits) {Case '7': new_cfg.c_cflag | = cs7; break; default: Case '8': new_cfg.c_cflag | = cs8; break;} switch (parity) {default: Case 'N': {new_cfg.c_cflag & = ~ Parenb; new_cfg.c_iflag & = ~ Inpck;} break; Case 'O': {new_cfg.c_cflag | = (parodd | parenb); new_cfg.c_iflag | = inpck;} break; Case 'E ': case 'E': {new_cfg.c_cflag | = parenb; new_cfg.c_cflag & = ~ Parodd; new_cfg.c_iflag | = inpck;} break; Case's ': {new_cfg.c_cflag & = ~ Parenb; new_cfg.c_cflag & = ~ Cstopb;} break;} switch (stopbits) {default: Case '1': {new_cfg.c_cflag & = ~ Cstopb;} break; Case '2': {new_cfg.c_cflag | = cstopb;} break;}/* Set wait time */new_cfg.c_cc [vtime] = 0; new_cfg.c_cc [Vmin] = 1; tcflush (comfd, tciflush); If (tcsetattr (comfd, tcsanow, & new_cfg) <0) {perror ("tcsetattr "); return-1;} printf ("open and set serial OK, % d \ n", comfd); Return comfd;} int serialcom: serialcomclose () {close (comfd ); stat = 0; return Stat;}/*> 0 file descriptor readable, = 0 timeout <0 undescriptor readable */INT serialcom: checkserialcomdata () {fd_set rset; struct timevaltv; int ret; fd_zero (& rset); fd_set (comfd, & rset); TV. TV _sec = USEC; TV. TV _usec = 0; ret = select (comfd + 1, & rset, null, null, & TV); If (Ret> 0) printf ("check have data \ n "); return ret;} int serialcom: serialcomread (char * pbuffer, int datalen) {int ret; ret = read (comfd, pbuffer, datalen); return ret;} int serialcom :: serialcomwrite (char * pbuffer, int datalen) {int ret; ret = write (comfd, pbuffer, datalen); If (Ret> 0) printf ("serialcomwrite pbuffer = % s \ n", pbuffer); return ret ;}

}; # Endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.