Falcon
Loading...
Searching...
No Matches
serialib.h
Go to the documentation of this file.
1
20
21#ifndef SERIALIB_H
22#define SERIALIB_H
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#ifdef __linux__
34#include <stdlib.h>
35#include <string.h>
36#include <sys/shm.h>
37#include <sys/types.h>
38#include <termios.h>
39
40#include <iostream>
41// File control definitions
42#include <fcntl.h>
43#include <sys/ioctl.h>
44#include <unistd.h>
45#endif
46
48#define UNUSED(x) (void) (x)
49
53class serialib {
54 public:
55 //_____________________________________
56 // ::: Constructors and destructors :::
57
58 // Constructor of the class
59 serialib();
60
61 // Destructor
62 ~serialib();
63
64 //_________________________________________
65 // ::: Configuration and initialization :::
66
67 // Open a device
68 char openDevice(const char* Device, const unsigned int Bauds);
69
70 // Close the current device
71 void closeDevice();
72
73 //___________________________________________
74 // ::: Read/Write operation on characters :::
75
76 // Write a char
77 char writeChar(char);
78
79 // Read a char (with timeout)
80 char readChar(char* pByte, const unsigned int timeOut_ms = 0);
81
82 //________________________________________
83 // ::: Read/Write operation on strings :::
84
85 // Write a string
86 char writeString(const char* String);
87
88 // Read a string (with timeout)
89 int readString(char* receivedString, char finalChar, unsigned int maxNbBytes,
90 const unsigned int timeOut_ms = 0);
91
92 // _____________________________________
93 // ::: Read/Write operation on bytes :::
94
95 // Write an array of bytes
96 char writeBytes(const void* Buffer, const unsigned int NbBytes);
97
98 // Read an array of byte (with timeout)
99 int readBytes(void* buffer, unsigned int maxNbBytes, const unsigned int timeOut_ms = 0,
100 unsigned int sleepDuration_us = 100);
101
102 // _________________________
103 // ::: Special operation :::
104
105 // Empty the received buffer
106 char flushReceiver();
107
108 // Return the number of bytes in the received buffer
109 int available();
110
111 // _________________________
112 // ::: Access to IO bits :::
113
114 // Set CTR status (Data Terminal Ready, pin 4)
115 bool DTR(bool status);
116 bool setDTR();
117 bool clearDTR();
118
119 // Set RTS status (Request To Send, pin 7)
120 bool RTS(bool status);
121 bool setRTS();
122 bool clearRTS();
123
124 // Get RI status (Ring Indicator, pin 9)
125 bool isRI();
126
127 // Get DCD status (Data Carrier Detect, pin 1)
128 bool isDCD();
129
130 // Get CTS status (Clear To Send, pin 8)
131 bool isCTS();
132
133 // Get DSR status (Data Set Ready, pin 9)
134 bool isDSR();
135
136 // Get RTS status (Request To Send, pin 7)
137 bool isRTS();
138
139 // Get CTR status (Data Terminal Ready, pin 4)
140 bool isDTR();
141
142 private:
143 // Read a string (no timeout)
144 int readStringNoTimeOut(char* String, char FinalChar, unsigned int MaxNbBytes);
145
146 // Current DTR and RTS state (can't be read on WIndows)
147 bool currentStateRTS;
148 bool currentStateDTR;
149
150#if defined(_WIN32) || defined(_WIN64)
151 // Handle on serial device
152 HANDLE hSerial;
153 // For setting serial port timeouts
154 COMMTIMEOUTS timeouts;
155#endif
156#ifdef __linux__
157 int fd;
158#endif
159};
160
164// Class timeOut
165class timeOut {
166 public:
167 // Constructor
168 timeOut();
169
170 // Init the timer
171 void initTimer();
172
173 // Return the elapsed time since initialization
174 unsigned long int elapsedTime_ms();
175
176 private:
177 // Used to store the previous time (for computing timeout)
178 struct timeval previousTime;
179};
180
181#endif // serialib_H
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: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:643
bool isDSR()
Get the DSR's status (pin 6) DSR stands for Data Set Ready.
Definition serialib.cpp:782
bool isDTR()
Get the DTR's status (pin 4) DTR stands for Data Terminal Ready May behave abnormally on Windows.
Definition serialib.cpp:841
int available()
Return the number of bytes in the received buffer (UNIX only).
Definition serialib.cpp:612
char flushReceiver()
Empty receiver buffer.
Definition serialib.cpp:595
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:705
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:802
bool isRI()
Get the RING's status (pin 9) Ring Indicator.
Definition serialib.cpp:821
char writeString(const char *String)
Write a string on the current serial port.
Definition serialib.cpp:311
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:371
bool setDTR()
Set the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition serialib.cpp:658
void closeDevice()
Close the connection with the current device.
Definition serialib.cpp:264
char writeBytes(const void *Buffer, const unsigned int NbBytes)
Write an array of data on the current serial port.
Definition serialib.cpp:342
char writeChar(char)
Write a char on the current serial port.
Definition serialib.cpp:282
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:536
bool clearRTS()
Clear the bit RTS (pin 7) RTS stands for Data Terminal Ready.
Definition serialib.cpp:742
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:466
bool isRTS()
Get the RTS's status (pin 7) RTS stands for Request To Send May behave abnormally on Windows.
Definition serialib.cpp:859
bool isCTS()
Get the CTS's status (pin 8) CTS stands for Clear To Send.
Definition serialib.cpp:763
bool clearDTR()
Clear the bit DTR (pin 4) DTR stands for Data Terminal Ready.
Definition serialib.cpp:680
timeOut()
Constructor of the class timeOut.
Definition serialib.cpp:879
void initTimer()
Initialise the timer. It writes the current time of the day in the structure PreviousTime.
Definition serialib.cpp:887
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:899