| 352 |
Kevin |
1 |
#include "SerialHelper.h"
|
|
|
2 |
|
|
|
3 |
SerialHelper::SerialHelper(QObject *parent) : QObject(parent)
|
|
|
4 |
{
|
|
|
5 |
connected = false;
|
|
|
6 |
speeds << "9600" << "19200" << "38400" << "57600" << "115200" << "230400" << "250000";
|
|
|
7 |
dataBits << "8 Bits" << "7 Bits" << "6 Bits" << "5 Bits";
|
|
|
8 |
stopBits << "1 Bit" << "1.5 Bits" << "2 Bits";
|
|
|
9 |
parity << "None" << "Even" << "Odd" << "Space" << "Mark";
|
|
|
10 |
flowControl << "None" << "Hardware" << "Software";
|
|
|
11 |
}
|
|
|
12 |
|
|
|
13 |
SerialHelper::~SerialHelper()
|
|
|
14 |
{
|
|
|
15 |
serialPort->close();
|
|
|
16 |
delete serialPort;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
void SerialHelper::Serial_QueryParameters()
|
|
|
20 |
{
|
|
|
21 |
QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
|
|
|
22 |
QStringList ports;
|
|
|
23 |
for (int i = 0; i < portsList.size(); i++) {
|
|
|
24 |
ports.append(portsList[i].portName());
|
|
|
25 |
}
|
|
|
26 |
emit Serial_UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
void SerialHelper::Serial_Connect(QString port, QString speed, QString dataBits,
|
|
|
30 |
QString stopBits, QString parity, QString flowControl)
|
|
|
31 |
{
|
|
|
32 |
if (!connected) {
|
|
|
33 |
serialPort = new QSerialPort();
|
|
|
34 |
connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
|
|
|
35 |
connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(Serial_ProcessError(QSerialPort::SerialPortError)));
|
|
|
36 |
serialPort->setPortName(port);
|
|
|
37 |
if (serialPort->open(QIODevice::ReadWrite)) {
|
|
|
38 |
connected = true;
|
|
|
39 |
bool ok = false;
|
|
|
40 |
|
|
|
41 |
int baud = speed.toInt(&ok);
|
|
|
42 |
if (ok) serialPort->setBaudRate(baud);
|
|
|
43 |
|
|
|
44 |
if (dataBits == "5 Bits")
|
|
|
45 |
serialPort->setDataBits(QSerialPort::Data5);
|
|
|
46 |
else if (dataBits == "6 Bits")
|
|
|
47 |
serialPort->setDataBits(QSerialPort::Data6);
|
|
|
48 |
else if (dataBits == "7 Bits")
|
|
|
49 |
serialPort->setDataBits(QSerialPort::Data7);
|
|
|
50 |
else
|
|
|
51 |
serialPort->setDataBits(QSerialPort::Data8);
|
|
|
52 |
|
|
|
53 |
if (stopBits == "1.5 Bits")
|
|
|
54 |
serialPort->setStopBits(QSerialPort::OneAndHalfStop);
|
|
|
55 |
else if (stopBits == "2 Bits")
|
|
|
56 |
serialPort->setStopBits(QSerialPort::TwoStop);
|
|
|
57 |
else
|
|
|
58 |
serialPort->setStopBits(QSerialPort::OneStop);
|
|
|
59 |
|
|
|
60 |
if (parity == "Even")
|
|
|
61 |
serialPort->setParity(QSerialPort::EvenParity);
|
|
|
62 |
else if (parity == "Odd")
|
|
|
63 |
serialPort->setParity(QSerialPort::OddParity);
|
|
|
64 |
else if (parity == "Space")
|
|
|
65 |
serialPort->setParity(QSerialPort::SpaceParity);
|
|
|
66 |
else if (parity == "Mark")
|
|
|
67 |
serialPort->setParity(QSerialPort::MarkParity);
|
|
|
68 |
else
|
|
|
69 |
serialPort->setParity(QSerialPort::NoParity);
|
|
|
70 |
|
|
|
71 |
if (flowControl == "Hardware")
|
|
|
72 |
serialPort->setFlowControl(QSerialPort::HardwareControl);
|
|
|
73 |
else if (flowControl == "Software")
|
|
|
74 |
serialPort->setFlowControl(QSerialPort::SoftwareControl);
|
|
|
75 |
else
|
|
|
76 |
serialPort->setFlowControl(QSerialPort::NoFlowControl);
|
|
|
77 |
|
|
|
78 |
// See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
|
|
|
79 |
serialPort->setDataTerminalReady(1);
|
|
|
80 |
|
|
|
81 |
if (ok) emit Serial_Connected();
|
|
|
82 |
else Serial_Disconnect();
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
void SerialHelper::Serial_Disconnect()
|
|
|
88 |
{
|
|
|
89 |
serialPort->disconnect();
|
|
|
90 |
serialPort->close();
|
|
|
91 |
delete serialPort;
|
|
|
92 |
connected = false;
|
|
|
93 |
emit Serial_Disconnected();
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
void SerialHelper::Serial_TransmitString(QString string)
|
|
|
97 |
{
|
|
|
98 |
serialPort->write(string.toStdString().c_str());
|
|
|
99 |
serialPort->write(SERIAL_NEWLINE_CHAR);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
void SerialHelper::Serial_TransmitByteArray(QByteArray buffer)
|
|
|
103 |
{
|
|
|
104 |
serialPort->write(buffer);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
void SerialHelper::Serial_ProcessIncomingData()
|
|
|
108 |
{
|
|
|
109 |
char tmpBuffer[SERIAL_BUFFER_SIZE];
|
|
|
110 |
int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
|
|
|
111 |
while (len > 0) {
|
|
|
112 |
for (int i = 0; i < len; i++) {
|
|
|
113 |
emit Serial_ReceivedByte(tmpBuffer[i]);
|
|
|
114 |
}
|
|
|
115 |
// Check if there is more data to be read from the serial port
|
|
|
116 |
len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
void SerialHelper::Serial_ProcessError(QSerialPort::SerialPortError error)
|
|
|
121 |
{
|
|
|
122 |
if (error == QSerialPort::NoError) return;
|
|
|
123 |
|
|
|
124 |
switch(error) {
|
|
|
125 |
case QSerialPort::DeviceNotFoundError:
|
|
|
126 |
emit UpdateStatus("Serial Error: Nonexistant device");
|
|
|
127 |
return;
|
|
|
128 |
case QSerialPort::PermissionError:
|
|
|
129 |
emit UpdateStatus("Serial Error: Device already opened or lacking permission");
|
|
|
130 |
return;
|
|
|
131 |
case QSerialPort::OpenError:
|
|
|
132 |
emit UpdateStatus("Serial Error: Device already opened");
|
|
|
133 |
return;
|
|
|
134 |
case QSerialPort::NotOpenError:
|
|
|
135 |
emit UpdateStatus("Serial Error: Device not opened");
|
|
|
136 |
return;
|
|
|
137 |
case QSerialPort::ParityError:
|
|
|
138 |
emit UpdateStatus("Serial Error: Parity error detected");
|
|
|
139 |
break;
|
|
|
140 |
case QSerialPort::FramingError:
|
|
|
141 |
emit UpdateStatus("Serial Error: Framing error detected");
|
|
|
142 |
break;
|
|
|
143 |
case QSerialPort::BreakConditionError:
|
|
|
144 |
emit UpdateStatus("Serial Error: Break condition detected");
|
|
|
145 |
break;
|
|
|
146 |
case QSerialPort::WriteError:
|
|
|
147 |
emit UpdateStatus("Serial Error: Unable to write to device");
|
|
|
148 |
break;
|
|
|
149 |
case QSerialPort::ReadError:
|
|
|
150 |
emit UpdateStatus("Serial Error: Unable to read from device");
|
|
|
151 |
break;
|
|
|
152 |
case QSerialPort::ResourceError:
|
|
|
153 |
emit UpdateStatus("Serial Error: Device missing or disconnected");
|
|
|
154 |
break;
|
|
|
155 |
case QSerialPort::UnsupportedOperationError:
|
|
|
156 |
emit UpdateStatus("Serial Error: Operation not supported or prohibited");
|
|
|
157 |
break;
|
|
|
158 |
case QSerialPort::TimeoutError:
|
|
|
159 |
emit UpdateStatus("Serial Error: Timeout occurred");
|
|
|
160 |
break;
|
|
|
161 |
case QSerialPort::UnknownError:
|
|
|
162 |
emit UpdateStatus("Serial Error: Unknown error");
|
|
|
163 |
break;
|
|
|
164 |
default:
|
|
|
165 |
break;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
serialPort->clearError();
|
|
|
169 |
Serial_Disconnect();
|
|
|
170 |
}
|