-
Notifications
You must be signed in to change notification settings - Fork 17
/
serial.c
235 lines (203 loc) · 6.36 KB
/
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
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
// serial code routine borrowed from:
// http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
// Note1: enable unix domain socket support is untested on Windows/Cygwin so
// it's better to leave commented out by default ...
// -------------------------------------------------
// Note2 (GI): I'm leaving this always enabled now, as I've gotten
// unix-sockets to work in winxp+cygwin
#define SUPPORT_UNIX_DOMAIN_SOCKET
#define _BSD_SOURCE _BSD_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#ifdef SUPPORT_UNIX_DOMAIN_SOCKET
#include <sys/un.h>
#include <sys/socket.h>
#endif
#include "serial.h"
#define error_message printf
int fd;
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tcgetattr\n", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
error_message ("error %d from tcsetattr\n", errno);
return -1;
}
return 0;
}
void set_blocking_serial (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tggetattr\n", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 2 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
error_message ("error %d setting term attributes\n", errno);
}
/**
* opens the desired serial port at the required 2000000 bps, or to a unix-domain socket
*
* portname = the desired "/dev/ttyS*" device portname to use
* "unix#..path.." defines a unix-domain named stream socket to connect to (emulator)
*/
bool serialOpen(char* portname)
{
if (!strncasecmp(portname, "unix#", 5)) {
#ifdef SUPPORT_UNIX_DOMAIN_SOCKET
struct sockaddr_un sock_st;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
error_message("error %d creating UNIX-domain socket: %s\n", errno, strerror (errno));
return false;
}
sock_st.sun_family = AF_UNIX;
strcpy(sock_st.sun_path, portname + 5);
if (connect(fd, (struct sockaddr*)&sock_st, sizeof(struct sockaddr_un))) {
error_message("error %d connecting to UNIX-domain socket %s: %s\n", errno, portname + 5, strerror (errno));
close(fd);
return false;
}
//set_blocking_std (fd, 0); // set no blocking
#else
error_message("unix domain socket is not compiled in this time!\n");
return false;
#endif
} else {
fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
error_message ("error %d opening %s: %s\n", errno, portname, strerror (errno));
return false;
}
#ifdef __APPLE__
// WARNING: This slower bps won't work with newer bitstreams
set_interface_attribs (fd, B230400, 0); // set speed to slower 230,400 bps, 8n1 (no parity)
#else
set_interface_attribs (fd, B2000000, 0); // set speed to 2,000,000 bps, 8n1 (no parity)
#endif
set_blocking_serial (fd, 0); // set no blocking
}
return true;
}
/**
* closes the opened serial port
*/
bool serialClose(void)
{
if (fd >= 0)
{
close(fd);
return true;
}
return false;
}
void serialFlush(void)
{
// http://stackoverflow.com/questions/13013387/clearing-the-serial-ports-buffer
// sleep(2); //required to make flush work, for some reason (for USB serial ports?)
// tcflush(fd,TCIOFLUSH);
// I'll now try a 'manual' flush, to see if that works for Ralph's mac and my mac...
int bytes_available = 0;
static char tmp[16384];
#ifdef FIONREAD
ioctl(fd, FIONREAD, &bytes_available);
#else
ioctl(fd, TIOCINQ, &bytes_available);
#endif
if (bytes_available > 0)
read(fd, tmp, bytes_available);
}
/**
* writes a string to the serial port
*/
void serialWrite(char* string)
{
serialFlush();
int i = strlen(string);
// do we need to add a carriage return to the end?
if (string[i-1] != '\n')
{
strcat(string, "\n");
i++;
}
write (fd, string, i); // send string
}
/**
* reads serial data and feeds it into the provided buffer. The routine will read up
* until the next '.' prompt. It should also crop out the first line, which is just
* and echo of the command.
*
* returns:
* true = read till the next '.' prompt.
* false = could not read till next '.' prompt (eg, buffer was filled)
*/
bool serialRead(char* buf, int bufsize)
{
char* ptr = buf;
char* secondline = NULL;
bool foundLF = false;
while (ptr - buf < bufsize)
{
int n = read (fd, ptr, bufsize); // read up to 'bufsize' characters if ready to read
if (n == -1)
return false;
// check for "." prompt
for (int k = 0; k < n; k++)
{
if ( *(ptr+k) == '\n' )
{
foundLF = true;
if (!secondline)
secondline = ptr+k+1;
}
else if (foundLF && *(ptr+k) == '.')
{
*(ptr+k) = '\0';
int len = strlen(secondline) + 1;
for (int z = 0; z < len; z++)
*(buf+z) = *(secondline+z);
return true;
}
else
foundLF = false;
}
ptr += n;
}
return false;
}