rs232.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2011 Petr Stetiar <[email protected]>, Gaben Ltd.
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use,
  8. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following
  11. * conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #ifndef __LIBRS232_H__
  27. #define __LIBRS232_H__
  28. #include <time.h>
  29. #define RS232_STRLEN 512
  30. #define RS232_STRLEN_DEVICE 256
  31. #ifdef __linux__
  32. #define RS232_PORT_POSIX "/dev/ttyS0"
  33. #else
  34. #define RS232_PORT_POSIX "/dev/cua00"
  35. #endif /* __linux__ */
  36. #define RS232_PORT_WIN32 "COM1"
  37. #if defined(WIN32) || defined(UNDER_CE) || defined(_MSC_VER)
  38. #include "librs232/rs232_windows.h"
  39. #ifdef _MSC_VER
  40. #pragma warning(disable:4996)
  41. #define snprintf _snprintf
  42. #endif
  43. #else
  44. #include "librs232/rs232_posix.h"
  45. #endif
  46. /*Ensure destination string is zero terminated*/
  47. #define strncpyz(dest, src, size) strncpy(dest, src, size); dest[size-1]='\0'
  48. #ifdef RS232_DEBUG
  49. const char* rs232_hex_dump(const void *data, unsigned int len);
  50. const char* rs232_ascii_dump(const void *data, unsigned int len);
  51. #if defined(WIN32) || defined(UNDER_CE)
  52. #include <windows.h>
  53. #define DBG(x, ...) \
  54. { \
  55. SYSTEMTIME t; \
  56. GetLocalTime(&t); \
  57. fprintf(stderr, "[%02d:%02d:%02d.%03d] %s(%d):%s: " x, t.wHour, t.wMinute, \
  58. t.wSecond, t.wMilliseconds, __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); \
  59. }
  60. #else
  61. #define DBG(x, ...) \
  62. { \
  63. time_t now = time(NULL); \
  64. struct tm* t = localtime(&now); \
  65. fprintf(stderr, "[%02d:%02d:%02d] %s(%d):%s: " x, t->tm_hour, t->tm_min, \
  66. t->tm_sec, __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); \
  67. }
  68. #endif /* #if defined(WIN32) || defined(UNDER_CE) */
  69. #else
  70. #define DBG(x, ...)
  71. #define rs232_hex_dump(x, y)
  72. #define rs232_ascii_dump(x, y)
  73. #endif /* RS232_DEBUG */
  74. enum rs232_baud_e {
  75. RS232_BAUD_300,
  76. RS232_BAUD_2400,
  77. RS232_BAUD_4800,
  78. RS232_BAUD_9600,
  79. RS232_BAUD_19200,
  80. RS232_BAUD_38400,
  81. RS232_BAUD_57600,
  82. RS232_BAUD_115200,
  83. RS232_BAUD_460800,
  84. RS232_BAUD_MAX
  85. };
  86. enum rs232_data_e {
  87. RS232_DATA_5,
  88. RS232_DATA_6,
  89. RS232_DATA_7,
  90. RS232_DATA_8,
  91. RS232_DATA_MAX
  92. };
  93. enum rs232_parity_e {
  94. RS232_PARITY_NONE,
  95. RS232_PARITY_ODD,
  96. RS232_PARITY_EVEN,
  97. RS232_PARITY_MAX
  98. };
  99. enum rs232_stop_e {
  100. RS232_STOP_1,
  101. RS232_STOP_2,
  102. RS232_STOP_MAX
  103. };
  104. enum rs232_flow_e {
  105. RS232_FLOW_OFF,
  106. RS232_FLOW_HW,
  107. RS232_FLOW_XON_XOFF,
  108. RS232_FLOW_MAX
  109. };
  110. enum rs232_status_e {
  111. RS232_PORT_CLOSED,
  112. RS232_PORT_OPEN,
  113. };
  114. enum rs232_dtr_e {
  115. RS232_DTR_OFF,
  116. RS232_DTR_ON,
  117. RS232_DTR_MAX
  118. };
  119. enum rs232_rts_e {
  120. RS232_RTS_OFF,
  121. RS232_RTS_ON,
  122. RS232_RTS_MAX
  123. };
  124. struct rs232_port_t {
  125. char dev[RS232_STRLEN_DEVICE+1];
  126. void *pt; /* platform specific stuff */
  127. enum rs232_baud_e baud;
  128. enum rs232_data_e data;
  129. enum rs232_stop_e stop;
  130. enum rs232_flow_e flow;
  131. enum rs232_parity_e parity;
  132. enum rs232_status_e status;
  133. enum rs232_dtr_e dtr;
  134. enum rs232_rts_e rts;
  135. };
  136. enum rs232_error_e {
  137. RS232_ERR_NOERROR,
  138. RS232_ERR_UNKNOWN,
  139. RS232_ERR_OPEN,
  140. RS232_ERR_CLOSE,
  141. RS232_ERR_FLUSH,
  142. RS232_ERR_CONFIG,
  143. RS232_ERR_READ,
  144. RS232_ERR_WRITE,
  145. RS232_ERR_SELECT,
  146. RS232_ERR_TIMEOUT,
  147. RS232_ERR_IOCTL,
  148. RS232_ERR_PORT_CLOSED,
  149. RS232_ERR_BREAK,
  150. RS232_ERR_FRAME,
  151. RS232_ERR_PARITY,
  152. RS232_ERR_RXOVERFLOW,
  153. RS232_ERR_OVERRUN,
  154. RS232_ERR_MAX
  155. };
  156. #if (defined(WIN32) || defined(UNDER_CE)) && !defined(RS232_STATIC)
  157. #ifdef RS232_EXPORT
  158. #define RS232_LIB __declspec(dllexport)
  159. #else
  160. #define RS232_LIB __declspec(dllimport)
  161. #endif
  162. #else
  163. #define RS232_LIB
  164. #endif
  165. RS232_LIB struct rs232_port_t * rs232_init(void);
  166. RS232_LIB void rs232_end(struct rs232_port_t *p);
  167. RS232_LIB unsigned int rs232_open(struct rs232_port_t *p);
  168. RS232_LIB unsigned int rs232_port_open(struct rs232_port_t *p);
  169. RS232_LIB unsigned int rs232_close(struct rs232_port_t *p);
  170. RS232_LIB unsigned int rs232_flush(struct rs232_port_t *p);
  171. RS232_LIB void rs232_set_device(struct rs232_port_t *p, const char *device);
  172. RS232_LIB unsigned int rs232_set_baud(struct rs232_port_t *p, unsigned int baud);
  173. RS232_LIB unsigned int rs232_set_stop(struct rs232_port_t *p, unsigned int stop);
  174. RS232_LIB unsigned int rs232_set_data(struct rs232_port_t *p, unsigned int data);
  175. RS232_LIB unsigned int rs232_set_parity(struct rs232_port_t *p, unsigned int parity);
  176. RS232_LIB unsigned int rs232_set_flow(struct rs232_port_t *p, unsigned int flow);
  177. RS232_LIB unsigned int rs232_set_dtr(struct rs232_port_t *p, unsigned int dtr);
  178. RS232_LIB unsigned int rs232_set_rts(struct rs232_port_t *p, unsigned int rts);
  179. RS232_LIB const char * rs232_get_device(struct rs232_port_t *p);
  180. RS232_LIB unsigned int rs232_get_baud(struct rs232_port_t *p);
  181. RS232_LIB unsigned int rs232_get_stop(struct rs232_port_t *p);
  182. RS232_LIB unsigned int rs232_get_data(struct rs232_port_t *p);
  183. RS232_LIB unsigned int rs232_get_parity(struct rs232_port_t *p);
  184. RS232_LIB unsigned int rs232_get_flow(struct rs232_port_t *p);
  185. RS232_LIB unsigned int rs232_get_dtr(struct rs232_port_t *p);
  186. RS232_LIB unsigned int rs232_get_rts(struct rs232_port_t *p);
  187. RS232_LIB unsigned int rs232_read(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *read_len);
  188. RS232_LIB unsigned int rs232_read_timeout(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *read_len, unsigned int timeout);
  189. RS232_LIB unsigned int rs232_read_timeout_forced(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *read_len, unsigned int timeout);
  190. RS232_LIB unsigned int rs232_write(struct rs232_port_t *p, const unsigned char *buf, unsigned int buf_len, unsigned int *write_len);
  191. RS232_LIB unsigned int rs232_write_timeout(struct rs232_port_t *p, const unsigned char *buf, unsigned int buf_len, unsigned int *write_len, unsigned int timeout);
  192. RS232_LIB unsigned int rs232_in_queue(struct rs232_port_t *p, unsigned int *in_bytes);
  193. RS232_LIB void rs232_in_queue_clear(struct rs232_port_t *p);
  194. RS232_LIB const char * rs232_to_string(struct rs232_port_t *p);
  195. RS232_LIB const char * rs232_strerror(unsigned int error);
  196. RS232_LIB const char * rs232_strbaud(unsigned int baud);
  197. RS232_LIB const char * rs232_strdata(unsigned int data);
  198. RS232_LIB const char * rs232_strparity(unsigned int parity);
  199. RS232_LIB const char * rs232_strstop(unsigned int stop);
  200. RS232_LIB const char * rs232_strflow(unsigned int flow);
  201. RS232_LIB const char * rs232_strdtr(unsigned int dtr);
  202. RS232_LIB const char * rs232_strrts(unsigned int rts);
  203. RS232_LIB unsigned int rs232_fd(struct rs232_port_t *p);
  204. #endif /* __LIBRS232_H__ */