-
Notifications
You must be signed in to change notification settings - Fork 0
/
972b.cpp
393 lines (342 loc) · 13.8 KB
/
972b.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "972b.h"
const String PressureTransducer::INCOMPLETE_RESPONSE = "ResponseTimeout";
const String PressureTransducer::RESPONSE_TOO_LONG = "LengthExceeded";
PressureTransducer::PressureTransducer(String addr, HardwareSerial& serial)
: deviceAddress(addr.length() > 0 ? addr : DEFAULT_ADDR),
serialPort(serial), responseTimeout(3000) {
}
// List of possible NACK codes
NAKCode PressureTransducer::nakCodes[] = {
{8, "Zero adjustment at too high pressure"},
{9, "Atmospheric adjustment at too low pressure"},
{160, "Unrecognized message"},
{169, "Invalid argument"},
{172, "Value out of range"},
{175, "Command/query character invalid"},
{180, "Protected setting (locked)"},
{195, "Control setpoint enabled (ENC)"}
};
int PressureTransducer::getNumNackCodes() {
return sizeof(nakCodes) / sizeof(NAKCode);
}
void PressureTransducer::sendCommand(String command, String parameter) {
if (command.length() == 0) return;
command.trim(); // Remove any leading or trailing whitespace
String fullCommand = "@" + this->deviceAddress + command;
if (command.endsWith("?")) { // it's a Query
fullCommand += ";FF";
} else { // it's a Command
fullCommand += "!" + parameter + ";FF";
}
serialPort.print(fullCommand);
serialPort.flush();
Serial.println("Sent command: " + fullCommand);
}
// Function to find the description for a given NAK code
NACKResult PressureTransducer::decodeNAK(String codeStr) {
int code = codeStr.toInt(); // Convert the String to an integer
NACKResult result;
result.found = false;
for (unsigned int i = 0; i < sizeof(nakCodes) / sizeof(NAKCode); i++) {
if (nakCodes[i].code == code) {
result.description = nakCodes[i].description;
result.found = true;
}
}
if (!result.found) {
result.description = "Unknown NAK code";
}
return result;
}
String PressureTransducer::readResponse() {
String response = "";
long startTime = millis();
while (millis() - startTime < responseTimeout) {
if (serialPort.available()) {
char c = serialPort.read();
if (response.length() < maxResponseLength) {
response += c; // add most recent character to response
if (response.endsWith(";FF")) {
break; // correctly formed response received
}
} else {
Serial.println(RESPONSE_TOO_LONG);
return RESPONSE_TOO_LONG;
}
}
}
if (response.length() == 0) {
Serial.println("Response incomplete or timeout.");
return INCOMPLETE_RESPONSE; // time-out or incomplete response
}
return response;
}
String PressureTransducer::parseResponse(const String& response) {
if (response.length() == 0) {
return "Error:None";
}
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
return response;
}
if (response.startsWith("@" + this->deviceAddress + "ACK")) {
int startIndex = response.indexOf("ACK") + 3;
int endIndex = response.indexOf(';', startIndex);
if (endIndex != -1) { // found the termination character
return response.substring(startIndex, endIndex); // return core info
} else {
return "Error:ACKmalformed";
}
}
else if (response.startsWith("@" + this->deviceAddress + "NAK")) {
int startIndex = response.indexOf("NAK") + 3; // Add three to move past "NAK"
int endIndex = response.indexOf(';', startIndex);
if (endIndex != -1) { // found the termination character
return response.substring(startIndex, endIndex);
} else {
return "Error:NAKmalformed";
}
} else {
Serial.println(response);
return "UnknownErr"; // unrecognized error
}
}
CommandResult PressureTransducer::status() {
CommandResult result; // info variable to return to caller
result.outcome = false; // Assume failure unless proven otherwise
Serial.println("Sending status query");
sendCommand("T?");
String response = readResponse();
Serial.print("Received status response:"); Serial.println(response);
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
return result; // Exit early
} else if (response.startsWith("@" + this->deviceAddress + "ACK")) {
result.outcome = true; // Indicate success to function caller
result.resultStr = parseResponse(response);
} else {
// Indicate failure to function caller
result.resultStr = parseResponse(response);
}
return result;
}
void PressureTransducer::changeBaudRate(String newBaudRate) {
// Array of valid baud rates
const long validBaudRates[] = {4800, 9600, 19200, 38400, 57600, 115200, 230400};
const int numRates = sizeof(validBaudRates) / sizeof(validBaudRates[0]);
bool isValidRate = false;
// Check if rate is valid
for (int i = 0; i < numRates; i++) {
if (newBaudRate.toInt() == validBaudRates[i]) {
isValidRate = true;
break;
}
}
if (!isValidRate) {
Serial.println("Invalid baud rate: " + newBaudRate);
return;
}
// Proceed with the command
sendCommand("BR", newBaudRate);
// Proceed to change the baud rate if valid
serialPort.end(); // End current communication
serialPort.begin(newBaudRate.toInt()); // Start with the new baud rate
Serial.println("Baud rate changed to: " + newBaudRate);
String response = readResponse();
// Check for critical communication errors first
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
Serial.println(response); // Print the error and return immediately
return;
}
String parsedResponse = parseResponse(response);
if (!parsedResponse.startsWith("Error")) {
Serial.println("Baud rate change successful: " + parsedResponse);
} else {
Serial.println(parsedResponse); // Print error message handled by parseResponse
}
}
void PressureTransducer::setRS485Delay(String delaySetting) {
sendCommand("RSD", delaySetting);
String response = readResponse();
String parsedResponse = parseResponse(response);
if (!parsedResponse.startsWith("Error")) {
Serial.println("RS485 Delay setting updated to: " + delaySetting);
} else {
Serial.println(parsedResponse);
}
}
void PressureTransducer::queryRS485Delay() {
sendCommand("RSD?");
String response = readResponse();
// Immediately handle communication error responses
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
Serial.println(response);
return;
}
String parsedResponse = parseResponse(response);
if (!parsedResponse.startsWith("Error")) {
Serial.println("Current RS485 Delay Setting: " + parsedResponse);
} else {
Serial.println(parsedResponse); // Print error messages handled by parseResponse
}
}
void PressureTransducer::printResponse(const String& response) { // to serial
// Immediately handle communication error responses
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
Serial.println(response);
return;
}
String parsedResponse = parseResponse(response);
if (parsedResponse.startsWith("Error")) {
Serial.println(parsedResponse); // Print the error message from parseResponse
} else if (response.startsWith("@" + this->deviceAddress)) {
if (response.indexOf("ACK") != -1) {
// If the response is acknowledgment, print it directly
Serial.println("ACK Response: " + parsedResponse);
} else if (response.indexOf("NAK") != -1) {
// The response is negative acknowledgment
Serial.println("NAK Error: " + parsedResponse);
}
} else {
// Handle unrecognized prefixes or other issues not caught by parseResponse
Serial.println("No valid response received or unrecognized prefix.");
}
}
bool PressureTransducer::checkForLockError(String response) {
if (response.startsWith("@" + this->deviceAddress + "NAK180")) {
Serial.println("Error: Transducer is locked. Unlock required to change parameters.");
return true; // Indicates a lock error was detected
}
return false; // No lock error
}
CommandResult PressureTransducer::setupSetpoint(int setpointNumber,
String setpoint,
String direction,
String hysteresis,
String enableMode) {
CommandResult result;
result.outcome = false; // Assume failure unless proven otherwise
// Construct command prefixes based on setpoint number
String spCmd = "SP" + String(setpointNumber);
String sdCmd = "SD" + String(setpointNumber);
String shCmd = "SH" + String(setpointNumber);
String enCmd = "EN" + String(setpointNumber);
// Step 1: Set the setpoint value
sendCommand(spCmd, setpoint);
String response = readResponse();
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
return result;
}
if (!response.startsWith("@" + this->deviceAddress + "ACK")) {
result.resultStr = parseResponse(response);
return result; // Early return on failure
}
// Step 2: Set the setpoint direction (ABOVE/BELOW)
sendCommand(sdCmd, direction);
response = readResponse();
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.outcome = false;
result.resultStr = response;
return result;
}
if (!response.startsWith("@" + this->deviceAddress + "ACK")) {
result.resultStr = parseResponse(response);
return result; // Early return on failure
}
// Step 3: Set the setpoint hysteresis value
sendCommand(shCmd, hysteresis);
response = readResponse();
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
return result;
}
if (!response.startsWith("@" + this->deviceAddress + "ACK")){
result.resultStr = parseResponse(response);
return result; // Early return on failure
}
// Step 4: Enable the setpoint
sendCommand(enCmd, enableMode);
response = readResponse();
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
return result; // Early return on failure
}
if (!response.startsWith("@" + this->deviceAddress + "ACK")) {
result.resultStr = parseResponse(response);
return result; // Early return on failure
}
// if everything is ok
result.outcome = true;
result.resultStr = "972bOK";
return result;
}
CommandResult PressureTransducer::requestPressure(String measureType) {
sendCommand(measureType + "?");
String response = readResponse();
CommandResult result;
result.outcome = false; // Assume failure unless proven otherwise
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
return result;
}
String parsedResponse = parseResponse(response);
if (response.startsWith("@" + this->deviceAddress + "ACK")) {
double pressureValue = sciToDouble(parsedResponse); // Convert to double
if (isnan(pressureValue)) {
result.resultStr = "Error:Invalid pressure reading";
} else {
result.outcome = true;
result.resultStr = parsedResponse;
}
} else {
result.resultStr = parsedResponse;
}
return result;
}
void PressureTransducer::printPressure(String measureType) {
sendCommand(measureType + "?");
String response = readResponse();
Serial.println(response);
}
CommandResult PressureTransducer::setPressureUnits(String units) {
sendCommand("U", units);// datasheet p.43
String response = readResponse();
CommandResult result; // info to return to caller
result.outcome = false; // Initially assume the outcome is false
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
} else if (response.startsWith("@" + this->deviceAddress + "ACK")){
result.outcome = true; // Indicate success
result.resultStr = parseResponse(response);
} else {
// Indicate NACK failure to function caller
result.resultStr = parseResponse(response);
}
return result;
}
CommandResult PressureTransducer::setUserTag(String tag) {
String command = "UT";
CommandResult result;
result.outcome = false; // Assume failure unless proven otherwise
sendCommand(command, tag);
String response = readResponse();
if (response == INCOMPLETE_RESPONSE || response == RESPONSE_TOO_LONG) {
result.resultStr = response;
} else if (response.startsWith("@" + this->deviceAddress + "ACK")) {
result.outcome = true;
result.resultStr = parseResponse(response);
} else {
// Indicate failure to caller
result.resultStr = parseResponse(response); // for the LCD
}
return result;
}
void PressureTransducer::setResponseTimeout(unsigned long timeout) {
responseTimeout = timeout;
}
// Function to parse string and perform conversion to double
double PressureTransducer::sciToDouble(String sciString) {
char buffer[sciString.length() + 1]; // Create a buffer to store the string as a char array
sciString.toCharArray(buffer, sizeof(buffer)); // Convert string to char array
return strtod(buffer, NULL); // Convert char array to double handling scientific notation
}