arduino-emulator
Loading...
Searching...
No Matches
serialib.h
Go to the documentation of this file.
1#pragma once
17#if USE_SERIALLIB
18
19// Used for TimeOut operations
20#include <sys/time.h>
21// Include for windows
22#if defined (_WIN32) || defined (_WIN64)
23 // Accessing to the serial port under Windows
24 #include <windows.h>
25#endif
26
27// Include for Linux
28#ifdef __linux__
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/shm.h>
32 #include <termios.h>
33 #include <string.h>
34 #include <iostream>
35 // File control definitions
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <sys/ioctl.h>
39#endif
40
41
43#define UNUSED(x) (void)(x)
44
45
50{
51public:
52
53 //_____________________________________
54 // ::: Constructors and destructors :::
55
56
57
58 // Constructor of the class
59 serialib ();
60
61 // Destructor
62 ~serialib ();
63
64
65
66 //_________________________________________
67 // ::: Configuration and initialization :::
68
69
70 // Open a device
71 char openDevice (const char *Device,const unsigned int Bauds);
72
73 // Close the current device
74 void closeDevice();
75
76
77
78
79 //___________________________________________
80 // ::: Read/Write operation on characters :::
81
82
83 // Write a char
84 char writeChar (char);
85
86 // Read a char (with timeout)
87 char readChar (char *pByte,const unsigned int timeOut_ms=0);
88
89
90
91
92 //________________________________________
93 // ::: Read/Write operation on strings :::
94
95
96 // Write a string
97 char writeString (const char *String);
98
99 // Read a string (with timeout)
100 int readString ( char *receivedString,
101 char finalChar,
102 unsigned int maxNbBytes,
103 const unsigned int timeOut_ms=0);
104
105
106
107 // _____________________________________
108 // ::: Read/Write operation on bytes :::
109
110
111 // Write an array of bytes
112 char writeBytes (const void *Buffer, const unsigned int NbBytes);
113
114 // Read an array of byte (with timeout)
115 int readBytes (void *buffer,unsigned int maxNbBytes,const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100);
116
117
118
119
120 // _________________________
121 // ::: Special operation :::
122
123
124 // Empty the received buffer
125 char flushReceiver();
126
127 // Return the number of bytes in the received buffer
128 int available();
129
130
131
132
133 // _________________________
134 // ::: Access to IO bits :::
135
136
137 // Set CTR status (Data Terminal Ready, pin 4)
138 bool DTR(bool status);
139 bool setDTR();
140 bool clearDTR();
141
142 // Set RTS status (Request To Send, pin 7)
143 bool RTS(bool status);
144 bool setRTS();
145 bool clearRTS();
146
147 // Get RI status (Ring Indicator, pin 9)
148 bool isRI();
149
150 // Get DCD status (Data Carrier Detect, pin 1)
151 bool isDCD();
152
153 // Get CTS status (Clear To Send, pin 8)
154 bool isCTS();
155
156 // Get DSR status (Data Set Ready, pin 9)
157 bool isDSR();
158
159 // Get RTS status (Request To Send, pin 7)
160 bool isRTS();
161
162 // Get CTR status (Data Terminal Ready, pin 4)
163 bool isDTR();
164
165
166private:
167 // Read a string (no timeout)
168 int readStringNoTimeOut (char *String,char FinalChar,unsigned int MaxNbBytes);
169
170 // Current DTR and RTS state (can't be read on WIndows)
171 bool currentStateRTS;
172 bool currentStateDTR;
173
174
175
176
177
178#if defined (_WIN32) || defined( _WIN64)
179 // Handle on serial device
180 HANDLE hSerial;
181 // For setting serial port timeouts
182 COMMTIMEOUTS timeouts;
183#endif
184#ifdef __linux__
185 int fd;
186#endif
187
188};
189
190
191
195// Class timeOut
197{
198public:
199
200 // Constructor
201 timeOut();
202
203 // Init the timer
204 void initTimer();
205
206 // Return the elapsed time since initialization
207 unsigned long int elapsedTime_ms();
208
209private:
210 // Used to store the previous time (for computing timeout)
211 struct timeval previousTime;
212};
213
214#endif
This class is used for communication over a serial device.
Definition serialib.h:50
bool setRTS()
Set the bit RTS (pin 7) RTS stands for Data Terminal Ready.
Definition serialib.cpp:720
serialib()
Constructor of the class serialib.
Definition serialib.cpp:26
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:635
bool isDSR()
Get the DSR's status (pin 6) DSR stands for Data Set Ready.
Definition serialib.cpp:792
bool isDTR()
Get the DTR's status (pin 4) DTR stands for Data Terminal Ready May behave abnormally on Windows.
Definition serialib.cpp:861
int available()
Return the number of bytes in the received buffer (UNIX only)
Definition serialib.cpp:600
char flushReceiver()
Empty receiver buffer.
Definition serialib.cpp:581
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:703
char openDevice(const char *Device, const unsigned int Bauds)
Open the serial port.
Definition serialib.cpp:92
bool isDCD()
Get the DCD's status (pin 1) CDC stands for Data Carrier Detect.
Definition serialib.cpp:818
bool isRI()
Get the RING's status (pin 9) Ring Indicator.
Definition serialib.cpp:839
char writeString(const char *String)
Write a string on the current serial port.
Definition serialib.cpp:273
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:339
bool setDTR()
Set the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition serialib.cpp:652
void closeDevice()
Close the connection with the current device.
Definition serialib.cpp:217
char writeBytes(const void *Buffer, const unsigned int NbBytes)
Write an array of data on the current serial port.
Definition serialib.cpp:307
char writeChar(char)
Write a char on the current serial port.
Definition serialib.cpp:241
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:513
bool clearRTS()
Clear the bit RTS (pin 7) RTS stands for Data Terminal Ready.
Definition serialib.cpp:745
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:439
bool isRTS()
Get the RTS's status (pin 7) RTS stands for Request To Send May behave abnormally on Windows.
Definition serialib.cpp:882
~serialib()
Destructor of the class serialib. It close the connection.
Definition serialib.cpp:40
bool isCTS()
Get the CTS's status (pin 8) CTS stands for Clear To Send.
Definition serialib.cpp:770
bool clearDTR()
Clear the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition serialib.cpp:675
This class can manage a timer which is used as a timeout.
Definition serialib.h:197
timeOut()
Constructor of the class timeOut.
Definition serialib.cpp:909
void initTimer()
Initialise the timer. It writes the current time of the day in the structure PreviousTime.
Definition serialib.cpp:917
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:928