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