qt串口接收器丢失数据

qt serial receiver missing data

我正在尝试根据 http://doc.qt.io/qt-5/qtserialport-creadersync-main-cpp.html 示例:

QCoreApplication coreApplication(argc, argv);

QTextStream standardOutput(stdout);

QSerialPort serialPort;
QByteArray readData;

serialPort.setPortName("ttyS4");
serialPort.setBaudRate(QSerialPort::Baud9600);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setParity(QSerialPort::EvenParity);
serialPort.setStopBits(QSerialPort::OneStop);
serialPort.setFlowControl(QSerialPort::NoFlowControl);

if (!serialPort.open(QIODevice::ReadOnly)) {
    standardOutput << QObject::tr("Failed to open port") << endl;
    return 1;
}

while (serialPort.waitForReadyRead(5000))
    readData.append(serialPort.readAll());
qDebug() << readData;

return coreApplication.exec();

我还尝试根据 http://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html 示例读取数据:

主要:

int main(int argc, char *argv[])
{
    QCoreApplication coreApplication(argc, argv);

    QTextStream standardOutput(stdout);
    QSerialPort serialPort;


    serialPort.setPortName("ttyS4");
    serialPort.setBaudRate(QSerialPort::Baud9600);
    serialPort.setDataBits(QSerialPort::Data8);
    serialPort.setParity(QSerialPort::EvenParity);
    serialPort.setStopBits(QSerialPort::OneStop);
    serialPort.setFlowControl(QSerialPort::NoFlowControl);


    if (!serialPort.open(QIODevice::ReadOnly)) {
        standardOutput << QObject::tr("Failed to open port") << endl;
        return 1;
    }
    SerialPortReader serialPortReader(&serialPort);

    return coreApplication.exec();
}

serialPortReader:

SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent):QObject(parent), m_serialPort(serialPort), m_standardOutput(stdout)
{

    connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));
    m_counter = 0;
    m_timer.start(5000);
}

SerialPortReader::~SerialPortReader()
{
}

void SerialPortReader::handleReadyRead()
{   m_counter++;
    m_readData.append(m_serialPort->readAll());
    qDebug()<< m_serialPort->readAll();
    qDebug() << "triggered" << m_counter;
}

void SerialPortReader::handleTimeout()
{
    if (m_readData.isEmpty()) {
        m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
    } else {
        m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
        m_standardOutput << m_readData << endl;
    }

    QCoreApplication::quit();
}

void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
    if (serialPortError == QSerialPort::ReadError) {
        m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        QCoreApplication::exit(1);
    }
}

但是当我向这个 COM 端口发送数据时(发送方的 serialPort 设置相同),并不是所有的数据都收到了。

通过MSB-RS232,我可以检查哪些数据确实已经发送到端口,我的发送方没有任何问题。

为了测试,我发送

主要内容:

QString alpha = "abcdefghijklmnopqrstuvwxyz123456789";
handler.writetoPort(alpha);

handler.cpp:

void SerialHandler::writetoPort(QString x)
{
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QByteArray encodedVar = codec->fromUnicode(x);
    writetoPort(encodedVar);
}

void SerialHandler::writetoPort(const QByteArray x)
{
    serial.write(x);
    serial.waitForBytesWritten(-1);
}

结果是输出: abyz123456789 或 abcdklmnopqrstuvwxyz123456789 或 abcdefghijklm 或 ...

总是不同的。

有人知道这里发生了什么吗?

感谢您阅读我的长文post。

--添加 17.07--

对于我的问题,这可能是强制性的: 该代码必须 运行 在微处理器上。

异步和同步示例在我的 Windows PC 上都运行良好。

也许这会有所帮助

void SerialPortReader::handleReadyRead()
{   
    m_counter++;
    while (m_serialPort->bytesAvailable())
    {
        m_readData.append(m_serialPort->readAll());
    }
    qDebug()<< m_readData;
    qDebug() << "triggered" << m_counter;
}

我在不同的 Board 上尝试了完全相同的 Projekt,一切运行顺利。 我认为旧板的 COM 端口可能已损坏。

会将此标记为已解决,但我无法确定真正的问题是什么。