forked from hydronautics-team/PoolsideGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_client.cpp
More file actions
116 lines (94 loc) · 2.96 KB
/
serial_client.cpp
File metadata and controls
116 lines (94 loc) · 2.96 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
#include "serial_client.h"
#include "UV/iserverdata.h"
#include "SettingsWindow/ThrusterSettings/thrustersettings.h"
#include <QString>
#include <QDebug>
const int MAX_COM_ID = 20;
Serial_Client::Serial_Client()
{
// timeoutTimer = new QTimer();
// connect(timeoutTimer, SIGNAL(timeout()), this, SLOT(timerTick()));
interface = new IServerData();
}
bool Serial_Client::portConnect(int port)
{
QString str;
#ifdef unix
str = "/dev/ttyUSB";
#else
str = "COM";
#endif
str.append(QString::number(port));
// qDebug () << "COM_SERVER: Trying to open port " << str;
serialPort = new QSerialPort(str);
serialPort->setBaudRate(QSerialPort::BaudRate::Baud57600, QSerialPort::AllDirections);
serialPort->setDataBits(QSerialPort::DataBits::Data8);
serialPort->setParity(QSerialPort::Parity::NoParity);
serialPort->setStopBits(QSerialPort::StopBits::OneStop);
serialPort->setFlowControl(QSerialPort::FlowControl::NoFlowControl);
if(serialPort->open(QIODevice::ReadWrite)) {
qDebug() << "COM_SERVER: port" << str << "successfully opened!";
}
else {
// qDebug() << " serial port openning error";
// qDebug() << serialPort->error();
delete serialPort;
return false;
}
return true;
}
void Serial_Client::run()
{
bool opened = false;
for(int i=0; i<MAX_COM_ID; i++) {
opened = portConnect(i);
if(opened) {
break;
}
}
if(opened) {
exec();
}
}
int Serial_Client::exec()
{
while(1)
{
int messageType = MESSAGE_NORMAL;
QByteArray msg;
msg = interface->generateMessage(messageType);
// qDebug() << "[SERIAL_CLIENT] Sending message type " << messageType << "||" << msg.size();
// qDebug() << msg;
serialPort->clear();
serialPort->write(msg, msg.size());
serialPort->flush();
serialPort->waitForReadyRead(100);
long long bytesAvailiable = serialPort->bytesAvailable();
if (bytesAvailiable > 0) {
msg.clear();
msg.push_back(serialPort->readAll());
// qDebug() << "[SERIAL_CLIENT] Message received. Bytes: " << msg.size();
bool exception_caught = false;
try {
interface->parseMessage(msg, messageType);
}
catch(const std::invalid_argument& error) {
// qDebug() << "[SERIAL_CLIENT] Parsing error: " << error.what();
exception_caught = true;
}
if(!exception_caught) {
//emit dataUpdated();
}
}
else {
// qDebug() << "[SERIAL_CLIENT] Didn't receive answer for message " << messageType;
// qDebug() << "[SERIAL_CLIENT] Bytes available:" << bytesAvailiable;
serialPort->readAll();
}
emit dataUpdated();
}
}
void Serial_Client::changeSelectedThruster(unsigned int slot)
{
interface->changeCurrentThruster(slot);
}