Hi,
several years ago I wrote a piece of code that reads data from a serial device. Code and application worked fine until today.
When I start up the application, it gets some data but then stops reading.
I have a "reconnect" button that closes and opens the port again. After reconnecting, still there is no data. But now when you move the mouse, data is read. As long as the mouse is moved, data comes in. Stop mouse moving, stops data reading.
Strange.
My system is Ubuntu 14.04,
QT Version is (QT-Creator says this): Based on Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit)
Here is how I read the data, the function is called by a timer every 20ms:
void serial_service::serial_receive(uint8_t channel)
{
if (channel < NUM_OF_CHANNELS)
{
QByteArray data
= service_data
[channel
].
serial.
readAll();
// we throw away data, if data.count() > SERIAL_BUFFER_SIZE
foreach (const char rec_char, data)
{
// use ringbuffer for code compatibility with the microcontroller-application
ringBufferWrite(&service_data[channel].receive_buffer, (uint8_t) rec_char);
}
}
}
void serial_service::serial_receive(uint8_t channel)
{
if (channel < NUM_OF_CHANNELS)
{
QByteArray data = service_data[channel].serial.readAll();
// we throw away data, if data.count() > SERIAL_BUFFER_SIZE
foreach (const char rec_char, data)
{
// use ringbuffer for code compatibility with the microcontroller-application
ringBufferWrite(&service_data[channel].receive_buffer, (uint8_t) rec_char);
}
}
}
To copy to clipboard, switch view to plain text mode
Here are my serial settings:
void open_serial_port
(uint8_t channel,
QString serial_device_name
) {
service_data[channel].receive_buffer.readIndex = 0; // todo: put ringbuffer into a class
service_data[channel].receive_buffer.writeIndex = 0;
if (serial_device_name.isEmpty())
return;
if (channel < NUM_OF_CHANNELS)
{
if (service_data[channel].serial.isOpen())
service_data[channel].serial.close();
service_data[channel].serial.setPortName(serial_device_name);
service_data
[channel
].
serial.
open(QIODevice::ReadWrite);
service_data[channel].serial.setBaudRate(QSerialPort::Baud9600);
service_data[channel].serial.setDataErrorPolicy(QSerialPort::SkipPolicy);
service_data[channel].serial.setFlowControl(QSerialPort::NoFlowControl);
service_data[channel].serial.setDataBits(QSerialPort::Data8);
service_data[channel].serial.setParity(QSerialPort::NoParity);
service_data[channel].serial.setStopBits(QSerialPort::OneStop);
//qDebug() << "serial_service::open_port: " << serial_device_name;
}
}
void open_serial_port(uint8_t channel, QString serial_device_name)
{
service_data[channel].receive_buffer.readIndex = 0; // todo: put ringbuffer into a class
service_data[channel].receive_buffer.writeIndex = 0;
if (serial_device_name.isEmpty())
return;
if (channel < NUM_OF_CHANNELS)
{
if (service_data[channel].serial.isOpen())
service_data[channel].serial.close();
service_data[channel].serial.setPortName(serial_device_name);
service_data[channel].serial.open(QIODevice::ReadWrite);
service_data[channel].serial.setBaudRate(QSerialPort::Baud9600);
service_data[channel].serial.setDataErrorPolicy(QSerialPort::SkipPolicy);
service_data[channel].serial.setFlowControl(QSerialPort::NoFlowControl);
service_data[channel].serial.setDataBits(QSerialPort::Data8);
service_data[channel].serial.setParity(QSerialPort::NoParity);
service_data[channel].serial.setStopBits(QSerialPort::OneStop);
//qDebug() << "serial_service::open_port: " << serial_device_name;
}
}
To copy to clipboard, switch view to plain text mode
The problem is really urgent. It's a database application that reads lap-times from a stopwatch (arduino inside). We use it for Kart-Racing and next week we have a race...
Thank you for any help!
by the way: Code and schematics are open source and can be found at www.typsiland.de
Bookmarks