-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrol.c
More file actions
483 lines (407 loc) · 10.4 KB
/
control.c
File metadata and controls
483 lines (407 loc) · 10.4 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/time.h>
#include <dirent.h>
//add include to dp server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdbool.h>
#define SERVER_PORT 5000
#define BUFFER_SIZE 256
///////
#define SETBITSPEED(opt, s) \
do \
{ \
cfsetispeed(&(opt), B##s); \
cfsetospeed(&(opt), B##s); \
} while(0)
#define CMD_PREFIX 0xeeee
#define CMD_SUFFIX 0xdddddddd
//char *dev[]={"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2"};
char file_dev[10][128];
//#define g_countTTYUSB 4 //sizeof(dev[0])
int g_fd[10] = {0}; //For serial device
unsigned char g_ifdebug = 0;
int g_countTTYUSB = 0;
///upd data
struct sockaddr_in g_server_addr, client_addr;
int g_server_socket_fd;
union PARA
{
char c[4];
long int d;
} para[5];
union CMD
{
char c[4];
long int d;
struct {
char flag[2];
char cmd;
char rev;
};
} cmd;
long int timeout = 0;
unsigned char g_isidle[10] = {0};
char buff[24];
char buff_child[1024];
void do_run_cmd(int fd, char buff[24]){
write(fd, buff, 24);
}
void fill_cmd_head(union CMD *cmd){
cmd->flag[0] = 0xee;
cmd->flag[1] = 0xee;
cmd->rev = 0;
}
void fill_data_tail(union PARA *para){
para->d = CMD_SUFFIX;
}
void run_cmd(int fd, union CMD *cmd, union PARA para[5]){
int j;
fill_cmd_head(cmd);
fill_data_tail(¶[4]);
memcpy(buff, cmd->c, 4);
memcpy(buff+4, para, 20);
if(g_ifdebug) {
printf("run_cmd:\n");
for(j = 0; j < 24; j++)
printf("%x,", buff[j]);
printf("\n");
}
do_run_cmd(fd, buff);
}
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
/* 五个参量 fd打开文件 speed设置波特率 bit数据位设置 neent奇偶校验位 stop停止位 */
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
SETBITSPEED(newtio, 9600);
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIOFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
/*
struct termios options; // 串口配置结构体
tcgetattr(fd,&options); //获取当前设置
bzero(&options,sizeof(options));
options.c_cflag |= B9600 | CLOCAL | CREAD; // 设置波特率,本地连接,接收使能
options.c_cflag &= ~CSIZE; //屏蔽数据位
options.c_cflag |= CS8; // 数据位为 8 ,CS7 for 7
options.c_cflag &= ~CSTOPB; // 一位停止位, 两位停止为 |= CSTOPB
options.c_cflag &= ~PARENB; // 无校验
//options.c_cflag |= PARENB; // 有校验
//options.c_cflag &= ~PARODD;// 偶校验
//options.c_cflag |= PARODD; // 奇校验
options.c_cc[VTIME] = 0; // 等待时间,单位百毫秒 (读)。后有详细说明
options.c_cc[VMIN] = 0; // 最小字节数 (读)。后有详细说明
tcflush(fd, TCIOFLUSH); // TCIFLUSH刷清输入队列。
//TCOFLUSH刷清输出队列。
//TCIOFLUSH刷清输入、输出队列。
tcsetattr(fd, TCSANOW, &options); // TCSANOW立即生效;
//TCSADRAIN:Wait until everything has been transmitted;
//TCSAFLUSH:Flush input and output buffers and make the change
*/
if(g_ifdebug) printf("set_opt: done!\n");
return 0;
}
int open_port(char const *dev)
{
/* fd 打开串口 comport表示第几个串口 */
int fd = open(dev, O_RDWR| //O_RDWR 读写方式打开;
O_NOCTTY| //O_NOCTTY 不允许进程管理串口(不太理解,一般都选上);
O_NDELAY); //O_NDELAY 非阻塞(默认为阻塞,打开后也可以使用fcntl()重新设置)
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
if(g_ifdebug) printf("open %s\n", dev);
if(fcntl(fd, F_SETFL, 0) < 0)
printf("fcntl failed!\n");
else
if(g_ifdebug) printf("fcntl=%d\n", fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
if(g_ifdebug) printf("isatty success!\n");
if(g_ifdebug) printf("fd-open=%d\n",fd);
return fd;
}
void usage() {
printf("\n\r\
./control [<-m -x -y -z>] [-l <format>] [-t <format>]\n\r\
-m: command: A,B,C,V,v, \n\r\
-x: oppsited x distance \n\r\
-y: oppsited y distance \n\r\
-z: oppsited z distance \n\r\
\n\r\
-l <format>: long command\n\r\
format: <command>,<x>,<y>,<z> \n\r\
-t <format>: long command with timeout \n\r\
format:<timeout>,<command>,<x>,<y>,<z> \n\r\
timeout in millisecond \n\r\
\n");
}
int isidle(int j) {
union CMD t_cmd;
union PARA t_para[5];
int count = 0;
int nread = 0;
char tmp_tv[100] = {0},tmp[100] = {0};
int i = 0;
memset(&t_cmd, 0, sizeof(union CMD));
memset(t_para, 0, 5 * sizeof(union PARA));
memset(buff_child, 0, 1024);
struct timeval tv;
gettimeofday(&tv, 0);
sprintf(tmp_tv, "copy %lu:%lu",tv.tv_sec, tv.tv_usec); //for string searching!
if (g_ifdebug)
printf("isidle: checking string: %s\n", tmp_tv);
t_cmd.cmd = 'C';
t_para[0].d = tv.tv_sec;
t_para[1].d = tv.tv_usec;
run_cmd(g_fd[j], &t_cmd, t_para);
fsync(g_fd[j]);
count = 0;
for( i = 0; i < timeout * 100; i++) { //unit of timeout is millisecond
usleep(10);
nread = read(g_fd[j], tmp, 100);
if(nread) {
memcpy(buff_child + count, tmp, nread);
count += nread;
}
if (g_ifdebug)
printf("received from arduino(%d) %s\n",count, buff_child);
if( strstr(buff_child, tmp_tv) != NULL) {
if(g_ifdebug) printf("g_isidle[%d] = 1\n", j);
g_isidle[j] = 1;
return 1;
}
}
return 0;
}
void listDir(char *path)
{
DIR *pDir;
struct dirent *ent;
char childpath[512];
pDir = opendir(path);
memset(childpath, 0, sizeof(childpath));
while((ent = readdir(pDir)) != NULL) //read pDir
{
//if type is NOT DT_DIR which is a file, print name directly
//this is also the exit of recursive
if (strstr(ent->d_name, "Arduino") != NULL) {
sprintf( file_dev[g_countTTYUSB], "%s/%s", path, ent->d_name);
g_countTTYUSB++;
}
}
}
void udpInit()
{
//<1>create the connector of socke
bzero(&g_server_addr,sizeof(g_server_addr));
g_server_addr.sin_family = AF_INET;
g_server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
g_server_addr.sin_port = htons(SERVER_PORT);
//<2>create socket
g_server_socket_fd = socket(AF_INET,SOCK_DGRAM, 0);
if(g_server_socket_fd == -1)
{
perror("Create Socket Failed:");
exit(1);
}
//<3>bind upd connector
if(-1 == (bind(g_server_socket_fd, (struct sockaddr*)&g_server_addr, sizeof(g_server_addr))))
{
perror("Server Bind Failed:");
exit(1);
}
}
void udpserver()
{
int n; //receiver data length
//<1>data transmit
socklen_t client_addr_length = sizeof(client_addr);
//[2]receive data
char buffer[BUFFER_SIZE];
bzero(buffer,BUFFER_SIZE);
n = recvfrom(g_server_socket_fd, buffer, BUFFER_SIZE, 0,
(struct sockaddr*)&client_addr, &client_addr_length);
if(n > 0)
{
buffer[n] = 0;
sscanf(buffer, "%ld,%c,%ld,%ld,%ld,%ld",
&timeout, &cmd.cmd, ¶[0].d, ¶[1].d, ¶[2].d, ¶[3].d);
printf("opt with optopt: %ld,%c,%ld,%ld,%ld,%ld\n",
timeout, cmd.cmd, para[0].d, para[1].d, para[2].d, para[3].d);
sendto(g_server_socket_fd, "Success", 7, 0,
(struct sockaddr*)&client_addr, sizeof(client_addr));
}
return;
}
int main(int argc, char **argv)
{
int ch;
int i, j;
bool udp_enter = false;
memset(&cmd, 0, 4);
memset(para, 0, 20);
memset(file_dev, 0, 128 * 10);
///////////////////////
// Command options
//
opterr = 0;
while ((ch = getopt(argc, argv, "dum:x:y:z:c:l:t:")) != -1)
{
switch(ch)
{
case 'm':
cmd.cmd = optarg[0];
break;
case 'x':
para[0].d = atoi(optarg);
break;
case 'y':
para[1].d = atoi(optarg);
break;
case 'z':
para[2].d = atoi(optarg);
break;
case 'c':
para[3].d = atoi(optarg);
break;
case 'l':
sscanf(optarg, "%c,%ld,%ld,%ld,%ld",
&cmd.cmd, ¶[0].d, ¶[1].d, ¶[2].d, ¶[3].d);
break;
case 't':
sscanf(optarg, "%ld,%c,%ld,%ld,%ld,%ld",
&timeout, &cmd.cmd, ¶[0].d, ¶[1].d, ¶[2].d, ¶[3].d);
break;
case 'u':
udpInit();
udp_enter = true;
break;
case 'd':
g_ifdebug = 1;
break;
case '?':
default:
usage();
exit(0);
}
printf("opt [%c] with optopt: %s\n", ch, optarg);
}
listDir("/dev");
for(i = 0; i < g_countTTYUSB; i++) {
//////////////////////////
// Open serial port
// By default open ttyUSB0
//
if((g_fd[i] = open_port(file_dev[i])) < 0){
perror("open_port error");
return -1;
}
//////////////////////////
// Set serial port
//
if((set_opt(g_fd[i], 9600, 8, 'N', 1)) < 0){
perror("set_opt error");
return -2;
}
}
if(timeout > 0) {
for(j = 0; j < g_countTTYUSB; j++) {
isidle(j);
}
} else
for (j = 0; j < g_countTTYUSB; j++)
g_isidle[j] = 1;
do {
if (udp_enter)
{
memset(&cmd, 0, 4);
memset(para, 0, 20);
memset(file_dev, 0, 128 * 10);
udpserver();
}
for(j = 0; j < g_countTTYUSB; j++) {
if (g_isidle[j])
run_cmd(g_fd[j], &cmd, para);
}
if (udp_enter) { //this if for info after moving!
struct timeval tv;
int sum = 0;
char tmp_tv[100] = {0};
gettimeofday(&tv, 0);
for(j = 0; j < g_countTTYUSB; j++) {
isidle(j);
sum += g_isidle[j];
}
if (sum == g_countTTYUSB) {
sprintf(tmp_tv, "Y: (%lu:%lu) ",tv.tv_sec, tv.tv_usec); //for string searching!
sendto(g_server_socket_fd, tmp_tv, 100, 0,
(struct sockaddr*)&client_addr, sizeof(client_addr));
} else {
sprintf(tmp_tv, "N: (%lu:%lu) ",tv.tv_sec, tv.tv_usec); //for string searching!
sendto(g_server_socket_fd, tmp_tv, 100, 0,
(struct sockaddr*)&client_addr, sizeof(client_addr));
}
}
} while(udp_enter);
for(j = 0; j < g_countTTYUSB; j++)
close(g_fd[j]);
return 0;
}