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