-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput-tape.c
387 lines (343 loc) · 9.35 KB
/
put-tape.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
/*
* Program for sending papertapes
*
* Licence GPL 2.0
*
* By Anders Sandahl 2024
*
*/
#include <argp.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <stdbool.h>
const char *argp_program_version =
"put-tape 0.99";
const char *argp_program_bug_address =
"<[email protected]>";
/* Program documentation. */
static char doc[] =
"Program for sending papertapes, takes input from stdin or file and sends it on a serial port." \
"Default is 9600 8N1 on device /dev/ttyUSB0.";
/* Options to be parsed. */
static struct argp_option options[] = {
{"device", 'd', "DEV", OPTION_ARG_OPTIONAL, "Serial device, /dev/ttyXXX"},
{"bits", 'b', "5,6,7,8", OPTION_ARG_OPTIONAL, "Number of data bits"},
{"parity", 'p', "N,E,O,M", OPTION_ARG_OPTIONAL, "Parity"},
{"transmit-delay", 't', "NUMBER", OPTION_ARG_OPTIONAL, "Character transmit delay 0-1000ms"},
{"stop", 'S', "1,2", OPTION_ARG_OPTIONAL, "Number of stop bits"},
{"speed", 's', "BAUD", OPTION_ARG_OPTIONAL, "Serial com speed"},
{"handshake", 'h', 0, OPTION_ARG_OPTIONAL, "Use RTS/CTS handshake"},
{"filename", 'f', "FILE", OPTION_ARG_OPTIONAL, "Input data file"},
{ 0 }
};
speed_t map_baudrate(int baud){
speed_t speed;
switch (baud) {
case 110:
speed = B110;
break;
case 150:
speed = B150;
break;
case 200:
speed = B200;
break;
case 300:
speed = B300;
break;
case 600:
speed = B600;
break;
case 1200:
speed = B1200;
break;
case 1800:
speed = B1800;
break;
case 2400:
speed = B2400;
break;
case 4800:
speed = B4800;
break;
case 9600:
speed = B9600;
break;
case 19200:
speed = B19200;
break;
case 38400:
speed = B38400;
break;
case 57600:
speed = B57600;
break;
case 115200:
speed = B115200;
break;
case 230400:
speed = B230400;
break;
case 460800:
speed = B460800;
break;
case 500000:
speed = B500000;
break;
case 576000:
speed = B576000;
break;
case 921600:
speed = B921600;
break;
case 1000000:
speed = B1000000;
break;
case 11520000:
speed = B1152000;
break;
case 15000000:
speed = B1500000;
break;
case 20000000:
speed = B2000000;
default:
speed = -1;
}
return speed;
}
/* Used by main to communicate with parse_opt. */
struct argp_arguments
{
char *device;
char *file;
int bits;
char parity;
int stop_bits;
bool handshake;
speed_t speed;
int transmit_delay;
};
/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct argp_arguments *arguments = state->input;
switch (key){
case 'd':
arguments->device = arg;
break;
case 'f':
arguments->file = arg;
break;
case 'b':
if (arg != NULL) {
switch (arg[0]){
case '5':
arguments->bits = 5;
break;
case '6':
arguments->bits = 6;
break;
case '7':
arguments->bits = 7;
break;
case '8':
arguments->bits = 8;
break;
default:
fprintf(stderr, "Error, invalid number of bits: %s\n", arg);
argp_usage (state);
return ARGP_ERR_UNKNOWN;
break;
break;
}
}
break;
case 'p':
if (arg != NULL) {
switch (arg[0]){
case 'N':
case 'O':
case 'E':
case 'M':
arguments->parity = arg[0];
break;
default:
fprintf(stderr, "Error, invalid parity (N/O/E/M): %s\n", arg);
argp_usage (state);
return ARGP_ERR_UNKNOWN;
break;
break;
}
}
break;
case 'S':
if (arg != NULL) {
switch (arg[0]){
case '1':
arguments->stop_bits = 1;
break;
case '2':
arguments->stop_bits = 2;
break;
default:
fprintf(stderr, "Error, invalid number of stop bits: %s\n", arg);
argp_usage (state);
return ARGP_ERR_UNKNOWN;
break;
}
}
break;
case 's':
if (arg != NULL) {
int baud = atoi(arg);
arguments->speed = map_baudrate(baud);
if (arguments->speed == -1) {
fprintf(stderr, "Invalid baudrate: %d\n", baud);
argp_usage (state);
return ARGP_ERR_UNKNOWN;
}
} else {
argp_usage (state);
return ARGP_ERR_UNKNOWN;
}
break;
case 't':
if (arg != NULL) {
arguments->transmit_delay = atoi(arg);
if (arguments->transmit_delay > 1000 || arguments->transmit_delay < 0) {
fprintf(stderr, "Invalid delay: %dms\n", arguments->transmit_delay);
argp_usage (state);
return ARGP_ERR_UNKNOWN;
}
} else {
argp_usage (state);
return ARGP_ERR_UNKNOWN;
}
break;
case 'h':
arguments->handshake = true;
break;
case ARGP_KEY_ARG:
if (state->arg_num != 0){
}
break;
case ARGP_KEY_END:
if (state->arg_num != 0){
argp_usage (state);
return ARGP_ERR_UNKNOWN;
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, NULL, doc };
int set_interface_attribs(int fd, speed_t speed, char parity, int bits, int stop_bits, bool handshake)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
fprintf(stderr, "Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetspeed(&tty, speed);
tty.c_cflag |= (CLOCAL | CREAD | PARENB); /* ignore modem controls */
tty.c_cflag &= ~CSIZE;
if (bits == 5)
tty.c_cflag |= CS5; /* 5-bit characters */
if (bits == 6)
tty.c_cflag |= CS6; /* 6-bit characters */
if (bits == 7)
tty.c_cflag |= CS7; /* 7-bit characters */
else if (bits == 8)
tty.c_cflag |= CS8; /* 8-bit characters */
else
return -1;
if (parity == 'N') {
tty.c_cflag &= ~PARENB; /* no parity */
} else if (parity == 'E') {
tty.c_cflag |= PARENB; /* parity */
tty.c_cflag &= ~PARODD; /* even parity */
} else if (parity == 'O') {
tty.c_cflag |= PARENB; /* parity */
tty.c_cflag |= PARODD; /* odd parity */
} else return -1;
if (stop_bits == 1)
tty.c_cflag &= ~CSTOPB; /* 1 stop bit */
else
tty.c_cflag |= CSTOPB; /* 2 stop bits */
if (handshake) {
tty.c_cflag |= CRTSCTS; /* hardware flowcontrol */
} else {
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
}
/* setup for non-canonical mode */
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
/* Read with timeout, 1 s */
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 10;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
fprintf(stderr, "Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
struct argp_arguments args;
int buff[1];
int ch;
int fd;
FILE *f;
args.bits = 8;
args.parity = 'N';
args.stop_bits = 1;
args.device = "/dev/ttyUSB0";
args.file = NULL;
args.transmit_delay = 0;
args.speed = B9600;
args.handshake = false;
if (0 > argp_parse (&argp, argc, argv, 0, 0, &args))
return -1;
fd = open(args.device, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
fprintf(stderr, "Error opening device %s: %s\n", args.device, strerror(errno));
return -1;
}
if (set_interface_attribs(fd, args.speed, args.parity, args.bits, args.stop_bits, args.handshake) < 0) {
close(fd);
return -1;
}
/*
* Use stdin if no filename is given.
*/
if (args.file != NULL) {
if ((f = fopen(args.file, "r")) == NULL) {
fprintf(stderr, "Could not open file \"%s\": %s\n", args.file, strerror(errno));
close(fd);
return -1;
}
} else {
f = stdin;
}
/*
* Get every char from f and put them on the serial port until EOF.
*/
while (EOF != (ch = fgetc(f))) {
buff[0] = ch;
write(fd, buff, 1);
usleep(1000 * args.transmit_delay);
}
close(fd);
}