/
serial.c
406 lines (317 loc) · 7.08 KB
/
serial.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "base.h"
#include "serial.h"
#include "util.h"
static int _open_device(struct serialsosurce *ss, char *device)
{
assert(ss);
assert(device);
assert(strlen(device) < 255);
memset(ss, 0, sizeof(*ss));
strcpy(ss->device, device);
ss->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (ss->fd == -1) {
DEBUG("fd = %d, %s\n", ss->fd, strerror(errno));
return -1;
}
ss->req = malloc(sizeof(ptable));
create(ss->req);
return 0;
}
static int _set_device(struct serialsosurce *ss, int baude, char bits, char parity, char stop)
{
int fd = ss->fd;
struct termios options;
if (tcgetattr(fd, &options) != 0) {
return -1;
}
bzero(&options, sizeof(struct termios));
switch (baude) {
case 9600:
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
break;
default:
return -1;
}
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input */
options.c_oflag &= ~OPOST; /*Output */
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
switch (parity) {
case 'N':
case 'n':
case 'S':
case 's':
options.c_cflag &= ~PARENB;
break;
case 'E':
case 'e':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
break;
case 'O':
case 'o':
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
break;
default:
return -1;
}
if (options.c_cflag & PARENB) {
// Enable Parity
options.c_iflag |= (INPCK | ISTRIP);
}
switch (stop) {
case '1':
options.c_cflag &= ~CSTOPB;
break;
default:
return -1;
}
switch (bits) {
case '7':
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
break;
case '8':
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
break;
default:
return -1;
}
options.c_cc[VTIME] = 0; /* 设置超时15 seconds */
options.c_cc[VMIN] = 1; /* define the minimum bytes data to be readed */
tcflush(fd, TCIFLUSH);
// set Attr
if (tcsetattr(fd, TCSANOW, &options) != 0) {
return -1;
}
return 0;
}
static int _close_device(struct serialsosurce *ss)
{
assert(ss);
if (ss->fd > 0) close(ss->fd);
if (ss->req) cleanup(ss->req);
memset(ss, 0, sizeof(struct serialsosurce));
return 0;
}
static int _check_command(char *buf, int sz)
{
// TODO: implementation totally check
int ret = 0;
ret |= (buf[0] == 0x02);
ret |= (buf[1] == 0x30 || buf[1] == 0x31);
return ret;
}
void *thread_serialcomm(void *parm)
{
struct serialsosurce *ss = (struct serialsosurce*)parm;
RESTART:
while (1) {
usleep(1000);
struct serialcommand *sc = get_data(ss->req, NULL);
int ret;
// clean serial
// struct timeval tv_clean;
// memset(&tv_clean, 0, sizeof(struct timeval));
// tv_clean.tv_sec = 0;
// tv_clean.tv_usec = 0;
//
// fd_set rfds_clean;
// FD_ZERO(&rfds_clean);
// FD_SET(ss->fd, &rfds_clean);
//
// int ret = select(ss->fd+1, &rfds_clean, NULL, NULL, &tv_clean);
// if (ret == -1) {
// DEBUG("select error\n");
// free(sc);
// goto RESTART;
// } else if (ret == 0) {
// DEBUG("never happen\n");
// free(sc);
// goto RESTART;
// } else {
// char tmp[4096];
// read(ss->fd, tmp, 4096);
// DEBUG("clean plc serial buffer\n");
// }
if (_check_command(sc->buf, sc->sz) == 0) {
DEBUG("cmd error\n");
free(sc);
goto RESTART;
}
int num = 0;
if (sc->buf[1] == 0x30) {
// DATA size + STX(1 byte) + ETX(1 byte) + SUM(2 byte)
num = ((sc->buf[6]-'0')*10 + (sc->buf[7]-'0'))*2+4;
} else {
num = 1;
}
if (num > 64*2+4) {
free(sc);
goto RESTART;
}
// write command
ret = safe_write(ss->fd, sc->buf, sc->sz);
if (ret < 0) {
free(sc);
goto RESTART;
}
ss->stats.n_send++;
char resp[4096];
memset(resp, 0, sizeof(resp));
int sz = 0;
char *p_resp = resp;
for (;;) {
struct timeval tv;
memset(&tv, 0, sizeof(struct timeval));
tv.tv_sec = 5;
tv.tv_usec = 0;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(ss->fd, &rfds);
ret = select(ss->fd+1, &rfds, NULL, NULL, &tv);
if (ret == -1) {
DEBUG("select error\n");
free(sc);
goto RESTART;
} else if (ret == 0) {
DEBUG("time expired\n");
free(sc);
goto RESTART;
} else {
int cnt = read(ss->fd, p_resp, num);
if (cnt == 0) {
DEBUG("serial error\n");
free(sc);
goto RESTART;
}
num -= cnt;
p_resp += cnt;
sz += cnt;
if (num == 0) {
// call cb
ss->stats.n_recv++;
sc->cb(sc->cxt, resp, sz);
break;
}
}
}
// free something
free(sc);
}
return (void *)NULL;
}
int serial_start(struct serialsosurce *ss)
{
int ret;
ret = _open_device(ss, "/dev/ttyUSB0");
assert(ret == 0);
ret = _set_device(ss, 9600, '7', 'E', '1');
assert(ret == 0);
pthread_t tid_serial;
ret = pthread_create(&tid_serial, NULL, thread_serialcomm, (void *)ss);
assert(ret == 0);
ss->tid_serial = tid_serial;
return 0;
}
int serial_stop(struct serialsosurce *ss)
{
pthread_cancel(ss->tid_serial);
pthread_join(ss->tid_serial, NULL);
_close_device(ss);
return 0;
}
int serial_command(struct serialsosurce *ss, struct serialcommand *sc)
{
assert(ss);
assert(sc);
struct serialcommand *local_sc = malloc(sizeof(struct serialcommand));
assert(sc);
local_sc->cxt = sc->cxt;
local_sc->cb = sc->cb;
local_sc->sz = sc->sz;
memcpy(local_sc->buf, sc->buf, sizeof(local_sc->buf));
// TODO: let write command has high priority
put_data(ss->req, (void *)local_sc, 1/*Priority*/);
return 0;
}
static char _getAscii(int i)
{
if (i >=0 && i <= 9) return i+'0';
else if (i>=10 && i <=15) return (i-10)+'A';
}
char _getAddressAscii(int address, char buf[4])
{
int x = address * 2 + 0x1000;
int i, j, m, n;
i = x / (16 * 16 * 16);
j = (x / (16 * 16)) % 16;
m = x - i*(16*16*16) - j*(16*16);
m = m / 16;
n = x % 16;
buf[0] = _getAscii(i);
buf[1] = _getAscii(j);
buf[2] = _getAscii(m);
buf[3] = _getAscii(n);
//printf("add2ascii:%d->%02x, %02x, %02x, %02x\n", address, buf[0], buf[1], buf[2], buf[3]);
}
int getReadCommandFrame (char *buf, int *sz, int address, int num)
{
if (buf == NULL || sz == NULL ||
(address < 0 || address > 255) || num < 0)
return -1;
buf[0] = 0x02;
buf[1] = '0';
_getAddressAscii(address, &buf[2]);
num = num *2;
buf[6] = _getAscii(num/10);
buf[7] = _getAscii(num%10);
buf[8] = 0x03;
int i, sum = 0;
for (i = 1; i <= 8; i++)
sum += buf[i];
i = sum&0xFF;
buf[9] = _getAscii(i/16);
buf[10] = _getAscii(i%16);
*sz = 11;
for (i = 0; i < *sz; i++) {
// printf("%02x ", buf[i]);
}
// printf("\n");
return 0;
}
int getWriteCommandFrame(char *buf, int *sz, int address, int num, char *data)
{
if (buf == NULL || sz == NULL ||
(address < 0 || address > 255) || num < 0)
return -1;
buf[0] = 0x02;
buf[1] = '1';
_getAddressAscii(address, &buf[2]);
num = num*2;
buf[6] = _getAscii(num/10);
buf[7] = _getAscii(num%10);
int i;
for (i = 0; i < num*2; i+=4) {
buf[8+i] = data[i+2];
buf[8+i+1] = data[i+3];
buf[8+i+2] = data[i];
buf[8+i+3] = data[i+1];
}
buf[8+num*2] = 0x03;
int sum = 0;
for (i = 1; i <= 8+num*2; i++)
sum += buf[i];
i = sum&0xFF;
buf[8+num*2+1] = _getAscii(i/16);
buf[8+num*2+2] = _getAscii(i%16);
*sz = 8+num*2+3;
for (i = 0; i < *sz; i++) {
// printf("%02x ", buf[i]);
}
// printf("\n");
return 0;
}