This repository has been archived by the owner on May 13, 2024. It is now read-only.
forked from FozzTexx/hp-plot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat-serial.c
83 lines (70 loc) · 1.88 KB
/
cat-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
/* Copyright 1998-2014 by Chris Osborn <fozztexx@fozztexx.com>
*
* This file is part of hp-plot.
*
* hp-plot is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* hp-plot is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hp-plot; see the file COPYING. If not see
* <http://www.gnu.org/licenses/>.
*/
#include "getargs.h"
#include "ttlock.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#define HPBUF 128
int main(int argc, char *argv[])
{
int count;
int fd;
struct termios state;
int i, j;
char buf[HPBUF];
int enqack = 0;
count = getargs(argc, argv, "eb", &enqack);
if (count < 0 || (argc - count) < 1) {
if (count < 0 && -count != '-')
fprintf(stderr, "Bad flag: %c\n", -count);
fprintf(stderr, "Usage: %s <device>\n"
, *argv);
exit(1);
}
while (ttlock(argv[1]))
sleep(10);
fd = open(argv[1], O_RDWR);
tcgetattr(fd, &state);
cfmakeraw(&state);
state.c_cflag &= ~(CRTSCTS | PARODD | CSIZE | CSTOPB);
state.c_cflag |= CS7 | PARENB;
cfsetispeed(&state, B2400);
cfsetospeed(&state, B2400);
tcsetattr(fd, TCSAFLUSH, &state);
i = 0;
write(fd, "OE;", 3);
while ((j = read(0, buf, 1)) > 0) {
write(fd, buf, j);
i += j;
if (i >= HPBUF && buf[0] == ';') {
i = 0;
write(fd, "OE;", 3);
buf[0] = 0;
for (j = 0; j < 2; )
j += read(fd, buf, 1);
}
}
close(fd);
ttunlock();
exit(0);
}