-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialUSBcomms.cpp
More file actions
149 lines (132 loc) · 4.12 KB
/
SerialUSBcomms.cpp
File metadata and controls
149 lines (132 loc) · 4.12 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
#include "SerialUSBcomms.h"
#include <QDebug>
SerialUSBcomms serialUSBcomms;
SerialUSBcomms::SerialUSBcomms(QObject *parent) :
QObject(parent)
{
qDebug() << "SerialUSBComms::SerialUSBComms() - Constructing.......";
#ifdef INCLUDE_SERIAL_USB
serialUSB = new QSerialPort(this);
#endif
setSerialCommsConnected(false);
setIsUSBinitialised(false);
}
void SerialUSBcomms::findSerialPort()
{
#ifdef INCLUDE_SERIAL_USB
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
//qDebug() << "SerialUSBcomms::findSerialPort() - Found a port:" << info.portName();
if (info.manufacturer().contains("Spark") || info.manufacturer().contains("Arduino")
|| info.portName().contains("usbmodem") || info.portName().contains("COM")
|| info.description().contains("freetronics") )
{
serialUSB->setPortName(info.portName() );
emit SerialPortFound(info.portName() );
QString Rhubarb = "SerialUSBcomms::findSerialPort() - Using serial port " + info.portName();
qDebug() << "SerialUSBcomms::findSerialPort() - Using:" << info.portName();
emit AddToHostWindowListWidget(Rhubarb);
}
}
#endif
}
void SerialUSBcomms::setUpSerialPort()
{
#ifdef INCLUDE_SERIAL_USB
findSerialPort();
#ifdef Q_OS_ANDROID
return;
#endif
serialUSB->open(QIODevice::ReadWrite);
serialUSB->setBaudRate(QSerialPort::Baud115200);
serialUSB->setDataBits(QSerialPort::Data8);
serialUSB->setParity(QSerialPort::NoParity);
serialUSB->setStopBits(QSerialPort::OneStop);
serialUSB->setFlowControl(QSerialPort::NoFlowControl);
if (serialUSB->isReadable()) qDebug() << "SerialUSBcomms::setUpSerialPort() -" << serialUSB->portName() << "is READABLE :-)";
if (serialUSB->isOpen() && serialUSB->isWritable())
{
qDebug() << "SerialUSBcomms::setUpSerialPort()" << serialUSB->portName() << " is Ready..." << Qt::endl;
setSerialCommsConnected(true);
}
else
{
//qDebug() << "SerialUSBcomms::setUpSerialPort()" << serialUSB->portName() << " failed to open..." << endl;
setSerialCommsConnected(false);
}
#endif
}
void SerialUSBcomms::closeSerialPort()
{
#ifdef INCLUDE_SERIAL_USB
if(serialCommsConnected)
{
serialUSB->close();
setSerialCommsConnected(false);
qDebug() << "SerialUSBcomms::closeSerialPort() has left the building";
}
#endif
}
void SerialUSBcomms::receivePacket()
{
#ifdef INCLUDE_SERIAL_USB
QByteArray rX;
rX = serialUSB->readAll();
#ifdef Q_OS_ANDROID
//lttoComms.androidRxPacket(rX);
//TODO: Is this fixed?
emit newSerialUSBdata(rX);
#else
emit newSerialUSBdata(rX);
#endif
#endif
}
//void SerialUSBcomms::readUSBdata()
//{
// QByteArray rX;
// rX = serialUSB->readAll();
// qDebug() << "SerialUSBcomms::readUSBdata() - Does this ever get called???????" << rX;
//}
void SerialUSBcomms::sendPacket(QByteArray packet)
{
#ifdef INCLUDE_SERIAL_USB
if (serialUSB->isOpen() && serialUSB->isWritable())
{
//qDebug() << "\tSending serial to USB";
serialUSB->write(packet);
serialUSB->flush();
}
else
{
serialUSBcomms.setUpSerialPort();
}
#else
packet.clear(); // to silence Compiler warning!
#endif
}
bool SerialUSBcomms::getIsUSBinitialised() const
{
return isUSBinitialised;
}
void SerialUSBcomms::setIsUSBinitialised(bool value)
{
isUSBinitialised = value;
}
bool SerialUSBcomms::getSerialCommsConnected() const
{
return serialCommsConnected;
}
void SerialUSBcomms::setSerialCommsConnected(bool value)
{
serialCommsConnected = value;
//TODO1: Hmmmmmmmm lttoComms.setSerialUSBcommsConnected(value);
}
void SerialUSBcomms::initialiseUSBsignalsAndSlots()
{
//Needs to be done here not in constructor because lttoComms is a static and may not exist when this static is created.
#ifdef INCLUDE_SERIAL_USB
qDebug() << "SerialUSBcomms::initialiseUSBsignalsAndSlots() - Triggered";
connect(serialUSB, SIGNAL(readyRead()), this, SLOT(receivePacket()) );
setIsUSBinitialised(true);
#endif
}