-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialib.h
269 lines (188 loc) · 6.59 KB
/
serialib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*!
\file serialib.h
\brief Header file of the class serialib. This class is used for communication over a serial device.
\author Philippe Lucidarme (University of Angers)
\version 2.0
\date december the 27th of 2019
This Serial library is used to communicate through serial port.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This is a licence-free software, it can be used by anyone who try to build a better world.
*/
#ifndef SERIALIB_H
#define SERIALIB_H
#if defined(__CYGWIN__)
// This is Cygwin special case
#include <sys/time.h>
#endif
// Include for windows
#if defined (_WIN32) || defined (_WIN64)
#if defined(__GNUC__)
// This is MinGW special case
#include <sys/time.h>
#else
// sys/time.h does not exist on "actual" Windows
#define NO_POSIX_TIME
#endif
// Accessing to the serial port under Windows
#include <windows.h>
#endif
// Include for Linux
#if defined (__linux__) || defined(__APPLE__)
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <termios.h>
#include <string.h>
#include <iostream>
#include <sys/time.h>
// File control definitions
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#endif
/*! To avoid unused parameters */
#define UNUSED(x) (void)(x)
/**
* number of serial data bits
*/
enum SerialDataBits {
SERIAL_DATABITS_5, /**< 5 databits */
SERIAL_DATABITS_6, /**< 6 databits */
SERIAL_DATABITS_7, /**< 7 databits */
SERIAL_DATABITS_8, /**< 8 databits */
SERIAL_DATABITS_16, /**< 16 databits */
};
/**
* number of serial stop bits
*/
enum SerialStopBits {
SERIAL_STOPBITS_1, /**< 1 stop bit */
SERIAL_STOPBITS_1_5, /**< 1.5 stop bits */
SERIAL_STOPBITS_2, /**< 2 stop bits */
};
/**
* type of serial parity bits
*/
enum SerialParity {
SERIAL_PARITY_NONE, /**< no parity bit */
SERIAL_PARITY_EVEN, /**< even parity bit */
SERIAL_PARITY_ODD, /**< odd parity bit */
SERIAL_PARITY_MARK, /**< mark parity */
SERIAL_PARITY_SPACE /**< space bit */
};
/*! \class serialib
\brief This class is used for communication over a serial device.
*/
class serialib
{
public:
//_____________________________________
// ::: Constructors and destructors :::
// Constructor of the class
serialib ();
// Destructor
~serialib ();
//_________________________________________
// ::: Configuration and initialization :::
// Open a device
char openDevice(const char *Device, const unsigned int Bauds,
SerialDataBits Databits = SERIAL_DATABITS_8,
SerialParity Parity = SERIAL_PARITY_NONE,
SerialStopBits Stopbits = SERIAL_STOPBITS_1);
// Check device opening state
bool isDeviceOpen();
// Close the current device
void closeDevice();
//___________________________________________
// ::: Read/Write operation on characters :::
// Write a char
int writeChar (char);
// Read a char (with timeout)
int readChar (char *pByte,const unsigned int timeOut_ms=0);
//________________________________________
// ::: Read/Write operation on strings :::
// Write a string
int writeString (const char *String);
// Read a string (with timeout)
int readString ( char *receivedString,
char finalChar,
unsigned int maxNbBytes,
const unsigned int timeOut_ms=0);
// _____________________________________
// ::: Read/Write operation on bytes :::
// Write an array of bytes
int writeBytes (const void *Buffer, const unsigned int NbBytes);
// Read an array of byte (with timeout)
int readBytes (void *buffer,unsigned int maxNbBytes,const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100);
// _________________________
// ::: Special operation :::
// Empty the received buffer
char flushReceiver();
// Return the number of bytes in the received buffer
int available();
// _________________________
// ::: Access to IO bits :::
// Set CTR status (Data Terminal Ready, pin 4)
bool DTR(bool status);
bool setDTR();
bool clearDTR();
// Set RTS status (Request To Send, pin 7)
bool RTS(bool status);
bool setRTS();
bool clearRTS();
// Get RI status (Ring Indicator, pin 9)
bool isRI();
// Get DCD status (Data Carrier Detect, pin 1)
bool isDCD();
// Get CTS status (Clear To Send, pin 8)
bool isCTS();
// Get DSR status (Data Set Ready, pin 9)
bool isDSR();
// Get RTS status (Request To Send, pin 7)
bool isRTS();
// Get CTR status (Data Terminal Ready, pin 4)
bool isDTR();
private:
// Read a string (no timeout)
int readStringNoTimeOut (char *String,char FinalChar,unsigned int MaxNbBytes);
// Current DTR and RTS state (can't be read on WIndows)
bool currentStateRTS;
bool currentStateDTR;
#if defined (_WIN32) || defined( _WIN64)
// Handle on serial device
HANDLE hSerial;
// For setting serial port timeouts
COMMTIMEOUTS timeouts;
#endif
#if defined (__linux__) || defined(__APPLE__)
int fd;
#endif
};
/*! \class timeOut
\brief This class can manage a timer which is used as a timeout.
*/
// Class timeOut
class timeOut
{
public:
// Constructor
timeOut();
// Init the timer
void initTimer();
// Return the elapsed time since initialization
unsigned long int elapsedTime_ms();
private:
#if defined (NO_POSIX_TIME)
// Used to store the previous time (for computing timeout)
LONGLONG counterFrequency;
LONGLONG previousTime;
#else
// Used to store the previous time (for computing timeout)
struct timeval previousTime;
#endif
};
#endif // serialib_H