You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.4KB

  1. /*
  2. OS Abstraction for serial interface.
  3. Copyright (C) 2006-2014 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #ifndef __GBSER_H
  17. #define __GBSER_H
  18. #define gbser_OK 0
  19. #define gbser_NOTHING -1
  20. #define gbser_TIMEOUT -2
  21. #define gbser_ERROR -3
  22. #if defined(__WIN32__) || defined(__CYGWIN__)
  23. #define WINSERIAL 1
  24. #else
  25. #define POSIXSERIAL 1
  26. #endif
  27. /* Open a serial port. |port_name| is the (platform specific) name
  28. * of the serial device to open. Under WIN32 familiar DOS port names
  29. * ('com1:') are translated into the equivalent name required by
  30. * WIN32
  31. */
  32. void* gbser_init(const char* port_name);
  33. /* Close a serial port
  34. */
  35. void gbser_deinit(void* handle);
  36. /* Set the serial port speed.
  37. */
  38. int gbser_set_speed(void* handle, unsigned speed);
  39. /* Set the serial port speed, start, parity and stop bits */
  40. int gbser_set_port(void* handle, unsigned speed,
  41. unsigned bits,
  42. unsigned parity,
  43. unsigned stop);
  44. /* Set the serial port up by parsing the supplied parameter string.
  45. * Valid parameter strings look like '4800,8,N,1'. Parsing is case-
  46. * insensitive, spaces are allowed around the commas and omitted
  47. * trailing fields will default to '8', 'N' and '1'
  48. */
  49. int gbser_setup(void* handle, const char* spec);
  50. /* Return true if there are characters available on the serial port
  51. */
  52. int gbser_avail(void* handle);
  53. /* Read as many bytes as are available without blocking. At most |len|
  54. * bytes will be read. Returns the number of bytes read or gbser_ERROR if an
  55. * error occurs.
  56. */
  57. int gbser_read(void* handle, void* buf, unsigned len);
  58. /* Read the specified number of bytes. Block until the requested number
  59. * of bytes have been read or the timeout (in ms) is exceeded.
  60. */
  61. int gbser_read_wait(void* handle, void* buf, unsigned len, unsigned ms);
  62. /* Read from the serial port until the specified |eol| character is
  63. * found. Any character matching |discard| will be discarded. To
  64. * read lines terminated by 0x0A0x0D discarding linefeeds use
  65. * gbser_read_line(h, buf, len, 1000, 0x0D, 0x0A);
  66. */
  67. int gbser_read_line(void* handle, void* buf,
  68. unsigned len, unsigned ms,
  69. int eol, int discard);
  70. /* Read a single character from the port, returning immediately if
  71. * none are available. TODO: Define return values
  72. */
  73. int gbser_readc(void* handle);
  74. /* Read a single character from the port, waiting up to |ms|
  75. * milliseconds for a character to be available.
  76. */
  77. int gbser_readc_wait(void* handle, unsigned ms);
  78. /* Discard any pending input on the serial port.
  79. */
  80. int gbser_flush(void* handle);
  81. /* Write |len| bytes from |buf| to the serial port.
  82. */
  83. int gbser_write(void* handle, const void* buf, unsigned len);
  84. /* Write a null terminated string in |str| to the serial
  85. * port.
  86. */
  87. int gbser_print(void* handle, const char* str);
  88. /* Write a single character to the serial port.
  89. */
  90. int gbser_writec(void* handle, int c);
  91. /* Return true if a port name seems to refer to a serial port.
  92. * On Windows this tests the filename (against the regex
  93. * /^(\\\\\.\\\\)?com\d+:?$/i). On Posix it returns the value of
  94. * isatty()
  95. */
  96. int gbser_is_serial(const char* port_name);
  97. /* This isn't part of the above abstraction; it's just a helper for
  98. * the other serial modules in the tree.
  99. *
  100. * Windows does a weird thing with serial ports.
  101. * COM ports 1 - 9 are "COM1:" through "COM9:"
  102. * The one after that is \\.\\com10 - this function tries to plaster over
  103. * that.
  104. * It returns a pointer to a staticly allocated buffer and is therefore not
  105. * thread safe. The buffer pointed to remains valid only until the next
  106. * call to this function.
  107. */
  108. const char* fix_win_serial_name_r(const char* comname, char* obuf, size_t len);
  109. const char* fix_win_serial_name(const char* comname);
  110. #endif /* GBSER_H */