UniSet  2.2.1
ComPort.h
00001 // --------------------------------------------------------------------------
00002 #ifndef _COMPORT_H_
00003 #define _COMPORT_H_
00004 // --------------------------------------------------------------------------
00005 #include <termios.h>
00006 #include <fcntl.h>
00007 #include <sys/ioctl.h>
00008 #include <string>
00009 #include <cc++/thread.h> // for use timeout_t
00010 // --------------------------------------------------------------------------
00011 class ComPort
00012 {
00013     public:
00014         enum Speed
00015         {
00016             ComSpeed0 = B0,
00017             ComSpeed50 = B50,
00018             ComSpeed75 = B75,
00019             ComSpeed110 = B110,
00020             ComSpeed134 = B134,
00021             ComSpeed150 = B150,
00022             ComSpeed200 = B200,
00023             ComSpeed300 = B300,
00024             ComSpeed600 = B600,
00025             ComSpeed1200 = B1200,
00026             ComSpeed1800 = B1800,
00027             ComSpeed2400 = B2400,
00028             ComSpeed4800 = B4800,
00029             ComSpeed9600 = B9600,
00030             ComSpeed19200 = B19200,
00031             ComSpeed38400 = B38400,
00032             ComSpeed57600 = B57600,
00033             ComSpeed115200 = B115200,
00034             ComSpeed230400 = B230400,
00035             ComSpeed460800 = B460800,
00036             ComSpeed500000 = B500000,
00037             ComSpeed576000 = B576000,
00038             ComSpeed921600 = B921600,
00039             ComSpeed1000000 = B1000000,
00040             ComSpeed1152000 = B1152000,
00041             ComSpeed1500000 = B1500000,
00042             ComSpeed2000000 = B2000000,
00043             ComSpeed2500000 = B2500000,
00044             ComSpeed3000000 = B3000000,
00045             ComSpeed3500000 = B3500000,
00046             ComSpeed4000000 = B4000000
00047         };
00048         enum Parity
00049         {
00050             Odd,
00051             Even,
00052             Space,
00053             Mark,
00054             NoParity
00055         };
00056         enum CharacterSize
00057         {
00058             CSize5 = CS5,
00059             CSize6 = CS6,
00060             CSize7 = CS7,
00061             CSize8 = CS8
00062         };
00063         enum StopBits
00064         {
00065             OneBit = 1,
00066             OneAndHalfBits = 2,
00067             TwoBits = 3
00068         };
00069 
00070         ComPort( const std::string& comDevice, bool nocreate = false );
00071         virtual ~ComPort();
00072 
00073         inline std::string getDevice()
00074         {
00075             return dev;
00076         }
00077 
00078         void setSpeed( Speed s );
00079         void setSpeed( const std::string& speed );
00080         inline Speed getSpeed()
00081         {
00082             return speed;
00083         }
00084 
00085         static Speed getSpeed( const std::string& s );
00086         static std::string getSpeed( Speed s );
00087 
00088         void setParity(Parity);
00089         void setCharacterSize(CharacterSize);
00090         void setStopBits(StopBits sBit);
00091 
00092         virtual void setTimeout( timeout_t msec );
00093         inline timeout_t getTimeout()
00094         {
00095             return uTimeout / 1000;    // msec
00096         }
00097 
00098         void setWaiting(bool waiting);
00099 
00100         virtual unsigned char receiveByte();
00101         virtual void sendByte(unsigned char x);
00102 
00103         virtual size_t receiveBlock(unsigned char* msg, size_t len);
00104         virtual size_t sendBlock(unsigned char* msg, size_t len);
00105 
00106         void setBlocking(bool blocking);
00107 
00108         virtual void cleanupChannel();
00109         virtual void reopen();
00110 
00111     protected:
00112         void openPort();
00113 
00114         static const int BufSize = 8192;
00115         unsigned char buf[BufSize];
00116         int curSym = { 0 };
00117         int bufLength = { 0 };
00118         int fd = { -1 };
00119         timeout_t uTimeout = { 0 };
00120         bool waiting = { false };
00121         Speed speed = ComSpeed38400;
00122         std::string dev = { "" };
00123 
00124         virtual unsigned char m_receiveByte( bool wait );
00125 
00126     private:
00127         struct termios oldTermios = { 0 };
00128 };
00129 // --------------------------------------------------------------------------
00130 #endif // _COMPORT_H_
00131 // --------------------------------------------------------------------------