-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrs232.c
314 lines (259 loc) · 5.49 KB
/
rs232.c
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
314
/*
* Copyright (c) 2011 Petr Stetiar <[email protected]>, Gaben Ltd.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rs232.h"
static const char *
rs232_baud[] = {
"300",
"2400",
"4800",
"9600",
"19200",
"38400",
"57600",
"115200",
"460800",
};
static const char *
rs232_data[] = {
"5",
"6",
"7",
"8",
};
static const char *
rs232_parity[] = {
"none",
"odd",
"even",
};
static const char *
rs232_stop[] = {
"1",
"2",
};
static const char *
rs232_flow[] = {
"off",
"hardware",
"xon/xoff",
};
static const char *
rs232_dtr[] = {
"off",
"on",
};
static const char *
rs232_rts[] = {
"off",
"on",
};
static const char *
rs232_error[] = {
"no error",
"unknown error",
"open error",
"close error",
"flush error",
"get/set settings error",
"read error",
"write error",
"select error",
"timeout error",
"ioctl error",
"port closed error",
};
#ifdef RS232_DEBUG
const char *
rs232_hex_dump(const void *data, unsigned int len)
{
static char string[1024] = {0};
unsigned char *d = (unsigned char *) data;
unsigned int i;
for (i = 0; len--; i += 3) {
if (i >= sizeof(string) - 4)
break;
snprintf(string+i, 4, "%02x ", *d++);
}
string[i] = '\0';
return string;
}
const char *
rs232_ascii_dump(const void *data, unsigned int len)
{
static char string[1024] = {0};
unsigned char *d = (unsigned char *) data;
unsigned int i;
unsigned char c;
for (i = 0; len--; i++) {
if (i >= sizeof(string) - 2)
break;
c = *d++;
snprintf(string+i, 2, "%c", isprint(c) ? c : '.');
}
string[i] = '\0';
return string;
}
#endif
RS232_LIB const char *
rs232_strerror(unsigned int error)
{
DBG("error=%d\n", error);
if (error >= RS232_ERR_MAX)
return NULL;
return rs232_error[error];
}
RS232_LIB const char *
rs232_strbaud(unsigned int baud)
{
DBG("baud=%d\n", baud);
if (baud >= RS232_BAUD_MAX)
return NULL;
return rs232_baud[baud];
}
RS232_LIB const char *
rs232_strdata(unsigned int data)
{
DBG("data=%d\n", data);
if (data >= RS232_DATA_MAX)
return NULL;
return rs232_data[data];
}
RS232_LIB const char *
rs232_strparity(unsigned int parity)
{
DBG("parity=%d\n", parity);
if (parity >= RS232_PARITY_MAX)
return NULL;
return rs232_parity[parity];
}
RS232_LIB const char *
rs232_strstop(unsigned int stop)
{
DBG("stop=%d\n", stop);
if (stop >= RS232_STOP_MAX)
return NULL;
return rs232_stop[stop];
}
RS232_LIB const char *
rs232_strflow(unsigned int flow)
{
DBG("flow=%d\n", flow);
if (flow >= RS232_FLOW_MAX)
return NULL;
return rs232_flow[flow];
}
RS232_LIB const char *
rs232_strdtr(unsigned int dtr)
{
DBG("dtr=%d\n", dtr);
if (dtr >= RS232_DTR_MAX)
return NULL;
return rs232_dtr[dtr];
}
RS232_LIB const char *
rs232_strrts(unsigned int rts)
{
DBG("rts=%d\n", rts);
if (rts >= RS232_RTS_MAX)
return NULL;
return rs232_rts[rts];
}
RS232_LIB const char *
rs232_to_string(struct rs232_port_t *p)
{
static char str[RS232_STRLEN+1];
DBG("p=%p\n", (void *)p);
if (p == NULL)
return NULL;
snprintf(str, RS232_STRLEN, "device: %s, baud: %s, data bits: %s,"
" parity: %s, stop bits: %s,"
" flow control: %s",
p->dev,
rs232_strbaud(p->baud),
rs232_strdata(p->data),
rs232_strparity(p->parity),
rs232_strstop(p->stop),
rs232_strflow(p->flow));
return str;
}
RS232_LIB const char *
rs232_get_device(struct rs232_port_t *p)
{
DBG("p=%p device: %s\n", (void *)p, p->dev);
return p->dev;
}
RS232_LIB unsigned int
rs232_get_baud(struct rs232_port_t *p)
{
DBG("p=%p baud: %d\n", (void *)p, p->baud);
return p->baud;
}
RS232_LIB unsigned int
rs232_get_stop(struct rs232_port_t *p)
{
DBG("p=%p baud: %d\n", (void *)p, p->stop);
return p->stop;
}
RS232_LIB unsigned int
rs232_get_data(struct rs232_port_t *p)
{
DBG("p=%p data: %d\n", (void *)p, p->data);
return p->data;
}
RS232_LIB unsigned int
rs232_get_parity(struct rs232_port_t *p)
{
DBG("p=%p parity: %d\n", (void *)p, p->parity);
return p->parity;
}
RS232_LIB unsigned int
rs232_get_flow(struct rs232_port_t *p)
{
DBG("p=%p flow: %d\n", (void *)p, p->flow);
return p->flow;
}
RS232_LIB unsigned int
rs232_port_open(struct rs232_port_t *p)
{
DBG("p=%p p->status=%d\n", (void *)p, p->status);
return p->status;
}
RS232_LIB unsigned int
rs232_get_dtr(struct rs232_port_t *p)
{
DBG("p=%p dtr: %d\n", (void *)p, p->dtr);
return p->dtr;
}
RS232_LIB unsigned int
rs232_get_rts(struct rs232_port_t *p)
{
DBG("p=%p rts: %d\n", (void *)p, p->rts);
return p->rts;
}