-
Notifications
You must be signed in to change notification settings - Fork 0
/
set485.c
321 lines (303 loc) · 8.94 KB
/
set485.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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#if defined(Linux)
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/serial.h>
#define FULLDUPLEX TIOCSER485FULLDUPLEX
#define HALFDUPLEX TIOCSER485HALFDUPLEX
#define SLAVEMULTI TIOCSER485SLAVEMULTIPLEX
#define SET485 TIOCSER485SET
#define GET485 TIOCSER485GET
#elif defined(SCO_SV)
#include <sys/sio.h>
#define FULLDUPLEX AIOC485FULLDUPLEX
#define HALFDUPLEX AIOC485HALFDUPLEX
#define SLAVEMULTI AIOC485SLAVEMULTI
#define SET485 result
#define GET485 AIOC485GETMODE
#elif defined(SunOS)
#include <sys/termios.h>
#define FULLDUPLEX TIOC485FULLDUPLEX
#define HALFDUPLEX TIOC485HALFDUPLEX
#define SLAVEMULTI TIOC485SLAVEMULTIPLEX
#define SET485 TIOC485SETMODE
#define GET485 TIOC485GETMODE
#endif
#define COMMAND_SIZE 256
#define VERSION "1.0"
#define IDENT_STRING "%s v%s Copyright 1999 \n", this, VERSION
#define HELP_STRING "Type help or ? for help\n"
int main(int argc, char **argv) {
char *this = *argv;
char *delim = " ";
char command[COMMAND_SIZE];
char data[COMMAND_SIZE];
int i;
char *dummy;
int dev = (int) NULL;
unsigned int result;
char prompt[COMMAND_SIZE] = "> ";
for (dummy = this; *dummy; dummy++)
if (*dummy == '/')
this = dummy + 1;
/* Command line mode */
if (argc > 1) {
for (argc--, argv++; argc; argc--, argv++) {
if (!strcmp("-g", *argv)) {
argc--; argv++;
if (!argc) {
printf("No device specified\n");
return 1;
}
/* dev = open(*argv, O_RDWR); */
dev = open(*argv, O_RDWR | O_NONBLOCK);
if (dev == -1) {
printf("Couldn't open '%s'\n", *argv);
perror(NULL);
return 1;
}
/* New open code */
i = fcntl(dev, F_GETFL, NULL);
if (i == -1) {
printf("Couldn't get file flags for `%s`\n", *argv);
perror(NULL);
return 1;
}
i = fcntl(dev, F_SETFL, i & ~O_NONBLOCK);
if (i == -1) {
printf("Couldn't set file flags for `%s`\n", *argv);
perror(NULL);
return 1;
}
if (ioctl(dev, GET485, &result) < 0) {
printf("Couldn't get mode for '%s'\n", *argv);
perror(NULL);
return 1;
}
switch (result) {
case FULLDUPLEX:
printf("Full duplex for '%s'\n", *argv);
break;
case HALFDUPLEX:
printf("Half duplex for '%s'\n", *argv);
break;
case SLAVEMULTI:
printf("Slave multiplex for '%s'\n", *argv);
break;
default:
printf("Unknown mode '%d' for '%s'\n", result, *argv);
break;
}
close(dev);
} else if (!strcmp("-s", *argv)) {
argc--; argv++;
if (!argc) {
printf("No device specified\n");
return 1;
}
dummy = *argv;
argc--; argv++;
if (!argc) {
printf("No mode specified for '%s'\n", dummy);
return 1;
}
/* dev = open(dummy, O_RDWR); */
dev = open(dummy, O_RDWR | O_NONBLOCK);
if (dev == -1) {
printf("Couldn't open '%s'\n", dummy);
perror(NULL);
return 1;
}
/* New open code */
i = fcntl(dev, F_GETFL, NULL);
if (i == -1) {
printf("Couldn't get file flags for `%s`\n", dummy);
perror(NULL);
return 1;
}
i = fcntl(dev, F_SETFL, i & ~O_NONBLOCK);
if (i == -1) {
printf("Couldn't set file flags for `%s`\n", dummy);
perror(NULL);
return 1;
}
if ((*argv[0] == 'f') || (*argv[0] == 'F'))
result = FULLDUPLEX;
else if ((*argv[0] == 'h') || (*argv[0] == 'H'))
result = HALFDUPLEX;
else if ((*argv[0] == 's') || (*argv[0] == 'S'))
result = SLAVEMULTI;
else {
printf("Invalid mode '%s' for '%s'\n", *argv, dummy);
return 1;
}
if (ioctl(dev, SET485, &result) < 0) {
printf("Couldn't set mode for '%s'\n", dummy);
perror(NULL);
return 1;
}
close(dev);
} else if (!strcmp("-h", *argv) || !strcmp("-?", *argv)) {
printf(IDENT_STRING);
printf("\n");
printf("%s [-g <dev>] [-s <dev> <mode>] [-g ...] [-s ...]\n", this);
printf("\n");
printf("-g <dev>\t\tGet device <dev>'s mode\n");
printf("-s <dev> <mode>\t\tSet device <dev> to 485 mode full, half "
"or slave\n");
printf("-h\t\t\tThis help screen\n");
printf("\n");
printf("Modes can be abbreviated to the first letter\n");
printf("Multiple gets/sets are allowed in any order on the command "
"line\n");
printf("%s will exit after the first error\n", this);
printf("\n");
} else {
printf("%s: Error: Unrecognized option `%s`\n", this, *argv);
return 1;
}
}
return 0;
}
/* Interactive mode */
printf(IDENT_STRING);
printf(HELP_STRING);
do {
printf("%s", prompt);
for (i = 0; i < COMMAND_SIZE - 1; i++) {
command[i] = getc(stdin);
if (command[i] == '\n')
break;
if (command[i] == '\t')
command[i] = ' ';
if (((i == 0) && (command[i] == ' ')) ||
((i > 0) && (command[i] == ' ') && (command[i - 1] == ' ')))
i--;
}
command[i] = (char) NULL;
strcpy(data, command);
if ((data[0] >= 'A') && (data[0] <= 'Z'))
data[0] += ' ';
strtok(data, delim);
switch (data[0]) {
case 's':
if (!dev) {
printf("No device currently open\n");
break;
}
dummy = strtok(NULL, delim);
if (!dummy) {
printf("You need to specify a mode: full, half or slave\n");
break;
}
if ((dummy[0] >= 'A') && (dummy[0] <= 'Z'))
dummy[0] += ' ';
if (dummy[0] == 'f')
result = FULLDUPLEX;
else if (dummy[0] == 'h')
result = HALFDUPLEX;
else if (dummy[0] == 's')
result = SLAVEMULTI;
else {
printf("Invalid mode '%s'; try full, half or slave\n", command);
break;
}
if (ioctl(dev, SET485, &result) < 0) {
printf("Couldn't set mode\n");
perror(NULL);
}
break;
case 'g':
if (!dev) {
printf("No device currently open\n");
break;
}
if (ioctl(dev, GET485, &result) < 0) {
printf("Couldn't get mode\n");
perror(NULL);
break;
}
switch (result) {
case FULLDUPLEX:
printf("Full duplex\n");
break;
case HALFDUPLEX:
printf("Half duplex\n");
break;
case SLAVEMULTI:
printf("Slave multiplex\n");
break;
default:
printf("Unknown mode '%d %x'\n", result, result);
break;
}
break;
case 'o':
dummy = strtok(NULL, delim);
if (!dummy) {
printf("You need to specify a device to open\n");
break;
}
if (dev)
close(dev);
/* dev = open(dummy, O_RDWR); */
dev = open(dummy, O_RDWR | O_NONBLOCK);
if (dev == -1) {
printf("Couldn't open '%s'\n", dummy);
perror(NULL);
break;
}
/* New open code */
i = fcntl(dev, F_GETFL, NULL);
if (i == -1) {
close(dev);
printf("Couldn't get file flags for `%s`\n", dummy);
perror(NULL);
break;
}
i = fcntl(dev, F_SETFL, i & ~O_NONBLOCK);
if (i == -1) {
close(dev);
printf("Couldn't set file flags for `%s`\n", dummy);
perror(NULL);
break;
}
sprintf(prompt, "%s> ", dummy);
break;
case 'c':
if (!dev) {
printf("No device currently open\n");
break;
}
close(dev);
sprintf(prompt, "> ");
break;
case 'h':
case '?':
printf(IDENT_STRING);
printf("\n");
printf("close\t\t\tClose the current device\n");
printf("get\t\t\tGet the current device's mode\n");
printf("help\t\t\tThis help screen\n");
printf("open <dev>\t\tOpen device <dev>\n");
printf("quit\t\t\tExit %s\n", this);
printf("set <mode>\t\tSet device to 485 mode full, half or slave\n");
printf("\n");
printf("Commands and modes can be abbreviated to the first letter\n");
printf("\n");
printf("To get command line help, run %s -h\n", this);
break;
case 'q':
case '\0':
break;
default:
printf("Invalid command '%s'\n", command);
printf(HELP_STRING);
break;
}
} while (data[0] != 'q');
return 0;
}