forked from ysun/RaspberryPiSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.c
More file actions
354 lines (292 loc) · 7.22 KB
/
control.c
File metadata and controls
354 lines (292 loc) · 7.22 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
#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>
#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"};
#define COUNTOFARDUINO 3 //sizeof(dev[0])
//#define COUNTOFARDUINO 1
int g_fd[10] = {0}; //For serial device
unsigned char g_ifdebug = 0;
union PARA
{
char c[4];
long int d;
} para[4];
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[20];
char buff_child[1024];
void do_run_cmd(int fd, char buff[20]){
write(fd, buff, 20);
}
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[4]){
int j;
fill_cmd_head(cmd);
fill_data_tail(¶[3]);
memcpy(buff, cmd->c, 4);
memcpy(buff+4, para, 16);
if(g_ifdebug) {
printf("run_cmd:\n");
for(j = 0; j < 20; 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
printf("open %s\n", dev);
if(fcntl(fd, F_SETFL, 0) < 0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n", fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
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[4];
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, 4 * 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; i++) {
usleep(1000);
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;
}
int main(int argc, char **argv)
{
int ch;
int i, j;
memset(&cmd, 0, 4);
memset(para, 0, 16);
///////////////////////
// Command options
//
opterr = 0;
while ((ch = getopt(argc, argv, "dm:x:y:z: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 'l':
sscanf(optarg, "%c,%ld,%ld,%ld",
&cmd.cmd, ¶[0].d, ¶[1].d, ¶[2].d);
break;
case 't':
sscanf(optarg, "%ld,%c,%ld,%ld,%ld",
&timeout, &cmd.cmd, ¶[0].d, ¶[1].d, ¶[2].d);
break;
case 'd':
g_ifdebug = 1;
break;
case '?':
default:
usage();
exit(0);
}
printf("opt [%c] with optopt: %s\n", ch, optarg);
}
for(i = 0; i < COUNTOFARDUINO; i++) {
//////////////////////////
// Open serial port
// By default open ttyUSB0
//
if((g_fd[i] = open_port(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 < COUNTOFARDUINO; j++) {
isidle(j);
}
} else
for (j = 0; j < COUNTOFARDUINO; j++)
g_isidle[j] = 1;
for(j = 0; j < COUNTOFARDUINO; j++) {
if (g_isidle[j])
run_cmd(g_fd[j], &cmd, para);
close(g_fd[j]);
}
return 0;
}