serialib.h 6.3 KB

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