Skip to content

Commit

Permalink
plugins/dmxusb: use support read buffer in QtSerial backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallegari committed Dec 1, 2024
1 parent dea24bb commit cd9ffcc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
41 changes: 28 additions & 13 deletions plugins/dmxusb/src/qtserial-interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ QList<DMXInterface *> QtSerialInterface::interfaces(QList<DMXInterface *> discov
QString name(info.description());
QString vendor(info.manufacturer());

qDebug() << "[QtSerialInterface] Serial: " << serial << "name:" << name << "vendor:" << vendor;
if (!serial.isEmpty() && !name.isEmpty() && !vendor.isEmpty())
qDebug() << "[QtSerialInterface] Serial: " << serial << "name:" << name << "vendor:" << vendor;

// Skip non wanted devices
if (validInterface(info.vendorIdentifier(), info.productIdentifier()) == false)
Expand Down Expand Up @@ -199,8 +200,8 @@ bool QtSerialInterface::open()
return false;
}

m_handle->setReadBufferSize(1024);
qDebug() << "Read buffer size:" << m_handle->readBufferSize() << m_handle->errorString();
//m_handle->setReadBufferSize(1024);
//qDebug() << "Read buffer size:" << m_handle->readBufferSize() << m_handle->errorString();

return true;
}
Expand Down Expand Up @@ -392,7 +393,7 @@ bool QtSerialInterface::write(const QByteArray& data)
}
}

QByteArray QtSerialInterface::read(int size, uchar* userBuffer)
QByteArray QtSerialInterface::read(int size, uchar *userBuffer)
{
//qDebug() << Q_FUNC_INFO;

Expand All @@ -401,14 +402,21 @@ QByteArray QtSerialInterface::read(int size, uchar* userBuffer)
if (m_handle == NULL)
return QByteArray();

if (m_handle->waitForReadyRead(10) == true)
if (m_readBuffer.length() < size)
{
return m_handle->read(size);
if (m_handle->waitForReadyRead(10) == true)
{
m_readBuffer.append(m_handle->read(1024));
//qDebug() << "[QtSerial] read buffer payload:" << m_readBuffer.toHex(',');
}
}
return QByteArray();
QByteArray ret = m_readBuffer.mid(0, qMin(size, m_readBuffer.length()));
m_readBuffer.remove(0, qMin(size, m_readBuffer.length()));

return ret;
}

uchar QtSerialInterface::readByte(bool* ok)
uchar QtSerialInterface::readByte(bool *ok)
{
if (ok) *ok = false;

Expand All @@ -417,15 +425,22 @@ uchar QtSerialInterface::readByte(bool* ok)

//qDebug() << Q_FUNC_INFO;

if (m_handle->waitForReadyRead(10) == true)
if (m_readBuffer.length() < 1)
{
QByteArray array = m_handle->read(1);
if (array.size() > 0)
if (m_handle->waitForReadyRead(10) == true)
{
if (ok) *ok = true;
return (uchar)array.at(0);
m_readBuffer.append(m_handle->read(1024));
//qDebug() << "[QtSerial] read buffer payload:" << m_readBuffer.toHex(',');
}
}

if (m_readBuffer.size() > 0)
{
if (ok) *ok = true;
uchar retByte = uchar(m_readBuffer.at(0));
m_readBuffer.remove(0, 1);
return retByte;
}

return 0;
}
1 change: 1 addition & 0 deletions plugins/dmxusb/src/qtserial-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class QtSerialInterface : public DMXInterface
private:
QSerialPort *m_handle;
QSerialPortInfo m_info;
QByteArray m_readBuffer;
};

#endif
Expand Down

0 comments on commit cd9ffcc

Please sign in to comment.