Commit 9b5c656f authored by Bill Bonney's avatar Bill Bonney

Fix to improve QSerialPort integration & stability.

parent fb6efcee
...@@ -23,7 +23,6 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, ...@@ -23,7 +23,6 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl,
int dataBits, int stopBits) : int dataBits, int stopBits) :
m_bytesRead(0), m_bytesRead(0),
m_port(NULL), m_port(NULL),
m_ports(new QVector<QString>()),
m_stopp(false), m_stopp(false),
m_reqReset(false) m_reqReset(false)
{ {
...@@ -32,9 +31,9 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, ...@@ -32,9 +31,9 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl,
// Setup settings // Setup settings
m_portName = portname.trimmed(); m_portName = portname.trimmed();
if (m_portName == "" && getCurrentPorts()->size() > 0) if (m_portName == "" && getCurrentPorts().size() > 0)
{ {
m_portName = m_ports->first().trimmed(); m_portName = m_ports.first().trimmed();
} }
qDebug() << "m_portName " << m_portName; qDebug() << "m_portName " << m_portName;
...@@ -87,14 +86,11 @@ SerialLink::~SerialLink() ...@@ -87,14 +86,11 @@ SerialLink::~SerialLink()
disconnect(); disconnect();
if(m_port) delete m_port; if(m_port) delete m_port;
m_port = NULL; m_port = NULL;
if (m_ports) delete m_ports;
m_ports = NULL;
} }
QVector<QString>* SerialLink::getCurrentPorts() QList<QString> SerialLink::getCurrentPorts()
{ {
Q_ASSERT_X(m_ports != NULL, "getCurrentPorts", "m_ports is NULL"); m_ports.clear();
m_ports->clear();
// Example use QSerialPortInfo // Example use QSerialPortInfo
// [TODO] make this thread safe // [TODO] make this thread safe
...@@ -110,7 +106,7 @@ QVector<QString>* SerialLink::getCurrentPorts() ...@@ -110,7 +106,7 @@ QVector<QString>* SerialLink::getCurrentPorts()
// << "Description : " << info.description(); // << "Description : " << info.description();
// qDebug() << "Manufacturer: " << info.manufacturer(); // qDebug() << "Manufacturer: " << info.manufacturer();
m_ports->append(info.portName()); m_ports.append(info.portName());
} }
return m_ports; return m_ports;
} }
...@@ -186,6 +182,18 @@ void SerialLink::run() ...@@ -186,6 +182,18 @@ void SerialLink::run()
m_port->setDataTerminalReady(false); m_port->setDataTerminalReady(false);
} }
} }
if (m_transmitBuffer.size() > 0) {
// send the data
QMutexLocker lockWrite(&m_writeMutex);
int num_written = m_port->write(m_transmitBuffer.constData());
if (num_written > 0){
m_transmitBuffer = m_transmitBuffer.remove(0,num_written);
}
}
bool error = m_port->waitForReadyRead(500); bool error = m_port->waitForReadyRead(500);
if(error) { // Waits for 1/2 second [TODO][BB] lower to SerialLink::poll_interval? if(error) { // Waits for 1/2 second [TODO][BB] lower to SerialLink::poll_interval?
...@@ -203,7 +211,6 @@ void SerialLink::run() ...@@ -203,7 +211,6 @@ void SerialLink::run()
// qDebug() << "readyReadTime #"<< __LINE__; // qDebug() << "readyReadTime #"<< __LINE__;
} }
if (bytes != m_bytesRead) // i.e things are good and data is being read. if (bytes != m_bytesRead) // i.e things are good and data is being read.
{ {
bytes = m_bytesRead; bytes = m_bytesRead;
...@@ -265,26 +272,25 @@ void SerialLink::writeBytes(const char* data, qint64 size) ...@@ -265,26 +272,25 @@ void SerialLink::writeBytes(const char* data, qint64 size)
{ {
if(m_port && m_port->isOpen()) { if(m_port && m_port->isOpen()) {
// qDebug() << "writeBytes" << m_portName << "attempting to tx " << size << "bytes."; // qDebug() << "writeBytes" << m_portName << "attempting to tx " << size << "bytes.";
int b = m_port->write(data, size);
if (b > 0) { QByteArray byteArray(data,size);
Q_ASSERT_X(b = size, "writeBytes", "failed to write all bytes"); {
QMutexLocker writeLocker(&m_writeMutex);
m_transmitBuffer.append(byteArray);
}
// qDebug() << "writeBytes " << m_portName << "tx'd" << b << "bytes:"; // qDebug() << "writeBytes " << m_portName << "tx'd" << b << "bytes:";
// Increase write counter // Increase write counter
m_bitsSentTotal += size * 8; m_bitsSentTotal += size * 8;
// Extra debug logging // Extra debug logging
// QByteArray* byteArray = new QByteArray(data,size);
// qDebug() << byteArray->toHex(); // qDebug() << byteArray->toHex();
// delete byteArray; // delete byteArray;
} else {
} else { disconnect();
disconnect(); // Error occured
// Error occured emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName()));
emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName()));
}
} }
} }
......
...@@ -65,7 +65,7 @@ public: ...@@ -65,7 +65,7 @@ public:
static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in configuration.h static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in configuration.h
/** @brief Get a list of the currently available ports */ /** @brief Get a list of the currently available ports */
QVector<QString>* getCurrentPorts(); QList<QString> getCurrentPorts();
void requestReset(); void requestReset();
...@@ -166,12 +166,14 @@ protected: ...@@ -166,12 +166,14 @@ protected:
quint64 m_connectionStartTime; quint64 m_connectionStartTime;
QMutex m_statisticsMutex; QMutex m_statisticsMutex;
QMutex m_dataMutex; QMutex m_dataMutex;
QVector<QString>* m_ports; QList<QString> m_ports;
private: private:
volatile bool m_stopp; volatile bool m_stopp;
volatile bool m_reqReset; volatile bool m_reqReset;
QMutex m_stoppMutex; QMutex m_stoppMutex;
QMutex m_writeMutex;
QByteArray m_transmitBuffer;
bool hardwareConnect(); bool hardwareConnect();
......
...@@ -42,7 +42,7 @@ class SerialLinkInterface : public LinkInterface ...@@ -42,7 +42,7 @@ class SerialLinkInterface : public LinkInterface
Q_OBJECT Q_OBJECT
public: public:
virtual QVector<QString>* getCurrentPorts() = 0; virtual QList<QString> getCurrentPorts() = 0;
virtual QString getPortName() = 0; virtual QString getPortName() = 0;
virtual int getBaudRate() = 0; virtual int getBaudRate() = 0;
virtual int getDataBits() = 0; virtual int getDataBits() = 0;
......
...@@ -577,12 +577,12 @@ void QGCToolBar::updateComboBox() ...@@ -577,12 +577,12 @@ void QGCToolBar::updateComboBox()
if (slink) if (slink)
{ {
//It's a serial link //It's a serial link
QVector<QString> *portlist = slink->getCurrentPorts(); QList<QString> portlist = slink->getCurrentPorts();
//if (!slink->isConnected()) //if (!slink->isConnected())
//{ //{
for (int j=0;j<portlist->size();j++) for (int j=0;j<portlist.count();j++)
{ {
portComboBox->addItem("Serial port:" + QString::number(i) + ":" + portlist->at(j)); portComboBox->addItem("Serial port:" + QString::number(i) + ":" + portlist[j]);
} }
//} //}
//We only really want to display from unconnected sources. //We only really want to display from unconnected sources.
......
...@@ -216,23 +216,23 @@ void SerialConfigurationWindow::setupPortList() ...@@ -216,23 +216,23 @@ void SerialConfigurationWindow::setupPortList()
if (!link) return; if (!link) return;
// Get the ports available on this system // Get the ports available on this system
QVector<QString>* ports = link->getCurrentPorts(); QList<QString> ports = link->getCurrentPorts();
QString storedName = this->link->getPortName(); QString storedName = this->link->getPortName();
bool storedFound = false; bool storedFound = false;
// Add the ports in reverse order, because we prepend them to the list // Add the ports in reverse order, because we prepend them to the list
for (int i = ports->size() - 1; i >= 0; --i) for (int i = ports.count() - 1; i >= 0; --i)
{ {
// Prepend newly found port to the list // Prepend newly found port to the list
if (ui.portName->findText(ports->at(i)) == -1) if (ui.portName->findText(ports[i]) == -1)
{ {
ui.portName->insertItem(0, ports->at(i)); ui.portName->insertItem(0, ports[i]);
if (!userConfigured) ui.portName->setEditText(ports->at(i)); if (!userConfigured) ui.portName->setEditText(ports[i]);
} }
// Check if the stored link name is still present // Check if the stored link name is still present
if (ports->at(i).contains(storedName) || storedName.contains(ports->at(i))) if (ports[i].contains(storedName) || storedName.contains(ports[i]))
storedFound = true; storedFound = true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment