-
Notifications
You must be signed in to change notification settings - Fork 41
/
SerialWrapper.c
executable file
·269 lines (226 loc) · 4.73 KB
/
SerialWrapper.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
#include "SerialWrapper.h"
int serialOpen(char* port_address, int baud_rate)
{
/* Open device: */
int fd;
struct termios options;
if( (fd = open(port_address, O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
{
return -1;
}
/* If port opened, set up our parameters: */
/* fcntl(fd, F_SETFL, FNDELAY); // non-blocking read */
fcntl(fd, F_SETFL,0); /* blocking read */
/* Save current port settings so nothing is corrupt on exit: */
tcgetattr(fd, &options);
cfmakeraw(&options);
/* Convert integer baud to baud type: */
/* Default to 9600 baud if none specified: */
switch (baud_rate)
{
case 4800:
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
break;
case 9600:
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
break;
case 19200:
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
break;
case 38400:
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
break;
case 57600:
cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);
break;
case 115200:
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
break;
case 230400:
cfsetispeed(&options, B230400);
cfsetospeed(&options, B230400);
break;
case 500000:
cfsetispeed(&options, B500000);
cfsetospeed(&options, B500000);
break;
default:
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
break;
}
/* Set options for proper port settings: */
options.c_cflag |= (CLOCAL | CREAD);
/* Turn off hardware flow control: */
options.c_cflag &= ~CRTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* Write our changes to the port configuration: */
tcsetattr(fd, TCSANOW, &options);
return fd;
}
int serialFlush(int fd)
{
if(tcflush(fd,TCIOFLUSH) == -1)
{
return -1;
}
return 0;
}
int serialFlushWrite(int fd)
{
if(tcflush(fd,TCOFLUSH) == -1)
{
return -1;
}
return 0;
}
int serialFlushRead(int fd)
{
if(tcflush(fd,TCIFLUSH) == -1)
{
return -1;
}
return 0;
}
int serialWrite(int fd, char* data, int length)
{
if(write(fd,data,length)==-1)
{
return -1;
}
return 0;
}
int serialRead(int fd, char* temp, int length)
{
int cnt;
if((cnt = read(fd,temp,length))==-1)
{
return -1;
}
return cnt;
}
int serialCommandByDelim(int fd, char* cmd, char* reply, int replyLength, int usecs, char delim)
{
int bufferCount = 0;
int index = 0;
char temp;
timer timerid;
/* Sleep for half a second before checking timeout, unless it should be smaller. */
int sleeptime = HALF_SECOND;
int st = usecs / 10;
if ( sleeptime > st )
{
sleeptime = st;
}
/* Create timeout timer: */
timerCreate(&timerid);
timerSet_us(&timerid, usecs);
/* Flush the read port: */
if ( serialFlushRead(fd) < 0)
{
timerDelete(&timerid);
return -1;
}
/* Write the command: */
if ( serialWrite(fd, cmd, strlen(cmd)) < 0)
{
timerDelete(&timerid);
return -1;
}
/* Wait for response: */
while( !isTimerExpired(&timerid) )
{
if( (bufferCount = serialBufferCount( fd )) > 0 )
{
if( serialRead( fd, &temp, 1 ) < 0 )
{
timerDelete(&timerid);
return -1;
}
/* Have we found the delimter yet? */
if( temp == delim )
{
timerDelete(&timerid);
return index;
}
reply[index] = temp;
index++;
if( index >= replyLength )
{
timerDelete(&timerid);
return index;
}
}
else
{
rt_sleep_us( sleeptime );
}
}
timerDelete(&timerid);
return index;
}
int serialCommandByLength(int fd, char* cmd, char* reply, int replyLength, int usecs)
{
int bufferCount = 0;
timer timerid;
/* Sleep for half a second before checking timeout, unless it should be smaller. */
int sleeptime = HALF_SECOND;
int st = usecs / 10;
if ( sleeptime > st )
{
sleeptime = st;
}
/* Create timeout timer: */
timerCreate(&timerid);
timerSet_us(&timerid, usecs);
/* Flush the read port: */
if ( serialFlushRead(fd) < 0)
{
timerDelete(&timerid);
return -1;
}
/* Write the command: */
if ( serialWrite(fd, cmd, strlen(cmd)) < 0)
{
timerDelete(&timerid);
return -1;
}
/* Wait for response: */
while ( (bufferCount = serialBufferCount( fd )) < replyLength )
{
if( isTimerExpired(&timerid) )
{
bufferCount = serialBufferCount( fd );
break;
}
rt_sleep_us( sleeptime );
}
/* Make sure we don't read more data than was asked for... */
if( bufferCount > replyLength )
{
bufferCount = replyLength;
}
bufferCount = serialRead( fd, reply, bufferCount );
timerDelete(&timerid);
return bufferCount;
}
int serialBufferCount(int fd)
{
int count;
ioctl(fd, FIONREAD, &count);
return count;
}
int serialClose(int fd)
{
return close(fd);
}