I wrote a very early serial communication code. However, today is also used to do a simple class encapsulation.
Code, such as the following:
Rs485Test.h
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <termios.h> #include <fcntl.h> #include <errno.h> #include <string.h> Class RS485 {public:///////////////////////////////////////////////////////////////to create a serial port connection//@param name Device name///@ Param speed Baud rate///@param parity parity///@param databits Data bit///@param stopbits stop bit////////////////////////////////////// RS485 (char *name,int speed,int parity,int databits,int stopbits); ~rs485 ();/////////////////// Send serial message//@param FD message struct///@param BUF message structure///@param length message structure////@ret Urn-1: Failure, 0: Success////////////////////////////////////////////////////////////int sendmsg (unsigned char * buf, int length );///////////////////////////////////////////////////////////////receive serial message//@param FD message struct///@param msg message struct///@ Param length Message structure///@return-1: Failure, 0: Success/////////////////////////int recvmsg (unsigned char * msg, int length);p rivate://////////////////////////// Open the serial device///@param dev Serial device name///@return-1: Failed. 0: Success////////////////////////////////////////////////////////////int opentty (const char *dev);//////////////////// Set the serial port baud rate///@param FD serial Device file Description descriptor///@param speed serial port baud rate//@return-1: Failed, 0: Success////////////////////////////////////////////////////////////void setttyspeed (int fd, int speed);////////////// Set the serial Port Properties///@param FD serial Device file Description descriptor///@param databits serial data bit///@param StopBits serial Stop bit//@param parity serial parity///@return-1: Failed. 0: Success////////////////////////////////////////////////////////////int setttyproperties (int fd,int databits,int Stopbits,int parity);///////////////////////////////////////////////////////////////open the serial device//@param FD Serial device file descriptive descriptor///@return-1: Failure, 0: Success/////////////////////////////////////void Closetty (int fd); int fd;};
Rs485Test.cpp
#include "rs485Test.h" int speed_arr[] ={B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300};int name_arr[] ={115 200, 38400, 19200, 9600, 4800, 2400, 1200, 300}; rs485::rs485 (char *name,int speed,int parity,int databits,int stopbits) {fd = Opentty (name); if (0 > FD) {printf ("tranconstructserial (): Open The%s device fail\n", name); }else{setttyspeed (fd, speed); Setttyproperties (FD, DataBits, stopbits, Parity);}} rs485::~rs485 () {close (FD);} int Rs485::opentty (const char *dev) {int fd; if (NULL = = Dev | | 0 = = strlen (dev)) return-1; FD = open (Dev, o_rdwr | O_noctty); if ( -1 = = FD) {printf ("Opentty (): Open then Ttydevice fail.\n"); Return-2; } return FD; void Rs485::setttyspeed (int fd, int speed) {int i; int status; struct Termios Opt; memset (&opt, 0, sizeof (struct termios)); 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) {printf ("setttyspeed (): tcsetattr fd fail\n"); Return } tcflush (FD, Tcioflush); }}}int rs485::setttyproperties (int fd, int databits, int stopbits, int parity) {struct Termios options; memset (&options, 0, sizeof (struct termios)); if (Tcgetattr (FD, &options)! = 0) {printf ("setttyproperties (): Can ' t get attr when Setup serial\n"); return-1; } options.c_cflag &= ~csize; Switch (databits) {case 7:options.c_cflag |= CS7; Break Case 8:options.c_cflag |= CS8; Break default:printf ("Setttyproperties (): Unsupported Data size\n"); Return-2; } 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:printf ("Setttyproperties (): Unsupported parity\n"); return-3; } switch (stopbits) {case 1:options.c_cflag &= ~CSTOPB; Break Case 2:options.c_cflag |= CSTOPB; Break default:printf ("Setttyproperties (): Unsupported Stop bits\n"); return-4; }/* Set input parity option */ if (parity! = ' n ' && parity! = ' n ') options.c_iflag |= inpck; Tcflush (FD, Tciflush); Options.c_cc[vtime] = 5; Options.c_cc[vmin] = 0; /* Update the options and do it now *//*set serial mode */Options.c_lflag &= ~ (Icanon | ECHO | Echoe | Isig | Iexten); /*input*/Options.c_oflag &= ~opost; /*output*/Options.c_iflag &= ~ (ICRNL | Ixon | Brkint | Istrip); Options.c_cflag |= (clocal | Cread); if (tcsetattr (FD, Tcsanow, &options)! = 0) {printf ("setttyproperties (): Setup Serial fail\n"); return-5; } return 0;} void Rs485::closetty (int fd) {close (FD);} int rs485::sendmsg (unsigned char * buf, int length) {int res; res = write (fd, buf, length); if (res! = length) {printf ("send_msg (): Send buf fail!\n"); return-1; }printf ("res =%d\n", res); return 0;} int rs485::recvmsg (unsigned char * msg, int length) {if (NULL = = Msg | | 0 = = length) return 0; printf ("Recv_msg (): The length of received buffer:%d\n ", length); Return read (FD, MSG, length);} int main () {RS485 rs485test ("/dev/ttyusb0", 115200, ' n ', 8, 1); unsigned char buf[8]= "lklk\n"; rs485test.sendmsg (buf,8); return 0;}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Simple Serial Communication Program