-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.cpp
279 lines (225 loc) · 9.02 KB
/
serial.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
//
// Created by Mingcan Li on 2/10/22.
//
#include "serial.h"
Serial::Serial() {}
Serial::Serial(const char* portName ) {
serial.setPortName(portName);
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
while(!serial.isOpen()) serial.open(QIODevice::ReadWrite);
usleep(3000000);
}
void Serial::open(const char* portName) {
serial.setPortName(portName);
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
while(!serial.isOpen()) serial.open(QIODevice::ReadWrite);
usleep(3000000);
}
/**
* @brief Serial::sendMessage Send serial message
* @param message Message to send
*/
void Serial::send(QByteArray Message) {
if (serial.isOpen() && serial.isWritable()) {
serial.write(Message);
serial.flush();
serial.waitForBytesWritten(1000);
}
}
QByteArray Serial::read() {
serial.waitForReadyRead(1000);
return serial.readAll();
}
//-------------------------------------------------------------------------------------------
SerialThread::SerialThread(const char* portName) {
this->portName=portName;
currState = IDLE; // Start in idle state
}
void SerialThread::run() {
//qDebug()<<"start SerialThread";
this->serial = new Serial(portName.c_str());
while(true) {
switch(currState) {
case IDLE:
currState = idleState();
break;
case READY:
currState = readyState();
break;
case CHARGE:
currState = chargeState();
break;
case DISCHARGE:
currState = dischargeState();
break;
case ERROR:
// Wait for user to resume or cancel the test and change the state
break;
default:
//qDebug()<<"Warning: Default state was reached";
usleep(2000000);
break;
}
}
}
int SerialThread::idleState(){
QByteArray receive = serial->read();
if(receive.contains("ready")) {
batReady = 1;
emit testerStateChange(READY, portName);
qDebug()<<"ready: "<<receive;
return READY;
} else if(receive.contains("error")) {
qDebug()<<"error: "<<receive;
emit error(portName, receive.toStdString().c_str(), currState);
return ERROR;
}
return IDLE;
}
int SerialThread::readyState() {
QByteArray receive=serial->read();
if(receive.contains("charge")) {
qDebug()<<"charge: "<<receive;//<<QString::fromStdString(portName);
this->csv = new CSVWriter();
csv->open(fileName.c_str());
emit testerStateChange(CHARGE, portName);
return CHARGE;
} else if(receive.contains("error")) {
qDebug()<<"error: "<<receive;
emit error(portName, receive.toStdString().c_str(), currState);
return ERROR;
}
return READY;
}
/**
* @brief SerialThread::startTest Handle signalStartTest signal sent from SerialHub when battery is ready and start button is pressed.
* Sets fileName, discharge current, and sends starttest message to Arduino to transition into the charge state
* @param port Serial port of the tester which is to start a test
* @param batteryID ID of the battery which is to be tested
* @param current Discharge current for the test
*/
void SerialThread::startTest(std::string port, const char* batteryID, double dischargeCurrent) {
if(port == portName) {
fileName = "../Battery Testing Data/";
if(std::string(batteryID).length() >= 6)
fileName += std::string(batteryID).substr(std::string(batteryID).length()-6, 6) + ".csv";
else
fileName += std::string(batteryID) + ".csv";
current = dischargeCurrent;
std::string toSend = "starttest,";
toSend += std::to_string(current);
//qDebug()<<QString::fromStdString(toSend);
serial->send(toSend.c_str());
}
}
int SerialThread::chargeState() {
QByteArray receive = serial->read();
if(receive.contains("discharge")) {
qDebug()<<"discharge: "<<receive;
csv->write("Voltage (V),Current (mA),Shunt Voltage (mV),Therm 1 (C),Therm 2 (C),Therm 3 (C)\n"); // Add headers to csv file
emit testerStateChange(DISCHARGE, portName);
return DISCHARGE;
} else if(receive.contains("error")) {
qDebug()<<"error: "<<receive;
emit error(portName, receive.toStdString().c_str(), currState);
return ERROR;
} else if(!receive.contains("charge") && !receive.isEmpty()) {
emit voltageChange(portName, receive.split('\n')[0]);//.split(',')[0]);//receiveStr.substr(0, receiveStr.find(",") + 1)); // TODO
}
// TODO emit voltageChange(portName, receive.split(',')[0]);
//QString receiveStr = QString::fromStdString(receive.toStdString()); // TODO
//std::string receiveStr = receive.toStdString(); // TODO
//if(receive.contains(",")) TODO
return CHARGE;
}
int SerialThread::dischargeState() {
QByteArray receive = serial->read();
if(receive.contains("error")) {
qDebug()<<"error: "<<receive;
emit error(portName, receive.toStdString().c_str(), currState);
return ERROR;
} else if(receive.contains("finish")) {
qDebug()<<"finish: "<<receive;
csv->close();
delete csv;
batReady = 0;
// TODO Remove emit testerStateChange(IDLE, portName); // TODO change to FINISH when finish state is added (to account for possible wait in firmware)
emit voltageChange(portName, ""); // TODO
emit testEnded(portName);
return IDLE;
} else {
if(!receive.contains("discharge")) {
csv->write(receive);
qDebug()<<receive;
//QString receiveStr = QString::fromStdString(receive.toStdString()); // TODO
//std::string receiveStr = receive.toStdString(); // TODO
if(receive.contains(",")) {
if(!receive.contains('\n')) {
emit voltageChange(portName, receive.split(',')[0]);//receiveStr.substr(0, receiveStr.find(",") + 1)); // TODO
}
}
}
return DISCHARGE;
}
}
/**
* @brief SerialThread::handleErrorDecision Handles user's decision to resume or cancel the test after an error. Triggered by errorDecisionMade()
* @param port Serial port of tester with the error being handled
* @param prevState State tester was in when error was thrown
* @param errorDecision int indicating user's decision to resume test (QMessageBox::Retry) or cancel test (QMessageBox::Cancel)
*/
void SerialThread::handleErrorDecision(std::string port, int prevState, int decision) {
if(port == this->portName) {
if(decision == QMessageBox::Retry) { // Resume test from previous state
//qDebug()<<"SerialThread beginning of resumeTest() (for correct port/tester) "<<QString::fromStdString(portName);
switch(prevState) {
case IDLE:
serial->send("resume:idle");
qDebug()<<"resume: idle";
break;
case READY:
serial->send("resume:ready");
qDebug()<<"resume: ready";
break;
case CHARGE:
serial->send("resume:charge");
qDebug()<<"resume: charge";
break;
case DISCHARGE:
serial->send("resume:discharge");
qDebug()<<"resume: discharge";
break;
default:
qDebug()<<"Warning: The previous state in which an error was thrown was not idle, ready, charge, or discharge";
break;
}
currState = prevState;
} else if(decision == QMessageBox::Abort) { // Abort test and return to idle state
//qDebug()<<"SerialThread beginning of cancelTest() (for correct port/tester) "<<QString::fromStdString(portName);
if(prevState != IDLE) {
//qDebug()<<"Error didn't come from idle state";
if(prevState != READY) {
//qDebug()<<"Error didn't come from ready state";
csv->remove(fileName.c_str());
delete csv;
emit voltageChange(portName, ""); // TODO Move outside of prevState != READY (to directly above testEnded(portName)) ???
}
batReady = 0;
emit testEnded(portName);
}
qDebug()<<"SerialThread abort test (resume: idle)";
serial->send("resume:idle");
currState = IDLE;
}
}
}