|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00017 #ifndef _COMPORT_H_ 00018 #define _COMPORT_H_ 00019 // -------------------------------------------------------------------------- 00020 #include <termios.h> 00021 #include <fcntl.h> 00022 #include <sys/ioctl.h> 00023 #include <string> 00024 #include "PassiveTimer.h" // for use timeout_t 00025 // -------------------------------------------------------------------------- 00026 namespace uniset 00027 { 00028 00029 class ComPort 00030 { 00031 public: 00032 enum Speed 00033 { 00034 ComSpeed0 = B0, 00035 ComSpeed50 = B50, 00036 ComSpeed75 = B75, 00037 ComSpeed110 = B110, 00038 ComSpeed134 = B134, 00039 ComSpeed150 = B150, 00040 ComSpeed200 = B200, 00041 ComSpeed300 = B300, 00042 ComSpeed600 = B600, 00043 ComSpeed1200 = B1200, 00044 ComSpeed1800 = B1800, 00045 ComSpeed2400 = B2400, 00046 ComSpeed4800 = B4800, 00047 ComSpeed9600 = B9600, 00048 ComSpeed19200 = B19200, 00049 ComSpeed38400 = B38400, 00050 ComSpeed57600 = B57600, 00051 ComSpeed115200 = B115200, 00052 ComSpeed230400 = B230400, 00053 ComSpeed460800 = B460800, 00054 ComSpeed500000 = B500000, 00055 ComSpeed576000 = B576000, 00056 ComSpeed921600 = B921600, 00057 ComSpeed1000000 = B1000000, 00058 ComSpeed1152000 = B1152000, 00059 ComSpeed1500000 = B1500000, 00060 ComSpeed2000000 = B2000000, 00061 ComSpeed2500000 = B2500000, 00062 ComSpeed3000000 = B3000000, 00063 ComSpeed3500000 = B3500000, 00064 ComSpeed4000000 = B4000000 00065 }; 00066 enum Parity 00067 { 00068 Odd, 00069 Even, 00070 Space, 00071 Mark, 00072 NoParity 00073 }; 00074 enum CharacterSize 00075 { 00076 CSize5 = CS5, 00077 CSize6 = CS6, 00078 CSize7 = CS7, 00079 CSize8 = CS8 00080 }; 00081 enum StopBits 00082 { 00083 OneBit = 1, 00084 OneAndHalfBits = 2, 00085 TwoBits = 3 00086 }; 00087 00088 ComPort( const std::string& comDevice, bool nocreate = false ); 00089 virtual ~ComPort(); 00090 00091 inline std::string getDevice() 00092 { 00093 return dev; 00094 } 00095 00096 void setSpeed( Speed s ); 00097 void setSpeed( const std::string& speed ); 00098 inline Speed getSpeed() 00099 { 00100 return speed; 00101 } 00102 00103 static Speed getSpeed( const std::string& s ); 00104 static std::string getSpeed( Speed s ); 00105 00106 void setParity(Parity); 00107 void setCharacterSize(CharacterSize); 00108 void setStopBits(StopBits sBit); 00109 00110 virtual void setTimeout( timeout_t msec ); 00111 inline timeout_t getTimeout() 00112 { 00113 return uTimeout / 1000; // msec 00114 } 00115 00116 void setWaiting(bool waiting); 00117 00118 virtual unsigned char receiveByte(); 00119 virtual void sendByte(unsigned char x); 00120 00121 virtual size_t receiveBlock(unsigned char* msg, size_t len); 00122 virtual size_t sendBlock(unsigned char* msg, size_t len); 00123 00124 void setBlocking(bool blocking); 00125 00126 virtual void cleanupChannel(); 00127 virtual void reopen(); 00128 00129 protected: 00130 void openPort(); 00131 00132 static const size_t BufSize = 8192; 00133 unsigned char buf[BufSize]; 00134 size_t curSym = { 0 }; 00135 size_t bufLength = { 0 }; 00136 int fd = { -1 }; 00137 timeout_t uTimeout = { 0 }; 00138 bool waiting = { false }; 00139 Speed speed = ComSpeed38400; 00140 std::string dev = { "" }; 00141 00142 virtual unsigned char m_receiveByte( bool wait ); 00143 00144 private: 00145 struct termios oldTermios = { 0 }; 00146 }; 00147 // ------------------------------------------------------------------------- 00148 } // end of uniset namespace 00149 // -------------------------------------------------------------------------- 00150 #endif // _COMPORT_H_ 00151 // --------------------------------------------------------------------------
1.7.6.1