-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboteqDevice.cpp
More file actions
354 lines (285 loc) · 7.38 KB
/
Copy pathRoboteqDevice.cpp
File metadata and controls
354 lines (285 loc) · 7.38 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sstream>
#include <Windows.h>
#include "RoboteqDevice.h"
#include "ErrorCodes.h"
using namespace std;
#define BUFFER_SIZE 1024
#define MISSING_VALUE -1024
RoboteqDevice::RoboteqDevice()
{
handle = RQ_INVALID_HANDLE;
}
RoboteqDevice::~RoboteqDevice()
{
Disconnect();
}
bool RoboteqDevice::IsConnected()
{
return handle != RQ_INVALID_HANDLE;
}
int RoboteqDevice::Connect(string port)
{
if(IsConnected())
{
cout<<"Device is connected, attempting to disconnect."<<endl;
Disconnect();
}
//Open port.
cout<<"Opening port: '"<<port<<"'...";
HANDLE h = CreateFileA(port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
handle = (int) h;
if(h == INVALID_HANDLE_VALUE)
{
handle = RQ_INVALID_HANDLE;
cout<<"failed."<<endl;
return RQ_ERR_OPEN_PORT;
}
cout<<"succeeded."<<endl;
cout<<"Initializing port...";
InitPort();
cout<<"...done."<<endl;
int status;
string response;
cout<<"Detecting device version...";
status = IssueCommand("?", "$1E", 10, response);
if(status != RQ_SUCCESS)
{
cout<<"failed (issue ?$1E response: "<<status<<")."<<endl;
Disconnect();
return RQ_UNRECOGNIZED_DEVICE;
}
if(response.length() < 12)
{
cout<<"failed (unrecognized version)."<<endl;
Disconnect();
return RQ_UNRECOGNIZED_VERSION;
}
cout<<response.substr(8, 4)<<"."<<endl;
return RQ_SUCCESS;
}
void RoboteqDevice::Disconnect()
{
if(IsConnected())
CloseHandle((HANDLE) handle);
handle = RQ_INVALID_HANDLE;
}
void RoboteqDevice::InitPort()
{
if(!IsConnected())
return;
DCB comSettings;
COMMTIMEOUTS CommTimeouts;
// Set timeouts in milliseconds
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 100;
SetCommTimeouts((HANDLE) handle,&CommTimeouts);
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState((HANDLE) handle, &comSettings);
comSettings.BaudRate = 115200;
comSettings.StopBits = ONESTOPBIT;
comSettings.ByteSize = 8;
comSettings.Parity = NOPARITY;
comSettings.fParity = FALSE;
SetCommState((HANDLE) handle, &comSettings);
}
int RoboteqDevice::Write(string str)
{
if(!IsConnected())
return RQ_ERR_NOT_CONNECTED;
//cout<<"Writing: "<<ReplaceString(str, "\r", "\r\n");
DWORD written = 0;
int bStatus = WriteFile((HANDLE) handle, str.c_str(), str.length(), &written, NULL);
if (bStatus == 0)
return RQ_ERR_TRANSMIT_FAILED;
return RQ_SUCCESS;
}
int RoboteqDevice::ReadAll(string &str)
{
DWORD countRcv;
if(!IsConnected())
return RQ_ERR_NOT_CONNECTED;
char buf[BUFFER_SIZE + 1] = "";
str = "";
int i = 0;
while(ReadFile((HANDLE) handle, buf, BUFFER_SIZE, &countRcv, NULL) != 0)
{
str.append(buf, countRcv);
//No further data.
if(countRcv < BUFFER_SIZE)
break;
}
//if(countRcv > 0)
//{
// str.append(buf, countRcv);
//}
return RQ_SUCCESS;
}
int RoboteqDevice::IssueCommand(string commandType, string command, string args, int waitms, string &response, bool isplusminus)
{
int status;
string read;
response = "";
if(args == "")
status = Write(commandType + command + "\r");
else
status = Write(commandType + command + " " + args + "\r");
if(status != RQ_SUCCESS)
return status;
sleepms(waitms);
status = ReadAll(read);
if(status != RQ_SUCCESS)
return status;
if(isplusminus)
{
if(read.length() < 2)
return RQ_INVALID_RESPONSE;
response = read.substr(read.length() - 2, 1);
return RQ_SUCCESS;
}
string::size_type pos = read.rfind(command + "=");
if(pos == string::npos)
return RQ_INVALID_RESPONSE;
pos += command.length() + 1;
string::size_type carriage = read.find("\r", pos);
if(carriage == string::npos)
return RQ_INVALID_RESPONSE;
response = read.substr(pos, carriage - pos);
return RQ_SUCCESS;
}
int RoboteqDevice::IssueCommand(string commandType, string command, int waitms, string &response, bool isplusminus)
{
return IssueCommand(commandType, command, "", waitms, response, isplusminus);
}
int RoboteqDevice::SetConfig(int configItem, int index, int value)
{
string response;
char command[10];
char args[50];
if(configItem < 0 || configItem > 255)
return RQ_INVALID_CONFIG_ITEM;
sprintf_s(command, "$%02X", configItem);
sprintf_s(args, "%i %i", index, value);
if(index == MISSING_VALUE)
{
sprintf_s(args, "%i", value);
index = 0;
}
if(index < 0)
return RQ_INDEX_OUT_RANGE;
int status = IssueCommand("^", command, args, 10, response, true);
if(status != RQ_SUCCESS)
return status;
if(response != "+")
return RQ_SET_CONFIG_FAILED;
return RQ_SUCCESS;
}
int RoboteqDevice::SetConfig(int configItem, int value)
{
return SetConfig(configItem, MISSING_VALUE, value);
}
int RoboteqDevice::SetCommand(int commandItem, int index, int value)
{
string response;
char command[10];
char args[50];
if(commandItem < 0 || commandItem > 255)
return RQ_INVALID_COMMAND_ITEM;
sprintf_s(command, "$%02X", commandItem);
sprintf_s(args, "%i %i", index, value);
if(index == MISSING_VALUE)
{
if(value != MISSING_VALUE)
sprintf_s(args, "%i", value);
index = 0;
}
if(index < 0)
return RQ_INDEX_OUT_RANGE;
int status = IssueCommand("!", command, args, 10, response, true);
if(status != RQ_SUCCESS)
return status;
if(response != "+")
return RQ_SET_COMMAND_FAILED;
return RQ_SUCCESS;
}
int RoboteqDevice::SetCommand(int commandItem, int value)
{
return SetCommand(commandItem, MISSING_VALUE, value);
}
int RoboteqDevice::SetCommand(int commandItem)
{
return SetCommand(commandItem, MISSING_VALUE, MISSING_VALUE);
}
int RoboteqDevice::GetConfig(int configItem, int index, int &result)
{
string response;
char command[10];
char args[50];
if(configItem < 0 || configItem > 255)
return RQ_INVALID_CONFIG_ITEM;
if(index < 0)
return RQ_INDEX_OUT_RANGE;
sprintf_s(command, "$%02X", configItem);
sprintf_s(args, "%i", index);
int status = IssueCommand("~", command, args, 10, response);
if(status != RQ_SUCCESS)
return status;
istringstream iss(response);
iss>>result;
if(iss.fail())
return RQ_GET_CONFIG_FAILED;
return RQ_SUCCESS;
}
int RoboteqDevice::GetConfig(int configItem, int &result)
{
return GetConfig(configItem, 0, result);
}
int RoboteqDevice::GetValue(int operatingItem, int index, int &result)
{
string response;
char command[10];
char args[50];
if(operatingItem < 0 || operatingItem > 255)
return RQ_INVALID_OPER_ITEM;
if(index < 0)
return RQ_INDEX_OUT_RANGE;
sprintf_s(command, "$%02X", operatingItem);
sprintf_s(args, "%i", index);
int status = IssueCommand("?", command, args, 10, response);
if(status != RQ_SUCCESS)
return status;
istringstream iss(response);
iss>>result;
if(iss.fail())
return RQ_GET_VALUE_FAILED;
return RQ_SUCCESS;
}
int RoboteqDevice::GetValue(int operatingItem, int &result)
{
return GetValue(operatingItem, 0, result);
}
string ReplaceString(string source, string find, string replacement)
{
string::size_type pos = 0;
while((pos = source.find(find, pos)) != string::npos)
{
source.replace(pos, find.size(), replacement);
pos++;
}
return source;
}
void sleepms(int milliseconds)
{
Sleep(milliseconds);
}