Commit f60e7231 authored by Bill Bonney's avatar Bill Bonney

FIX: Added the use of waitForBytesWritten to enable sending data

parent 51fa42c9
...@@ -63,16 +63,6 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, ...@@ -63,16 +63,6 @@ SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl,
m_dataBits = dataBits; m_dataBits = dataBits;
m_stopBits = stopBits; m_stopBits = stopBits;
// Set the port name
// if (m_portName == "")
// {
// m_name = tr("Serial Link ") + QString::number(getId());
// }
// else
// {
// m_name = portname.trimmed();
// }
loadSettings(); loadSettings();
} }
void SerialLink::requestReset() void SerialLink::requestReset()
...@@ -91,8 +81,6 @@ SerialLink::~SerialLink() ...@@ -91,8 +81,6 @@ SerialLink::~SerialLink()
QList<QString> SerialLink::getCurrentPorts() QList<QString> SerialLink::getCurrentPorts()
{ {
m_ports.clear(); m_ports.clear();
// Example use QSerialPortInfo
// [TODO] make this thread safe
QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts(); QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
...@@ -152,6 +140,7 @@ void SerialLink::run() ...@@ -152,6 +140,7 @@ void SerialLink::run()
{ {
//Need to error out here. //Need to error out here.
emit communicationError(getName(),"Error connecting: " + m_port->errorString()); emit communicationError(getName(),"Error connecting: " + m_port->errorString());
disconnect(); // This tidies up and sends the necessary signals
return; return;
} }
...@@ -183,18 +172,18 @@ void SerialLink::run() ...@@ -183,18 +172,18 @@ void SerialLink::run()
} }
} }
if (m_transmitBuffer.size() > 0) { if (m_transmitBuffer.length() > 0) {
// send the data QMutexLocker writeLocker(&m_writeMutex);
QMutexLocker lockWrite(&m_writeMutex); int numWritten = m_port->write(m_transmitBuffer);
int num_written = m_port->write(m_transmitBuffer.constData()); bool txError = m_port->waitForBytesWritten(-1);
if (num_written > 0){ // if ((txError) || (numWritten == -1))
m_transmitBuffer = m_transmitBuffer.remove(0,num_written); // qDebug() << "TX Error!";
} m_transmitBuffer = m_transmitBuffer.remove(0, numWritten);
} else {
// qDebug() << "Wait write response timeout %1" << QTime::currentTime().toString();
} }
bool error = m_port->waitForReadyRead(10);
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?
QByteArray readData = m_port->readAll(); QByteArray readData = m_port->readAll();
...@@ -208,9 +197,9 @@ void SerialLink::run() ...@@ -208,9 +197,9 @@ void SerialLink::run()
m_bitsReceivedTotal += readData.length() * 8; m_bitsReceivedTotal += readData.length() * 8;
} }
} else { } else {
// qDebug() << "readyReadTime #"<< __LINE__; // qDebug() << "Wait write response timeout %1" << QTime::currentTime().toString();
} }
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;
...@@ -273,20 +262,17 @@ void SerialLink::writeBytes(const char* data, qint64 size) ...@@ -273,20 +262,17 @@ 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.";
QByteArray byteArray(data,size); QByteArray byteArray(data, size);
{ {
QMutexLocker writeLocker(&m_writeMutex); QMutexLocker writeLocker(&m_writeMutex);
m_transmitBuffer.append(byteArray); m_transmitBuffer.append(byteArray);
} }
// 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
// qDebug() << byteArray->toHex(); // qDebug() << byteArray->toHex();
// delete byteArray;
} else { } else {
disconnect(); disconnect();
// Error occured // Error occured
...@@ -420,6 +406,8 @@ bool SerialLink::hardwareConnect() ...@@ -420,6 +406,8 @@ bool SerialLink::hardwareConnect()
} }
QObject::connect(m_port,SIGNAL(aboutToClose()),this,SIGNAL(disconnected())); QObject::connect(m_port,SIGNAL(aboutToClose()),this,SIGNAL(disconnected()));
QObject::connect(m_port, SIGNAL(error(QSerialPort::SerialPortError)),
this, SLOT(linkError(QSerialPort::SerialPortError)));
// port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead); // port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
m_connectionStartTime = MG::TIME::getGroundTimeNow(); m_connectionStartTime = MG::TIME::getGroundTimeNow();
...@@ -451,6 +439,11 @@ bool SerialLink::hardwareConnect() ...@@ -451,6 +439,11 @@ bool SerialLink::hardwareConnect()
return true; // successful connection return true; // successful connection
} }
void SerialLink::linkError(QSerialPort::SerialPortError error)
{
qDebug() << error;
}
/** /**
* @brief Check if connection is active. * @brief Check if connection is active.
......
...@@ -142,6 +142,8 @@ public slots: ...@@ -142,6 +142,8 @@ public slots:
bool connect(); bool connect();
bool disconnect(); bool disconnect();
void linkError(QSerialPort::SerialPortError error);
protected: protected:
quint64 m_bytesRead; quint64 m_bytesRead;
QSerialPort* m_port; QSerialPort* m_port;
...@@ -166,13 +168,13 @@ protected: ...@@ -166,13 +168,13 @@ protected:
quint64 m_connectionStartTime; quint64 m_connectionStartTime;
QMutex m_statisticsMutex; QMutex m_statisticsMutex;
QMutex m_dataMutex; QMutex m_dataMutex;
QMutex m_writeMutex;
QList<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; QByteArray m_transmitBuffer;
bool hardwareConnect(); bool hardwareConnect();
......
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