LxSerial.h
Go to the documentation of this file.
00001 //============================================================================
00002 // Name        : LxSerial.cpp
00003 // Author      : Eelko van Breda,www.dbl.tudelft.nl
00004 // Version     : 0.1
00005 // Copyright   : Copyright (c) 2008 LGPL
00006 // Description : serial communicatin class linux
00007 //============================================================================
00008 
00009 #ifndef LXSERIAL_H_
00010 #define LXSERIAL_H_
00011 //#define __DBG__
00012 
00013 #include <fcntl.h>                                                                                                                              /* fileio */
00014 #include <termios.h>                                                                                                                    /* terminal i/o system, talks to /dev/tty* ports  */
00015 #include <unistd.h>                                                                                                                             /* Read function */
00016 #include <sys/ioctl.h>                                                                                                                  /* ioctl function */
00017 #include <iostream>
00018 #include <assert.h>
00019 #include <stdint.h>
00020 #include <string.h>
00021 
00022 #define INVALID_DEVICE_HANDLE           -1
00023 
00024 #ifdef __APPLE__
00025         #ifndef IOSSIOSPEED
00026                 #define IOSSIOSPEED    _IOW('T', 2, speed_t)
00027         #endif
00028 #endif
00029 
00030 class LxSerial
00031 {
00032         public:
00033                 enum PortType {         RS232,                                                                                                  // Normal RS232
00034                                                         RS485_EXAR,                                                                                             // EXAR XR16C2850
00035                                                         RS485_FTDI,                                                                                             // FTDI FT232RL in 485 mode
00036                                                         RS485_SMSC,                                                                                             // SMSC SCH311X RS-485 mode (Versalogic Sidewinder board)
00037                                                         TCP
00038                 };
00039 #ifdef __APPLE__
00040                 enum PortSpeed {        S50                     =       50,                                                                     // Baudrate to use for the port --> see termios.h
00041                                                         S75                     =       75,
00042                                                         S110            =       110,
00043                                                         S134            =       134,
00044                                                         S150            =       150,
00045                                                         S200            =       200,
00046                                                         S300            =       300,
00047                                                         S600            =       600,
00048                                                         S1200           =       1200,
00049                                                         S1800           =       1800,
00050                                                         S2400           =       2400,
00051                                                         S4800           =       4800,
00052                                                         S9600           =       9600,
00053                                                         S19200          =       19200,
00054                                                         S38400          =       38400,
00055                                                         S57600          =       57600,
00056                                                         S115200         =       115200,
00057                                                         S230400         =       230400,
00058                                                         S460800         =       460800,
00059                                                         S500000         =       500000,
00060                                                         S576000         =       576000,
00061                                                         S921600         =       921600,
00062                                                         S1000000        =       1000000,
00063                                                         S1152000        =       1152000,
00064                                                         S1500000        =       1500000,
00065                                                         S2000000        =       2000000,
00066                                                         S2500000        =       2500000,
00067                                                         S3000000        =       3000000,
00068                                                         S3500000        =       3500000,
00069                                                         S4000000        =       4000000
00070                 };
00071 #else
00072                 enum PortSpeed {        S50                     =       B50,                                                                    // Baudrate to use for the port --> see termios.h
00073                                                         S75                     =       B75,
00074                                                         S110            =       B110,
00075                                                         S134            =       B134,
00076                                                         S150            =       B150,
00077                                                         S200            =       B200,
00078                                                         S300            =       B300,
00079                                                         S600            =       B600,
00080                                                         S1200           =       B1200,
00081                                                         S1800           =       B1800,
00082                                                         S2400           =       B2400,
00083                                                         S4800           =       B4800,
00084                                                         S9600           =       B9600,
00085                                                         S19200          =       B19200,
00086                                                         S38400          =       B38400,
00087                                                         S57600          =       B57600,
00088                                                         S115200         =       B115200,
00089                                                         S230400         =       B230400,
00090                                                         S460800         =       B460800,
00091                                                         S500000         =       B500000,
00092                                                         S576000         =       B576000,
00093                                                         S921600         =       B921600,
00094                                                         S1000000        =       B1000000,
00095                                                         S1152000        =       B1152000,
00096                                                         S1500000        =       B1500000,
00097                                                         S2000000        =       B2000000,
00098                                                         S2500000        =       B2500000,
00099                                                         S3000000        =       B3000000,
00100                                                         S3500000        =       B3500000,
00101                                                         S4000000        =       B4000000
00102                 };
00103 #endif
00104                 /*return values*/
00105                 static const int READ_ERROR                             =       -1;
00106                 static const int COLLISION_DETECT_ERROR         =       -2;
00107                 static const int ECHO_TIMEOUT_ERROR                     =       -3;
00108 
00109                 /*magic numbers*/
00110                 static const int COLLISION_WAIT_TIME_USEC       =       10000;                                                  // microseconds
00111                 static const int ECHO_WAIT_TIME_SEC                     =       1;                                                              // seconds
00112                 static const int ECHO_WAIT_TIME_USEC            =       0;                                                      // microseconds
00113                 static const int WAIT_FOR_DATA_DSEC                     =       5;                                                              //
00114 
00115         protected:
00116                 int                     hPort;                                                                                                                  // file handle to the port
00117                 std::string             s_port_name;                                                                                                    // name of the port that was opened
00118 //              bool                    b_initialised;                                                                                                  //
00119                 bool                    b_clear_echo;                                                                                                   // read sended characters from Rx when true
00120                 bool                    b_rts;                                                                                                                  // this boolean must be set to enforce setting the RTS signal without hardware contol enabled
00121                 bool                    b_hw_flow_control;                                                                                              //
00122                 bool                    b_socket;
00123                 termios                 options, old_options;                                                                                   //
00124                 bool                    wait_for_input(int *seconds, int *microseconds);                                        // private member function to wait for port. the time variables are modified after return to reflect the time not slept
00125                 void                    set_port_type(LxSerial::PortType port_type);
00126 
00127         public:
00128                         LxSerial();
00129                          ~LxSerial();
00130                 bool    port_open(const std::string& portname, LxSerial::PortType port_type);   // open serial port. If overridden, make sure you set s_port_name!!
00131                 bool    is_port_open();
00132                 std::string&    get_port_name();
00133                 bool    set_speed(LxSerial::PortSpeed baudrate );                                               // enumerated
00134                 bool    set_speed_int(const int baudrate);      // Set speed by integer value directly - UNPROTECTED!
00135                 void    set_clear_echo(bool clear);                                                                             // clear echoed charackters from input and detect collisions on write
00136                 bool    port_close();
00137                 int     port_read(unsigned char* buffer, int numBytes) const;
00138                 int     port_read(unsigned char* buffer, int numBytes, int seconds, int microseconds);
00139                 int     port_write(unsigned char* buffer, int numBytes);
00140                 void    flush_buffer();                                                                                                 // flush input and output buffers
00141 
00142 };
00143 
00144 
00145 #endif /*LXSERIAL_H_*/


shared_serial
Author(s): Wouter Caarls
autogenerated on Fri Aug 28 2015 13:06:29