Advertisement
Guest User

header

a guest
Mar 15th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. /*!
  2. \file serialib.h
  3. \brief Serial library to communicate throught serial port, or any device emulating a serial port.
  4. \author Philippe Lucidarme (University of Angers) <serialib@googlegroups.com>
  5. \version 1.2
  6. \date 28 avril 2011
  7. This Serial library is used to communicate through serial port.
  8.  
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  10. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  11. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
  12. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  13. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  14.  
  15. This is a licence-free software, it can be used by anyone who try to build a better world.
  16. */
  17.  
  18.  
  19. #ifndef SERIALIB_H
  20. #define SERIALIB_H
  21.  
  22.  
  23. // Used for TimeOut operations
  24. #include <time.h>
  25. // Include for windows
  26. #if defined (_WIN32) || defined( _WIN64)
  27. // Accessing to the serial port under Windows
  28. #include <windows.h>
  29. #endif
  30.  
  31. // Include for Linux
  32. #ifdef __linux__
  33. #include <stdlib.h>
  34. #include <sys/types.h>
  35. #include <sys/shm.h>
  36. #include <termios.h>
  37. #include <string.h>
  38. #include <iostream>
  39. // File control definitions
  40. #include <fcntl.h>
  41. #include <unistd.h>
  42. #include <sys/ioctl.h>
  43. #endif
  44.  
  45.  
  46.  
  47. /*! \class serialib
  48. \brief This class can manage a serial port. The class allows basic operations (opening the connection, reading, writing data and closing the connection).
  49. \example Example1.cpp
  50. */
  51.  
  52.  
  53. class serialib
  54. {
  55. public:
  56. // Constructor of the class
  57. serialib ();
  58.  
  59. // Destructor
  60. ~serialib ();
  61.  
  62.  
  63.  
  64. //_________________________________________
  65. // ::: Configuration and initialization :::
  66.  
  67.  
  68. // Open a device
  69. char Open (const char *Device,const unsigned int Bauds);
  70.  
  71. // Close the current device
  72. void Close();
  73.  
  74.  
  75.  
  76. //___________________________________________
  77. // ::: Read/Write operation on characters :::
  78.  
  79.  
  80. // Write a char
  81. char WriteChar (char);
  82.  
  83. // Read a char (with timeout)
  84. char ReadChar (char *pByte,const unsigned int TimeOut_ms=NULL);
  85.  
  86.  
  87.  
  88. //________________________________________
  89. // ::: Read/Write operation on strings :::
  90.  
  91.  
  92. // Write a string
  93. char WriteString (const char *String);
  94. // Read a string (with timeout)
  95. int ReadString ( char *String,
  96. char FinalChar,
  97. unsigned int MaxNbBytes,
  98. const unsigned int TimeOut_ms=NULL);
  99.  
  100.  
  101.  
  102. // _____________________________________
  103. // ::: Read/Write operation on bytes :::
  104.  
  105.  
  106. // Write an array of bytes
  107. char Write (const void *Buffer, const unsigned int NbBytes);
  108.  
  109. // Read an array of byte (with timeout)
  110. int Read (void *Buffer,unsigned int MaxNbBytes,const unsigned int TimeOut_ms=NULL);
  111.  
  112.  
  113. // _________________________
  114. // ::: Special operation :::
  115.  
  116.  
  117. // Empty the received buffer
  118. void FlushReceiver();
  119.  
  120. // Return the number of bytes in the received buffer
  121. int Peek();
  122.  
  123. private:
  124. // Read a string (no timeout)
  125. int ReadStringNoTimeOut (char *String,char FinalChar,unsigned int MaxNbBytes);
  126.  
  127.  
  128. #if defined (_WIN32) || defined( _WIN64)
  129. HANDLE hSerial;
  130. COMMTIMEOUTS timeouts;
  131. #endif
  132. #ifdef __linux__
  133. int fd;
  134. #endif
  135.  
  136. };
  137.  
  138.  
  139.  
  140. /*! \class TimeOut
  141. \brief This class can manage a timer which is used as a timeout.
  142. */
  143. // Class TimeOut
  144. class TimeOut
  145. {
  146. public:
  147.  
  148. // Constructor
  149. TimeOut();
  150.  
  151. // Init the timer
  152. void InitTimer();
  153.  
  154. // Return the elapsed time since initialization
  155. unsigned long int ElapsedTime_ms();
  156.  
  157. private:
  158. struct timeval PreviousTime;
  159. };
  160.  
  161.  
  162.  
  163. /*!
  164. \mainpage serialib class
  165.  
  166. \brief
  167. \htmlonly
  168. <TABLE>
  169. <TR><TD>
  170. <a href="../serialibv1.2.zip" title="Download the serialib class">
  171. <TABLE>
  172. <TR><TD><IMG SRC="download.png" BORDER=0 WIDTH=100> </TD></TR>
  173. <TR><TD><P ALIGN="center">[Download]</P> </TD></TR>
  174. </TABLE>
  175. </A>
  176. </TD>
  177. <TD>
  178. <script type="text/javascript"><!--google_ad_client = "ca-pub-0665655683291467";
  179. google_ad_slot = "0230365165";
  180. google_ad_width = 728;
  181. google_ad_height = 90;
  182. //-->
  183. </script>
  184. <script type="text/javascript"
  185. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  186. </script>
  187. </TD>
  188. </TR>
  189. </TABLE>
  190.  
  191. \endhtmlonly
  192.  
  193. The class serialib offers simple access to the serial port devices for windows and linux. It can be used for any serial device (Built-in serial port, USB to RS232 converter, arduino board or any hardware using or emulating a serial port)
  194. \image html serialib.png
  195. The class can be used under Windows and Linux.
  196. The class allows basic operations like :
  197. - opening and closing connection
  198. - reading data (characters, array of bytes or strings)
  199. - writing data (characters, array of bytes or strings)
  200. - non-blocking functions (based on timeout).
  201.  
  202.  
  203. \author Philippe Lucidarme (University of Angers) <serialib@googlegroups.com>
  204. \date 1th may 2011 (Last update: 25th september 2012)
  205. \version 1.2
  206.  
  207. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  208. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  209. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
  210. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  211. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  212.  
  213. This is a licence-free software, it can be used by anyone who try to build a better world.
  214. */
  215.  
  216.  
  217.  
  218.  
  219. #endif // SERIALIB_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement