Serialib  2.0
Multi plateform serial library
serialib.h
Go to the documentation of this file.
1 
19 #ifndef SERIALIB_H
20 #define SERIALIB_H
21 
22 
23 
24 // Used for TimeOut operations
25 #include <sys/time.h>
26 // Include for windows
27 #if defined (_WIN32) || defined (_WIN64)
28  // Accessing to the serial port under Windows
29  #include <windows.h>
30 #endif
31 
32 // Include for Linux
33 #if defined (__linux__) || defined(__APPLE__)
34  #include <stdlib.h>
35  #include <sys/types.h>
36  #include <sys/shm.h>
37  #include <termios.h>
38  #include <string.h>
39  #include <iostream>
40  // File control definitions
41  #include <fcntl.h>
42  #include <unistd.h>
43  #include <sys/ioctl.h>
44 #endif
45 
46 
48 #define UNUSED(x) (void)(x)
49 
59 };
60 
68 };
69 
79 };
80 
84 class serialib
85 {
86 public:
87 
88  //_____________________________________
89  // ::: Constructors and destructors :::
90 
91 
92 
93  // Constructor of the class
94  serialib ();
95 
96  // Destructor
97  ~serialib ();
98 
99 
100 
101  //_________________________________________
102  // ::: Configuration and initialization :::
103 
104 
105  // Open a device
106  char openDevice(const char *Device, const unsigned int Bauds,
109  SerialStopBits Stopbits = SERIAL_STOPBITS_1);
110 
111  // Close the current device
112  void closeDevice();
113 
114 
115 
116 
117  //___________________________________________
118  // ::: Read/Write operation on characters :::
119 
120 
121  // Write a char
122  char writeChar (char);
123 
124  // Read a char (with timeout)
125  char readChar (char *pByte,const unsigned int timeOut_ms=0);
126 
127 
128 
129 
130  //________________________________________
131  // ::: Read/Write operation on strings :::
132 
133 
134  // Write a string
135  char writeString (const char *String);
136 
137  // Read a string (with timeout)
138  int readString ( char *receivedString,
139  char finalChar,
140  unsigned int maxNbBytes,
141  const unsigned int timeOut_ms=0);
142 
143 
144 
145  // _____________________________________
146  // ::: Read/Write operation on bytes :::
147 
148 
149  // Write an array of bytes
150  char writeBytes (const void *Buffer, const unsigned int NbBytes);
151 
152  // Read an array of byte (with timeout)
153  int readBytes (void *buffer,unsigned int maxNbBytes,const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100);
154 
155 
156 
157 
158  // _________________________
159  // ::: Special operation :::
160 
161 
162  // Empty the received buffer
163  char flushReceiver();
164 
165  // Return the number of bytes in the received buffer
166  int available();
167 
168 
169 
170 
171  // _________________________
172  // ::: Access to IO bits :::
173 
174 
175  // Set CTR status (Data Terminal Ready, pin 4)
176  bool DTR(bool status);
177  bool setDTR();
178  bool clearDTR();
179 
180  // Set RTS status (Request To Send, pin 7)
181  bool RTS(bool status);
182  bool setRTS();
183  bool clearRTS();
184 
185  // Get RI status (Ring Indicator, pin 9)
186  bool isRI();
187 
188  // Get DCD status (Data Carrier Detect, pin 1)
189  bool isDCD();
190 
191  // Get CTS status (Clear To Send, pin 8)
192  bool isCTS();
193 
194  // Get DSR status (Data Set Ready, pin 9)
195  bool isDSR();
196 
197  // Get RTS status (Request To Send, pin 7)
198  bool isRTS();
199 
200  // Get CTR status (Data Terminal Ready, pin 4)
201  bool isDTR();
202 
203 
204 private:
205  // Read a string (no timeout)
206  int readStringNoTimeOut (char *String,char FinalChar,unsigned int MaxNbBytes);
207 
208  // Current DTR and RTS state (can't be read on WIndows)
209  bool currentStateRTS;
210  bool currentStateDTR;
211 
212 
213 
214 
215 
216 #if defined (_WIN32) || defined( _WIN64)
217  // Handle on serial device
218  HANDLE hSerial;
219  // For setting serial port timeouts
220  COMMTIMEOUTS timeouts;
221 #endif
222 #if defined (__linux__) || defined(__APPLE__)
223  int fd;
224 #endif
225 
226 };
227 
228 
229 
233 // Class timeOut
234 class timeOut
235 {
236 public:
237 
238  // Constructor
239  timeOut();
240 
241  // Init the timer
242  void initTimer();
243 
244  // Return the elapsed time since initialization
245  unsigned long int elapsedTime_ms();
246 
247 private:
248  // Used to store the previous time (for computing timeout)
249  struct timeval previousTime;
250 };
251 
252 #endif // serialib_H
This class is used for communication over a serial device.
Definition: serialib.h:85
bool setRTS()
Set the bit RTS (pin 7) RTS stands for Data Terminal Ready.
Definition: serialib.cpp:806
serialib()
Constructor of the class serialib.
Definition: serialib.cpp:29
bool DTR(bool status)
Set or unset the bit DTR (pin 4) DTR stands for Data Terminal Ready Convenience method :This method c...
Definition: serialib.cpp:721
bool isDSR()
Get the DSR's status (pin 6) DSR stands for Data Set Ready.
Definition: serialib.cpp:878
bool isDTR()
Get the DTR's status (pin 4) DTR stands for Data Terminal Ready May behave abnormally on Windows.
Definition: serialib.cpp:947
int available()
Return the number of bytes in the received buffer (UNIX only)
Definition: serialib.cpp:686
char flushReceiver()
Empty receiver buffer.
Definition: serialib.cpp:667
bool RTS(bool status)
Set or unset the bit RTS (pin 7) RTS stands for Data Termina Ready Convenience method :This method ca...
Definition: serialib.cpp:789
bool isDCD()
Get the DCD's status (pin 1) CDC stands for Data Carrier Detect.
Definition: serialib.cpp:904
bool isRI()
Get the RING's status (pin 9) Ring Indicator.
Definition: serialib.cpp:925
char writeString(const char *String)
Write a string on the current serial port.
Definition: serialib.cpp:359
char readChar(char *pByte, const unsigned int timeOut_ms=0)
Wait for a byte from the serial device and return the data read.
Definition: serialib.cpp:425
bool setDTR()
Set the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition: serialib.cpp:738
void closeDevice()
Close the connection with the current device.
Definition: serialib.cpp:303
char writeBytes(const void *Buffer, const unsigned int NbBytes)
Write an array of data on the current serial port.
Definition: serialib.cpp:393
char writeChar(char)
Write a char on the current serial port.
Definition: serialib.cpp:327
int readBytes(void *buffer, unsigned int maxNbBytes, const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100)
Read an array of bytes from the serial device (with timeout)
Definition: serialib.cpp:599
bool clearRTS()
Clear the bit RTS (pin 7) RTS stands for Data Terminal Ready.
Definition: serialib.cpp:831
int readString(char *receivedString, char finalChar, unsigned int maxNbBytes, const unsigned int timeOut_ms=0)
Read a string from the serial device (with timeout)
Definition: serialib.cpp:525
bool isRTS()
Get the RTS's status (pin 7) RTS stands for Request To Send May behave abnormally on Windows.
Definition: serialib.cpp:968
~serialib()
Destructor of the class serialib. It close the connection.
Definition: serialib.cpp:43
bool isCTS()
Get the CTS's status (pin 8) CTS stands for Clear To Send.
Definition: serialib.cpp:856
bool clearDTR()
Clear the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition: serialib.cpp:761
char openDevice(const char *Device, const unsigned int Bauds, SerialDataBits Databits=SERIAL_DATABITS_8, SerialParity Parity=SERIAL_PARITY_NONE, SerialStopBits Stopbits=SERIAL_STOPBITS_1)
Open the serial port.
Definition: serialib.cpp:124
This class can manage a timer which is used as a timeout.
Definition: serialib.h:235
timeOut()
Constructor of the class timeOut.
Definition: serialib.cpp:995
void initTimer()
Initialise the timer. It writes the current time of the day in the structure PreviousTime.
Definition: serialib.cpp:1003
unsigned long int elapsedTime_ms()
Returns the time elapsed since initialization. It write the current time of the day in the structure ...
Definition: serialib.cpp:1014
SerialDataBits
Definition: serialib.h:53
@ SERIAL_DATABITS_5
Definition: serialib.h:54
@ SERIAL_DATABITS_16
Definition: serialib.h:58
@ SERIAL_DATABITS_8
Definition: serialib.h:57
@ SERIAL_DATABITS_7
Definition: serialib.h:56
@ SERIAL_DATABITS_6
Definition: serialib.h:55
SerialParity
Definition: serialib.h:73
@ SERIAL_PARITY_EVEN
Definition: serialib.h:75
@ SERIAL_PARITY_SPACE
Definition: serialib.h:78
@ SERIAL_PARITY_MARK
Definition: serialib.h:77
@ SERIAL_PARITY_NONE
Definition: serialib.h:74
@ SERIAL_PARITY_ODD
Definition: serialib.h:76
SerialStopBits
Definition: serialib.h:64
@ SERIAL_STOPBITS_2
Definition: serialib.h:67
@ SERIAL_STOPBITS_1
Definition: serialib.h:65
@ SERIAL_STOPBITS_1_5
Definition: serialib.h:66