2
0

rs232.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include "librs232/rs232.h"
  31. static const char *
  32. rs232_baud[] = {
  33. "300",
  34. "2400",
  35. "4800",
  36. "9600",
  37. "19200",
  38. "38400",
  39. "57600",
  40. "115200",
  41. "460800",
  42. };
  43. static const char *
  44. rs232_data[] = {
  45. "5",
  46. "6",
  47. "7",
  48. "8",
  49. };
  50. static const char *
  51. rs232_parity[] = {
  52. "none",
  53. "odd",
  54. "even",
  55. };
  56. static const char *
  57. rs232_stop[] = {
  58. "1",
  59. "2",
  60. };
  61. static const char *
  62. rs232_flow[] = {
  63. "off",
  64. "hardware",
  65. "xon/xoff",
  66. };
  67. static const char *
  68. rs232_dtr[] = {
  69. "off",
  70. "on",
  71. };
  72. static const char *
  73. rs232_rts[] = {
  74. "off",
  75. "on",
  76. };
  77. static const char *
  78. rs232_error[] = {
  79. "no error",
  80. "unknown error",
  81. "open error",
  82. "close error",
  83. "flush error",
  84. "get/set settings error",
  85. "read error",
  86. "write error",
  87. "select error",
  88. "timeout error",
  89. "ioctl error",
  90. "port closed error",
  91. };
  92. #ifdef RS232_DEBUG
  93. const char *
  94. rs232_hex_dump(const void *data, unsigned int len)
  95. {
  96. static char string[1024] = {0};
  97. unsigned char *d = (unsigned char *) data;
  98. unsigned int i;
  99. for (i = 0; len--; i += 3) {
  100. if (i >= sizeof(string) - 4)
  101. break;
  102. snprintf(string+i, 4, "%02x ", *d++);
  103. }
  104. string[i] = '\0';
  105. return string;
  106. }
  107. const char *
  108. rs232_ascii_dump(const void *data, unsigned int len)
  109. {
  110. static char string[1024] = {0};
  111. unsigned char *d = (unsigned char *) data;
  112. unsigned int i;
  113. unsigned char c;
  114. for (i = 0; len--; i++) {
  115. if (i >= sizeof(string) - 2)
  116. break;
  117. c = *d++;
  118. snprintf(string+i, 2, "%c", isprint(c) ? c : '.');
  119. }
  120. string[i] = '\0';
  121. return string;
  122. }
  123. #endif
  124. RS232_LIB const char *
  125. rs232_strerror(unsigned int error)
  126. {
  127. DBG("error=%d\n", error);
  128. if (error >= RS232_ERR_MAX)
  129. return NULL;
  130. return rs232_error[error];
  131. }
  132. RS232_LIB const char *
  133. rs232_strbaud(unsigned int baud)
  134. {
  135. DBG("baud=%d\n", baud);
  136. if (baud >= RS232_BAUD_MAX)
  137. return NULL;
  138. return rs232_baud[baud];
  139. }
  140. RS232_LIB const char *
  141. rs232_strdata(unsigned int data)
  142. {
  143. DBG("data=%d\n", data);
  144. if (data >= RS232_DATA_MAX)
  145. return NULL;
  146. return rs232_data[data];
  147. }
  148. RS232_LIB const char *
  149. rs232_strparity(unsigned int parity)
  150. {
  151. DBG("parity=%d\n", parity);
  152. if (parity >= RS232_PARITY_MAX)
  153. return NULL;
  154. return rs232_parity[parity];
  155. }
  156. RS232_LIB const char *
  157. rs232_strstop(unsigned int stop)
  158. {
  159. DBG("stop=%d\n", stop);
  160. if (stop >= RS232_STOP_MAX)
  161. return NULL;
  162. return rs232_stop[stop];
  163. }
  164. RS232_LIB const char *
  165. rs232_strflow(unsigned int flow)
  166. {
  167. DBG("flow=%d\n", flow);
  168. if (flow >= RS232_FLOW_MAX)
  169. return NULL;
  170. return rs232_flow[flow];
  171. }
  172. RS232_LIB const char *
  173. rs232_strdtr(unsigned int dtr)
  174. {
  175. DBG("dtr=%d\n", dtr);
  176. if (dtr >= RS232_DTR_MAX)
  177. return NULL;
  178. return rs232_dtr[dtr];
  179. }
  180. RS232_LIB const char *
  181. rs232_strrts(unsigned int rts)
  182. {
  183. DBG("rts=%d\n", rts);
  184. if (rts >= RS232_RTS_MAX)
  185. return NULL;
  186. return rs232_rts[rts];
  187. }
  188. RS232_LIB const char *
  189. rs232_to_string(struct rs232_port_t *p)
  190. {
  191. static char str[RS232_STRLEN+1];
  192. DBG("p=%p\n", (void *)p);
  193. if (p == NULL)
  194. return NULL;
  195. snprintf(str, RS232_STRLEN, "device: %s, baud: %s, data bits: %s,"
  196. " parity: %s, stop bits: %s,"
  197. " flow control: %s",
  198. p->dev,
  199. rs232_strbaud(p->baud),
  200. rs232_strdata(p->data),
  201. rs232_strparity(p->parity),
  202. rs232_strstop(p->stop),
  203. rs232_strflow(p->flow));
  204. return str;
  205. }
  206. RS232_LIB const char *
  207. rs232_get_device(struct rs232_port_t *p)
  208. {
  209. DBG("p=%p device: %s\n", (void *)p, p->dev);
  210. return p->dev;
  211. }
  212. RS232_LIB unsigned int
  213. rs232_get_baud(struct rs232_port_t *p)
  214. {
  215. DBG("p=%p baud: %d\n", (void *)p, p->baud);
  216. return p->baud;
  217. }
  218. RS232_LIB unsigned int
  219. rs232_get_stop(struct rs232_port_t *p)
  220. {
  221. DBG("p=%p baud: %d\n", (void *)p, p->stop);
  222. return p->stop;
  223. }
  224. RS232_LIB unsigned int
  225. rs232_get_data(struct rs232_port_t *p)
  226. {
  227. DBG("p=%p data: %d\n", (void *)p, p->data);
  228. return p->data;
  229. }
  230. RS232_LIB unsigned int
  231. rs232_get_parity(struct rs232_port_t *p)
  232. {
  233. DBG("p=%p parity: %d\n", (void *)p, p->parity);
  234. return p->parity;
  235. }
  236. RS232_LIB unsigned int
  237. rs232_get_flow(struct rs232_port_t *p)
  238. {
  239. DBG("p=%p flow: %d\n", (void *)p, p->flow);
  240. return p->flow;
  241. }
  242. RS232_LIB unsigned int
  243. rs232_port_open(struct rs232_port_t *p)
  244. {
  245. DBG("p=%p p->status=%d\n", (void *)p, p->status);
  246. return p->status;
  247. }
  248. RS232_LIB unsigned int
  249. rs232_get_dtr(struct rs232_port_t *p)
  250. {
  251. DBG("p=%p dtr: %d\n", (void *)p, p->dtr);
  252. return p->dtr;
  253. }
  254. RS232_LIB unsigned int
  255. rs232_get_rts(struct rs232_port_t *p)
  256. {
  257. DBG("p=%p rts: %d\n", (void *)p, p->rts);
  258. return p->rts;
  259. }