-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwiringSerial.c
262 lines (207 loc) · 5.96 KB
/
wiringSerial.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
/*
* wiringSerial.c:
* Handle a serial port
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "wiringSerial.h"
/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*********************************************************************************
*/
int serialOpen (char *device, int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
default:
return -2 ;
}
if ((fd = open (device,O_RDWR | O_NOCTTY | O_NDELAY)) == -1){
return -1 ;
}
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
//printf("fd: %d\n",fd);
return fd ;
}
/*
* serialFlush:
* Flush the serial buffers (both tx & rx)
*********************************************************************************
*/
void serialFlush (int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
/*
* serialClose:
* Release the serial port
*********************************************************************************
*/
void serialClose (int fd)
{
close (fd) ;
}
/*
* serialPutchar:
* Send a single character to the serial port
*********************************************************************************
*/
void serialPutchar (int fd, unsigned char c)
{
write (fd, &c, 1) ;
}
/*
* serialPuts:
* Send a string to the serial port
*********************************************************************************
*/
void serialPuts (int fd, char *s)
{
write (fd, s, strlen (s)) ;
}
/*
* serialPuts:
* Send a string to the serial port
*********************************************************************************
*/
void serialWrite (int fd, char *s, int count)
{
write (fd, s, count) ;
serialFlush(fd);
}
/*
* serialPrintf:
* Printf over Serial
*********************************************************************************
*/
void serialPrintf (int fd, char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
serialPuts (fd, buffer) ;
}
/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
int serialDataAvail (int fd)
{
int result ;
if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;
return result ;
}
/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
int serialDataBlock (int fd, int count)
{
int ret;
while((ret=serialDataAvail(fd)) < count){
if(ret<0)
return -1;
usleep(5);
}
return 1;
}
/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
int serialDataRead (int fd, char* buf, int count)
{
int ret;
ret = serialDataBlock(fd, count);
if(ret<0)
return -1;
return read(fd, buf, count);
}
/*
* serialGetchar:
* Get a single character from the serial device.
* Note: Zero is a valid character and this function will time-out after
* 10 seconds.
*********************************************************************************
*/
int serialGetchar (int fd)
{
uint8_t x ;
if (read (fd, &x, 1) != 1)
return -1 ;
return ((int)x) & 0xFF ;
}