-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserialio.c
More file actions
166 lines (129 loc) · 2.71 KB
/
serialio.c
File metadata and controls
166 lines (129 loc) · 2.71 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
#include <sys/types.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <termios.h>
#include <err.h>
#include "serialio.h"
/*
* Serial I/O routines.
*/
static int serial_setup (int fd, speed_t baud);
/*
* Read a character from the serial port. Note that this
* routine will block until a valid character is read.
*/
int
serial_readchar (int fd, uint8_t * c)
{
char b;
int r;
while ((r = read (fd, &b, 1)) == -1)
;
if (r != -1) {
*c = b;
#ifdef MSR_DEBUG
printf ("[0x%x]\n", b);
#endif
}
return (r);
}
/*
* Read a series of characters from the serial port. This
* routine will block until the desired number of characters
* is read.
*/
int
serial_read (int fd, void * buf, size_t len)
{
size_t i;
uint8_t b, *p;
p = buf;
#ifdef SERIAL_DEBUG
printf("[RX %.3d]", len);
#endif
for (i = 0; i < len; i++) {
serial_readchar (fd, &b);
#ifdef SERIAL_DEBUG
printf(" %.2x", b);
#endif
p[i] = b;
}
#ifdef SERIAL_DEBUG
printf("\n");
#endif
return (0);
}
int
serial_write (int fd, void * buf, size_t len)
{
return (write (fd, buf, len));
}
/*
* Set serial line options. We need to set the baud rate and
* turn off most of the internal processing in the tty layer in
* order to avoid having some of the output from the card reader
* interpreted as control characters and swallowed.
*/
static int
serial_setup (int fd, speed_t baud)
{
struct termios options;
if (tcgetattr(fd, &options) == -1)
return (-1);
/* Set baud rate */
cfsetispeed(&options, baud);
cfsetospeed(&options, baud);
/* Control modes */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/*
* Local modes
* We have to clear the ISIG flag to defeat signal
* processing in order to see the file separator
* character (0x1C) which the device will send as
* part of its end of record markers.
*/
options.c_lflag &= ~ICANON;
options.c_lflag &= ~ECHO;
options.c_lflag &= ~ECHONL;
options.c_lflag &= ~ISIG;
options.c_lflag &= ~IEXTEN;
/* Input modes */
options.c_iflag &= ~ICRNL;
options.c_iflag &= ~IXON;
options.c_iflag &= ~IXOFF;
options.c_iflag &= ~IXANY;
options.c_iflag |= IGNBRK;
options.c_iflag |= IGNPAR;
/* Output modes */
options.c_oflag &= ~OPOST;
if (tcsetattr(fd, TCSANOW, &options) == -1)
return (-1);
return (0);
}
int
serial_open(char *path, int * fd, int blocking, speed_t baud)
{
int f;
f = open(path, blocking | O_RDWR | O_FSYNC);
if (f == -1)
return (-1);
if (serial_setup (f, baud) != 0) {
close (f);
return (-1);
}
*fd = f;
return (0);
}
int
serial_close(int fd)
{
close (fd);
return (0);
}