diff --git a/src/comm/SerialLink.cc b/src/comm/SerialLink.cc index 0f50e912f5254292910756866650d31e3e34e8b8..c91ef70d5d7cccee93be1e81c66e8e9ad8d5fa24 100644 --- a/src/comm/SerialLink.cc +++ b/src/comm/SerialLink.cc @@ -23,7 +23,6 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, int dataBits, int stopBits) : m_bytesRead(0), m_port(NULL), - m_ports(new QVector()), m_stopp(false), m_reqReset(false) { @@ -32,9 +31,9 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, // Setup settings 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; @@ -87,14 +86,11 @@ SerialLink::~SerialLink() disconnect(); if(m_port) delete m_port; m_port = NULL; - if (m_ports) delete m_ports; - m_ports = NULL; } -QVector* SerialLink::getCurrentPorts() +QList SerialLink::getCurrentPorts() { - Q_ASSERT_X(m_ports != NULL, "getCurrentPorts", "m_ports is NULL"); - m_ports->clear(); + m_ports.clear(); // Example use QSerialPortInfo // [TODO] make this thread safe @@ -110,7 +106,7 @@ QVector* SerialLink::getCurrentPorts() // << "Description : " << info.description(); // qDebug() << "Manufacturer: " << info.manufacturer(); - m_ports->append(info.portName()); + m_ports.append(info.portName()); } return m_ports; } @@ -186,6 +182,18 @@ void SerialLink::run() 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); if(error) { // Waits for 1/2 second [TODO][BB] lower to SerialLink::poll_interval? @@ -203,7 +211,6 @@ void SerialLink::run() // qDebug() << "readyReadTime #"<< __LINE__; } - if (bytes != m_bytesRead) // i.e things are good and data is being read. { bytes = m_bytesRead; @@ -265,26 +272,25 @@ void SerialLink::writeBytes(const char* data, qint64 size) { if(m_port && m_port->isOpen()) { // qDebug() << "writeBytes" << m_portName << "attempting to tx " << size << "bytes."; - int b = m_port->write(data, size); - if (b > 0) { - Q_ASSERT_X(b = size, "writeBytes", "failed to write all bytes"); + QByteArray byteArray(data,size); + { + 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 - m_bitsSentTotal += size * 8; + // Increase write counter + m_bitsSentTotal += size * 8; - // Extra debug logging -// QByteArray* byteArray = new QByteArray(data,size); + // Extra debug logging // qDebug() << byteArray->toHex(); // delete byteArray; - - } else { - disconnect(); - // Error occured - emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName())); - } + } else { + disconnect(); + // Error occured + emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName())); } } diff --git a/src/comm/SerialLink.h b/src/comm/SerialLink.h index cab1e91d7a485b00e298c996f7c77eda8f527480..963a14a258e24f80b03e56add9f2c343cf5c08b1 100644 --- a/src/comm/SerialLink.h +++ b/src/comm/SerialLink.h @@ -65,7 +65,7 @@ public: static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in configuration.h /** @brief Get a list of the currently available ports */ - QVector* getCurrentPorts(); + QList getCurrentPorts(); void requestReset(); @@ -166,12 +166,14 @@ protected: quint64 m_connectionStartTime; QMutex m_statisticsMutex; QMutex m_dataMutex; - QVector* m_ports; + QList m_ports; private: volatile bool m_stopp; volatile bool m_reqReset; QMutex m_stoppMutex; + QMutex m_writeMutex; + QByteArray m_transmitBuffer; bool hardwareConnect(); diff --git a/src/comm/SerialLinkInterface.h b/src/comm/SerialLinkInterface.h index 59ff90132e19d0a8165d844641e2d834d904023a..767d3011c95179983fc475bd65e23ce9e0b5266f 100644 --- a/src/comm/SerialLinkInterface.h +++ b/src/comm/SerialLinkInterface.h @@ -42,7 +42,7 @@ class SerialLinkInterface : public LinkInterface Q_OBJECT public: - virtual QVector* getCurrentPorts() = 0; + virtual QList getCurrentPorts() = 0; virtual QString getPortName() = 0; virtual int getBaudRate() = 0; virtual int getDataBits() = 0; diff --git a/src/ui/QGCToolBar.cc b/src/ui/QGCToolBar.cc index 30d89f5d817a3eb80438a3e0ac3115081affd802..40a80d616b45fffa561f0d7b164a23360fd0e596 100644 --- a/src/ui/QGCToolBar.cc +++ b/src/ui/QGCToolBar.cc @@ -577,12 +577,12 @@ void QGCToolBar::updateComboBox() if (slink) { //It's a serial link - QVector *portlist = slink->getCurrentPorts(); + QList portlist = slink->getCurrentPorts(); //if (!slink->isConnected()) //{ - for (int j=0;jsize();j++) + for (int j=0;jaddItem("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. diff --git a/src/ui/SerialConfigurationWindow.cc b/src/ui/SerialConfigurationWindow.cc index 3354deb6cdbb8cd5b217a31cb08dab31494f2eeb..72974c7c14fa039da95cbc857f3a8d7d8367d908 100644 --- a/src/ui/SerialConfigurationWindow.cc +++ b/src/ui/SerialConfigurationWindow.cc @@ -216,23 +216,23 @@ void SerialConfigurationWindow::setupPortList() if (!link) return; // Get the ports available on this system - QVector* ports = link->getCurrentPorts(); + QList ports = link->getCurrentPorts(); QString storedName = this->link->getPortName(); bool storedFound = false; // 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 - if (ui.portName->findText(ports->at(i)) == -1) + if (ui.portName->findText(ports[i]) == -1) { - ui.portName->insertItem(0, ports->at(i)); - if (!userConfigured) ui.portName->setEditText(ports->at(i)); + ui.portName->insertItem(0, ports[i]); + if (!userConfigured) ui.portName->setEditText(ports[i]); } // 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; }