Skip to content

Commit

Permalink
Serial bugfixes: Clear input/output buffers on connect, don't send mo…
Browse files Browse the repository at this point in the history
…re than one update at once.
  • Loading branch information
cibomahto committed Jul 29, 2014
1 parent cf9cfa4 commit ac2b7c0
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/PatternPaint/blinkytape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ void BlinkyTape::handleSerialError(QSerialPort::SerialPortError error)
return;
}

// Handle a spurious disconnect, like when the user unplugs the BlinkyTape
// TODO: handle other error types?
if (error == QSerialPort::ResourceError) {
qCritical() << serial->errorString();
qCritical() << "Serial resource error, BlinkyTape unplugged?" << serial->errorString();
close();
}
else {
qCritical() << "Unrecognized serial error:" << serial->error() << serial->errorString();
}
}

#if defined(Q_OS_WIN)
Expand Down Expand Up @@ -113,18 +117,12 @@ bool BlinkyTape::open(QSerialPortInfo info) {
}

qDebug() << "Connecting to BlinkyTape on " << info.portName();
//serial->setPort(info);

serial->setPortName(info.portName());
serial->setBaudRate(QSerialPort::Baud19200);
// serial->setDataBits(QSerialPort::Data8);
// serial->setParity(QSerialPort::NoParity);
// serial->setStopBits(QSerialPort::OneStop);
// serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setBaudRate(QSerialPort::Baud115200);

if( !serial->open(QIODevice::ReadWrite) ) {
qDebug() << "error: " << serial->error() << serial->errorString();
qDebug() << "Could not connect to BlinkyTape";
qDebug() << "Could not connect to BlinkyTape. Error: " << serial->error() << serial->errorString();
return false;
}

Expand All @@ -133,6 +131,10 @@ bool BlinkyTape::open(QSerialPortInfo info) {
return false;
}

// TODO: Create a new serial port object, instead of clearing the current one?
serial->clear(QSerialPort::AllDirections);
serial->clearError();

emit(connectionStatusChanged(true));

#if defined(Q_OS_WIN)
Expand Down Expand Up @@ -162,12 +164,20 @@ void BlinkyTape::sendUpdate(QByteArray LedData)
return;
}

// Try to read anything that's available
// Read out any data the strip might have sent, to avoid overflowing the
// read buffer. BlinkyTapes with stock firmware might send a single byte
// back after every update.
if(serial->bytesAvailable() > 0) {
serial->readAll();
serial->clear(QSerialPort::Input);
}

// TODO: Check if we can write to the device?
// If there is data pending to send, skip this update to prevent overflowing
// the buffer.
// TODO: Tested on OS X. Does this work on Windows, Linux?
if(serial->bytesToWrite() >0) {
qDebug() << "Output data still in buffer, dropping this update frame";
return;
}

if(LedData.length() != ledCount*3) {
qCritical() << "Length not correct, not sending update!";
Expand Down

0 comments on commit ac2b7c0

Please sign in to comment.