-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjfyserial.cpp
274 lines (211 loc) · 5.01 KB
/
jfyserial.cpp
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
#include "jfyserial.h"
#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "jfyexception.h"
using namespace std;
using namespace Jfy;
Serial::Serial()
: _handle( -1 ),
_readTimeout( 1 )
{
}
Serial::Serial( const string& device )
: _device( device ),
_handle( -1 ),
_readTimeout( 1 )
{
}
bool Serial::setDevice( const string& device )
{
if ( isOpen() ) {
cerr << "Device is open." << endl;
return false;
}
_device = device;
return true;
}
string Serial::device() const
{
return _device;
}
void Serial::setReadTimeout( int seconds )
{
_readTimeout = seconds;
}
int Serial::readTimeout() const
{
return _readTimeout;
}
bool Serial::open()
{
if ( _device.empty() ) {
cerr << "Device is not set." << endl;
return false;
}
if ( isOpen() ) {
cerr << "Device is already open." << endl;
return true;
}
struct termios options;
// Open the device
_handle = ::open( _device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
if ( _handle == -1 ) {
cerr << "Cannot open " << _device << "." << endl;
return false;
}
fcntl( _handle, F_SETFL, 0 );
// Set a bunch of options on the serial device
tcgetattr( _handle, &options );
cfsetispeed( &options, B9600 );
cfsetospeed( &options, B9600 );
cfmakeraw( &options );
options.c_cc[ VMIN ] = 0;
options.c_cc[ VTIME ] = 10;
tcsetattr( _handle, TCSANOW, &options );
return true;
}
void Serial::close()
{
if ( _handle == -1 )
return;
::close( _handle );
_handle = -1;
}
bool Serial::isOpen() const
{
return _handle != -1;
}
bool Serial::sendRequest( const Data& request )
{
if ( !isOpen() ) {
cerr << "Device is not open." << endl;
return false;
}
PacketData packetData = request.packetData();
size_t bytesWritten = write( _handle, packetData.data(), packetData.size() );
if ( bytesWritten == -1 ) {
cerr << "Could not write to the device." << endl;
return false;
} else if ( bytesWritten < packetData.size() ) {
cerr << "Short write to device." << endl;
return false;
}
return true;
}
Data Serial::readResponse()
{
Data response;
try {
if ( !isOpen() )
throw Exception( "Device is not open." );
// Read the header
if ( !waitForBytesReady( 7 ) )
throw Exception( "Device timed out." );
unsigned char header[ 7 ];
readData( header, sizeof( header ) );
if ( header[ 0 ] != 0xa5 || header[ 1 ] != 0x0a5 )
throw Exception( "Response header is invalid." );
response.setSourceAddress( header[ 2 ] );
response.setDestinationAddress( header[ 3 ] );
response.setControlCode( header[ 4 ] );
response.setFunctionCode( header[ 5 ] );
int dataSize = static_cast< int >( header[ 6 ] );
// Read the data (if any)
if ( dataSize > 0 ) {
if ( !waitForBytesReady( dataSize ) )
throw Exception( "Device timed out." );
char* responseData = new char[ dataSize ];
readData( responseData, dataSize );
response.setData( responseData, dataSize );
delete [] responseData;
}
unsigned short checksum = readUnsignedShort();
unsigned short footer;
readData( &footer, 2 );
if ( checksum != response.checksum() )
throw Exception( "Checksum mismatch" );
}
catch ( Exception e ) {
cerr << e.what() << endl;
return Data();
}
return response;
}
Data Serial::sendRequestReadResponse( const Data& request )
{
Data response;
for ( int retry = 0; retry < 3; ++retry ) {
if ( !sendRequest( request ) )
continue;
response = readResponse();
if ( response.isValid() )
break;
}
return response;
}
bool Serial::waitForBytesReady( int numBytes ) const
{
int bytes = 0;
time_t startTime = time( 0 );
while ( time( 0 ) - startTime < _readTimeout ) {
if ( ioctl( _handle, FIONREAD, &bytes ) == -1 ) {
cerr << "Cannot determine number of bytes waiting on the device." << endl;
return false;
}
if ( bytes >= numBytes )
break;
}
return bytes >= numBytes;
}
void Serial::readData( void* buffer, size_t size )
{
if ( !waitForBytesReady( size ) )
throw Exception( "Timed out waiting for data to become available." );
char* buf = (char*)buffer;
time_t startTime = time( 0 );
while ( size > 0 && time( 0 ) - startTime < _readTimeout ) {
int bytesRead = read( _handle, buf, size );
if ( bytesRead == -1 )
throw Exception( "Could not read from the device." );
buf += bytesRead;
size -= bytesRead;
}
if ( size > 0 )
throw Exception( "Read timed out." );
}
short Serial::readShort()
{
short s;
try {
if ( !waitForBytesReady( 2 ) )
throw Exception( "Device timed out." );
readData( &s, sizeof( s ) );
s = ( ( s & 0xff00 ) >> 8 ) | ( ( s & 0x00ff ) << 8 );
}
catch ( Exception e ) {
cerr << e.what() << endl;
return 0;
}
return s;
}
unsigned short Serial::readUnsignedShort()
{
unsigned short s;
try {
if ( !waitForBytesReady( 2 ) )
throw Exception( "Device timed out." );
readData( &s, sizeof( s ) );
s = ( ( s & 0xff00 ) >> 8 ) | ( ( s & 0x00ff ) << 8 );
}
catch ( Exception e ) {
cerr << e.what() << endl;
return 0;
}
return s;
}