-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserial.cpp
More file actions
311 lines (228 loc) · 5.82 KB
/
serial.cpp
File metadata and controls
311 lines (228 loc) · 5.82 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <math.h>
#include <stdio.h>
#include <sys/file.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#ifndef __WIN32__
#include <sys/ioctl.h>
#include <termios.h>
#include <bits/time.h>
#endif
#include "serial.h"
SerialClass::SerialClass()
{
#ifdef __WIN32__
hComm = INVALID_HANDLE_VALUE;
#else
hComm = -1;
#endif
} /* SerialClass::SerialClass */
SerialClass::~SerialClass()
{
#ifdef __WIN32__
if ( hComm != INVALID_HANDLE_VALUE ) {
CloseHandle( hComm );
}
#else
if ( hComm >= 0 ) {
close( hComm );
}
#endif
} /* SerialClass::~SerialClass */
SerialClass::ErrorType SerialClass::OpenPort(const char* name, int rate, int handshake)
{
#ifdef __WIN32__
/* serial device open for Win32 */
DCB dcbSerialParams;
if( hComm != INVALID_HANDLE_VALUE )
{
CloseHandle( hComm );
}
hComm = CreateFile( name, // port name
GENERIC_READ | GENERIC_WRITE, // Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL ); // Null for Comm Devices
if ( hComm == INVALID_HANDLE_VALUE )
{
return( ERROR_OPENING );
}
if ( !GetCommState( hComm, &dcbSerialParams ) )
{
return( ERROR_CONFIG );
}
dcbSerialParams.BaudRate = rate; // Setting BaudRate = rate
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
if( handshake )
{
dcbSerialParams.fOutxCtsFlow = TRUE;
dcbSerialParams.fOutxDsrFlow = TRUE;
dcbSerialParams.fRtsControl = RTS_CONTROL_HANDSHAKE;
printf( "Hardware handshake enabled.\n" );
}
else
{
dcbSerialParams.fOutxCtsFlow = FALSE;
dcbSerialParams.fOutxDsrFlow = FALSE;
dcbSerialParams.fRtsControl = RTS_CONTROL_ENABLE;
}
if( !SetCommState( hComm, &dcbSerialParams ) )
{
return ERROR_CONFIG;
}
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 2;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 2;
if( !SetCommTimeouts( hComm, &timeouts ) )
{
return ERROR_CONFIG;
}
#else
struct termios tty;
/* serial device open routine for Linux */
hComm = open( name , O_RDWR|O_NOCTTY );
if ( hComm == -1 )
{
return( ERROR_OPENING );
}
/* configure the serial device */
tcgetattr( hComm, &tty );
/* setup serial UART (this works best for me -Lameguy64) */
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8-bit characters
tty.c_cflag &= ~PARENB; // no parity bit
tty.c_cflag &= ~CSTOPB; // only need 1 stop bit
tty.c_cflag &= ~CRTSCTS; // no hardware flowcontrol
// setup for non-canonical mode
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
/* set flow control if enabled */
if( handshake )
{
printf( "Hardware handshake enabled.\n" );
tty.c_cflag |= CRTSCTS;
}
/* ignore parity errors */
tty.c_iflag = IGNPAR;
/* set the baud rate */
cfsetspeed( &tty, (speed_t)rate );
/* fetch bytes as they become available */
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 2;
/* apply port configuration */
if ( tcsetattr( hComm, TCSANOW, &tty ) != 0 )
{
return( ERROR_CONFIG );
}
#endif
return( OK );
} /* SerialClass::OpenPort */
SerialClass::ErrorType SerialClass::SetRate(int rate) {
#ifdef __WIN32__
DCB dcbSerialParams;
if ( hComm == INVALID_HANDLE_VALUE ) {
return ERROR_NOT_OPEN;
}
if( !GetCommState( hComm, &dcbSerialParams ) )
{
return ERROR_CONFIG;
}
dcbSerialParams.BaudRate = rate; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
if ( !SetCommState( hComm, &dcbSerialParams ) )
{
return ERROR_CONFIG;
}
#else
struct termios tty;
if ( hComm == -1 )
{
return ERROR_NOT_OPEN;
}
tcgetattr( hComm, &tty );
cfsetspeed( &tty, (speed_t)rate );
if ( tcsetattr( hComm, TCSANOW, &tty ) != 0 )
{
return( ERROR_CONFIG );
}
#endif
return( OK );
}
int SerialClass::PendingBytes()
{
#ifdef __WIN32__
DWORD dwErrorFlags;
COMSTAT ComStat;
ClearCommError(hComm, &dwErrorFlags, &ComStat);
return( (int)ComStat.cbInQue );
#else
int bytes;
ioctl(hComm, FIONREAD, &bytes);
return( bytes );
#endif
} /* SerialClass::PendingBytes */
int SerialClass::SendBytes(void* data, int length)
{
#ifdef __WIN32__
DWORD bytesWritten;
if ( !WriteFile( hComm, data, length, &bytesWritten, NULL ) ) {
return -1;
}
#else
int bytesWritten;
bytesWritten = write(hComm, data, length);
#endif
return( bytesWritten );
} /* SerialClass::SendBytes */
int SerialClass::ReceiveBytes(void* data, int bytes)
{
#ifdef __WIN32__
DWORD bytesReceived;
if( !ReadFile( hComm, data, bytes, &bytesReceived, NULL ) )
{
return( -1 );
}
#else
int bytesReceived;
struct timeval timeout;
fd_set read_fds, write_fds, except_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(hComm, &read_fds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if ( select(hComm+1, &read_fds, &write_fds, &except_fds, &timeout) == 1 ) {
bytesReceived = read(hComm, data, bytes);
} else {
return( -1 );
}
#endif
return( bytesReceived );
} /* SerialClass::ReceiveBytes */
void SerialClass::ClosePort() {
#ifdef __WIN32__
if ( hComm != INVALID_HANDLE_VALUE ) {
CloseHandle( hComm );
hComm = INVALID_HANDLE_VALUE;
}
#else
if ( hComm >= 0 ) {
close( hComm );
hComm = -1;
}
#endif
}