UniSet  1.4.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 
00009 #include <string>
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     void setSpeed( Speed s );
00074     void setSpeed( std::string speed );
00075     inline Speed getSpeed(){ return speed; }
00076     
00077     static Speed getSpeed( const std::string s );
00078     static std::string getSpeed( Speed s );
00079     
00080     void setParity(Parity);
00081     void setCharacterSize(CharacterSize);
00082     void setStopBits(StopBits sBit);
00083     
00084     virtual void setTimeout(int timeout);
00085     void setWaiting(bool waiting);
00086     inline int getTimeout(){ return uTimeout*1000; } // msec
00087     
00088     virtual unsigned char receiveByte();
00089     virtual void sendByte(unsigned char x);
00090 
00091     virtual int receiveBlock(unsigned char*msg,int len);
00092     virtual int sendBlock(unsigned char*msg,int len);
00093     
00094     void setBlocking(bool blocking);
00095     
00096     virtual void cleanupChannel();
00097     virtual void reopen();
00098     
00099 protected:
00100     void openPort();
00101 
00102     static const int BufSize=8192;
00103     unsigned char buf[BufSize];
00104     int curSym;
00105     int bufLength;
00106     int fd;
00107     int uTimeout;
00108     bool waiting;
00109     Speed speed;
00110     std::string dev;
00111     
00112     virtual unsigned char m_receiveByte( bool wait );
00113     
00114 private:
00115     struct termios oldTermios;
00116 };
00117 
00118 #endif // _COMPORT_H_