-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_lib.h
313 lines (286 loc) · 10.3 KB
/
serial_lib.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*********************************************************************************************//**
* @file PortableSerialLib.h
*
*
* @author Copyright (c) 2012 René Staffen [email protected]
* @version $Id: PortableSerialLib.h 14 2012-03-09 21:50:57Z Vlad $
*
* @copyright 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.
* For a full text of the license look at the enclosed gpl2.txt
* or at http://www.gnu.org/licenses/gpl-2.0.html
*
* @details
*
*
*//*********************************************************************************************/
#ifndef SERIAL_LIB_H
#define SERIAL_LIB_H
#include <inttypes.h>
// Handle that represents a serial port object
typedef struct PSerLibHandleInternal_t *PSerLibHandle_t;
// Indicates a bad Handle
#define PSL_NOPORT_HANDLE ((PSerLibHandle_t)(0))
/**
* predefined baud rate constants \see PSerLib_setParams
* some may not supported by different platforms
*/
typedef enum PSL_Baudrates_e {
PSL_BR_50,
PSL_BR_75,
PSL_BR_110,
PSL_BR_134,
PSL_BR_150,
PSL_BR_200,
PSL_BR_300,
PSL_BR_600,
PSL_BR_1200,
PSL_BR_1800,
PSL_BR_2400,
PSL_BR_4800,
PSL_BR_9600,
PSL_BR_14400,
PSL_BR_19200,
PSL_BR_28800,
PSL_BR_38400,
PSL_BR_56000,
PSL_BR_57600,
PSL_BR_115200,
PSL_BR_128000,
PSL_BR_230400,
PSL_BR_256000,
PSL_BR_460800,
PSL_BR_500000,
PSL_BR_576000,
PSL_BR_921600,
PSL_BR_1000000,
PSL_BR_1152000,
PSL_BR_1500000,
PSL_BR_2000000,
PSL_BR_2500000,
PSL_BR_3000000,
PSL_BR_3500000,
PSL_BR_4000000,
PSL_BR_CONSTANTS_COUNT
} PSL_Baudrates_e;
// Constants for configuring number of data bits per transmitted character, see PSerLib_setParams
typedef enum PSL_NumDataBits_e {
PSL_DB_5,
PSL_DB_6,
PSL_DB_7,
PSL_DB_8,
PSL_DB_CONSTANTS_COUNT
} PSL_NumDataBits_e;
// Constants for parity settings, see PSerLib_setParams
typedef enum PSL_Parity_e {
PSL_P_none,
PSL_P_odd,
PSL_P_even,
PSL_P_mark,
PSL_P_space,
PSL_P_CONSTANTS_COUNT
} PSL_Parity_e;
// Constants for configuring number of stop bits per transmitted character, see PSerLib_setParams
typedef enum PSL_StopBits_e {
PSL_SB_1,
PSL_SB_1p5,
PSL_SB_2,
PSL_SB_CONSTANTS_COUNT
} PSL_StopBits_e;
// Constants for configuring flow control, see PSerLib_setParams
typedef enum PSL_FlowControl_e {
PSL_FC_none,
PSL_FC_rts_cts,
PSL_FC_xon_xoff,
PSL_FC_rts_cts__xon_xoff,
PSL_FC_CONSTANTS_COUNT
} PSL_FlowControl_e;
// Error codes that may be returned by the functions, see PSerLib_getErrorMessage
typedef enum PSL_ErrorCodes_e {
PSL_ERROR_none,
PSL_ERROR_couldNotOpen,
PSL_ERROR_invalidHandle,
PSL_ERROR_unsupportedBitrate,
PSL_ERROR_unsupportedConfig,
PSL_ERROR_configurePort,
PSL_ERROR_notYetImplemented,
PSL_ERROR_bufferToSmall,
PSL_ERROR_writeDataFailed,
PSL_ERROR_readDataFailed,
PSL_ERROR_START_LINUX_SPECIFIC = 2000,
PSL_ERROR_couldNotReadProcTtyFile = PSL_ERROR_START_LINUX_SPECIFIC,
PSL_ERROR_couldParseProcTtyFile,
PSL_ERROR_couldNotReadSysClassTty,
PSL_ERROR_CODES_COUNT
} PSL_ErrorCodes_e;
/**
* @brief translates an error code in something printable
* @param i_errorCode the error code to translate
* @returns pointer to a null-terminated string that gives a short description of the error
* @ingroup PortUtility
*/
const char *PSerLib_getErrorMessage(PSL_ErrorCodes_e i_errorCode);
/**
* @brief returns a list with device names of available serial ports
*
* @param o_names buffer for output\n
* Each device name is quit a \\0 character.
* An empty name indicates the end of the list.\n
* Look at details - section for an example how to use the output.
* @param i_maxlen length of buffer
* @param o_numPortsFound pointer to variable that receives the number of ports found
*
* @returns PSL_ERROR_none if successful else another error code
*
* @details Fiddeling out devices may be done in following fashion:
\code{.c}
char buff[1000];
char *iterator = buff;
int n;
PSerLib_getAvailablePorts(buff, sizeof(buff), &n);
printf("found %i devices:\n", n);
for(;*iterator; iterator += strlen(iterator) + 1) {
printf("%s\n", iterator);
}
\endcode
* @ingroup PortUtility
*/
PSL_ErrorCodes_e PSerLib_getAvailablePorts(char **(*o_names), int* o_numPortsFound);
/**
* tries to open the specified serial port
* @param i_portName name of the port to open\n
* These names are indeed some kind of platform specific - sorry.
* But you may get a list of possible ports from \see PSerLib_getAvailablePorts.
* @param o_handle pointer to a handle variable that receives the handle to the opened
* port on success or PSL_NOPORT_HANDLE if opening failed
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortHandling
*/
PSL_ErrorCodes_e PSerLib_open(const char *i_portName, PSerLibHandle_t *o_handle);
/**
* @brief closes and deinitializes the port
*
* @param io_portH pointer to port handle to close.
* The handle will be invalidated.
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortHandling
*/
PSL_ErrorCodes_e PSerLib_close(PSerLibHandle_t *io_portH);
/**
* @brief sets the port to the given configuration
* @attention currently not all baud rates are supported on
* linux (have to figure out how to set custom baud rates that are not defined in termios)
*
* @todo support all enumerated and custom baud rates
*
* @param io_port handle of port to be configured
* @param i_baudrate baudrate if argument is less than PSL_BR_CONSTANTS_COUNT
* it is interpreted as one of the enumerated constants.
* If it is larger it is interpreted as baudrate.
* @param i_bits number of data bits
* @param i_parity parity
* @param i_stopbits number of stopbits
* @param i_flowControl flow control to use
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortHandling
*/
PSL_ErrorCodes_e PSerLib_setParams(PSerLibHandle_t io_port,
PSL_Baudrates_e i_baudrate,
PSL_NumDataBits_e i_bits,
PSL_Parity_e i_parity,
PSL_StopBits_e i_stopbits,
PSL_FlowControl_e i_flowControl);
/**
* @todo specify
* @ingroup PortHandling
*/
PSL_ErrorCodes_e PSerLib_setTimeOuts(PSerLibHandle_t io_port);
/**
* @brief outputs the given character to the port
*
* @param io_port handle of port to write to
* @param c character to send
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortWrite
*/
PSL_ErrorCodes_e PSerLib_putc(PSerLibHandle_t io_port, char c);
/**
* @brief outputs the given string to the port
*
* @param io_port handle of port to write to
* @param i_str null terminated string
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortWrite
*/
PSL_ErrorCodes_e PSerLib_puts(PSerLibHandle_t io_port, const char *i_str);
/**
* @brief outputs the given data to the port
*
* @param io_port handle of port to write to
* @param i_data pointer to data to output
* @param i_dataLen number of bytes to read from data pointer
* @param o_bytesWritten number of bytes actual written
* (May differ because of time out or error)\n
* Can be NULL if not required.
*
* @remark Sending/receiving of binary data may be incompatible with
* connections that base on XON/XOFF flow control
* because XON/XOFF are special bytes.
* If you use XON/XOFF flow control then ensure that these
* bytes cannot appear in i_data else your communication may screw up.
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortWrite
*/
PSL_ErrorCodes_e PSerLib_writeBinaryData(PSerLibHandle_t io_port,
const uint8_t *i_data,
int i_dataLen,
int *o_bytesWritten);
/**
* @brief reads data from the port
*
* @param io_port handle of port to read from
* @param o_string pointer to buffer where the data should be stored.
* @param i_bufferLength number of bytes the buffer can take.
* Remember that the buffer also have to take the end
* character and terminating \\0 character
* @param i_endLineChars \\0 terminated string, that contains characters
* that indicates line end.
* The line end character will also be copied into
* output buffer.
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortRead
*/
PSL_ErrorCodes_e PSerLib_readLine(PSerLibHandle_t io_port,
char *o_string,
int i_bufferLength,
const char *i_endLineChars);
/**
* @brief reads data from the port
*
* @param io_port handle of port to read from
* @param o_data pointer to buffer where the data should be stored.
* Ensure that the buffer can take at least i_dataToRead bytes.
* @param i_dataToRead number of bytes to read
* @param o_bytesRead number of bytes actual read
* (May differ because of time out or error)\n
* Can be NULL if not required.
*
* @remark see remarks of PSerLib_writeBinaryData
*
* @returns PSL_ERROR_none if successful else another error code
* @ingroup PortRead
*/
PSL_ErrorCodes_e PSerLib_readBinaryData(PSerLibHandle_t io_port,
uint8_t *o_data,
int i_dataToRead,
int *o_bytesRead);
#endif