00001 00038 #if !defined(_WIN32) 00039 00040 #ifndef SERIAL_IMPL_UNIX_H 00041 #define SERIAL_IMPL_UNIX_H 00042 00043 #include "serial/serial.h" 00044 00045 #include <pthread.h> 00046 00047 namespace serial { 00048 00049 using std::size_t; 00050 using std::string; 00051 using std::invalid_argument; 00052 00053 using serial::SerialException; 00054 using serial::IOException; 00055 00056 class MillisecondTimer { 00057 public: 00058 MillisecondTimer(const uint32_t millis); 00059 int64_t remaining(); 00060 00061 private: 00062 static timespec timespec_now(); 00063 timespec expiry; 00064 }; 00065 00066 class serial::Serial::SerialImpl { 00067 public: 00068 SerialImpl (const string &port, 00069 unsigned long baudrate, 00070 bytesize_t bytesize, 00071 parity_t parity, 00072 stopbits_t stopbits, 00073 flowcontrol_t flowcontrol); 00074 00075 virtual ~SerialImpl (); 00076 00077 void 00078 open (); 00079 00080 void 00081 close (); 00082 00083 bool 00084 isOpen () const; 00085 00086 size_t 00087 available (); 00088 00089 bool 00090 waitReadable (uint32_t timeout); 00091 00092 void 00093 waitByteTimes (size_t count); 00094 00095 size_t 00096 read (uint8_t *buf, size_t size = 1); 00097 00098 size_t 00099 write (const uint8_t *data, size_t length); 00100 00101 void 00102 flush (); 00103 00104 void 00105 flushInput (); 00106 00107 void 00108 flushOutput (); 00109 00110 void 00111 sendBreak (int duration); 00112 00113 void 00114 setBreak (bool level); 00115 00116 void 00117 setRTS (bool level); 00118 00119 void 00120 setDTR (bool level); 00121 00122 bool 00123 waitForChange (); 00124 00125 bool 00126 getCTS (); 00127 00128 bool 00129 getDSR (); 00130 00131 bool 00132 getRI (); 00133 00134 bool 00135 getCD (); 00136 00137 void 00138 setPort (const string &port); 00139 00140 string 00141 getPort () const; 00142 00143 void 00144 setTimeout (Timeout &timeout); 00145 00146 Timeout 00147 getTimeout () const; 00148 00149 void 00150 setBaudrate (unsigned long baudrate); 00151 00152 unsigned long 00153 getBaudrate () const; 00154 00155 void 00156 setBytesize (bytesize_t bytesize); 00157 00158 bytesize_t 00159 getBytesize () const; 00160 00161 void 00162 setParity (parity_t parity); 00163 00164 parity_t 00165 getParity () const; 00166 00167 void 00168 setStopbits (stopbits_t stopbits); 00169 00170 stopbits_t 00171 getStopbits () const; 00172 00173 void 00174 setFlowcontrol (flowcontrol_t flowcontrol); 00175 00176 flowcontrol_t 00177 getFlowcontrol () const; 00178 00179 void 00180 readLock (); 00181 00182 void 00183 readUnlock (); 00184 00185 void 00186 writeLock (); 00187 00188 void 00189 writeUnlock (); 00190 00191 protected: 00192 void reconfigurePort (); 00193 00194 private: 00195 string port_; // Path to the file descriptor 00196 int fd_; // The current file descriptor 00197 00198 bool is_open_; 00199 bool xonxoff_; 00200 bool rtscts_; 00201 00202 Timeout timeout_; // Timeout for read operations 00203 unsigned long baudrate_; // Baudrate 00204 uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte 00205 00206 parity_t parity_; // Parity 00207 bytesize_t bytesize_; // Size of the bytes 00208 stopbits_t stopbits_; // Stop Bits 00209 flowcontrol_t flowcontrol_; // Flow Control 00210 00211 // Mutex used to lock the read functions 00212 pthread_mutex_t read_mutex; 00213 // Mutex used to lock the write functions 00214 pthread_mutex_t write_mutex; 00215 }; 00216 00217 } 00218 00219 #endif // SERIAL_IMPL_UNIX_H 00220 00221 #endif // !defined(_WIN32)