简体   繁体   English

Qt串行端口有时丢失最后一个字节

[英]Qt serial port lost sometimes the last byte

I am making RPM counter GUI with Qt, and arduino, i am using cooling fun to get the RPM values. 我正在用Qt和arduino制作RPM计数器GUI,我在使用冷却乐趣来获得RPM值。 I am using the serial port to read the values from arduino, but sometimes i miss the last byte from the value that i print in Qt GUI, but i dont have the same problem in the arduino serial monitor. 我正在使用串行端口从arduino读取值,但有时我会错过在Qt GUI中打印的值的最后一个字节,但在arduino串行监视器中没有相同的问题。 Here is the code from Qt : 这是Qt的代码:

void ArduinoRpm::serialReciver()
{

    QString input_converter;
    std::string to_file_string;

    int sizeFromInput = 0;
    char *dataBuffer;

    //get the port size depending on bytes available to read
    int bufferSize = serial->bytesAvailable();
    //int bufferSize = serial->bytesAvailable();

    //dataBuffer, get the data from serial port, bufferSize + 1 for the newline
    dataBuffer = new char[bufferSize + 2];

    //flush the port before read


    //This function reads a line of ASCII characters from the device, up to a maximum of
    //maxSize - 1 bytes, stores the characters in dataBuffer, and returns the number of bytes
    //read. if a line could not be read but no error ocurred, this function returns 0. if an
    //error occurs, this function returns the length of what could read or -1 if nothing was read.
    //bufferSize is the maxsize readline can read.

    sizeFromInput = serial->readLine(dataBuffer, bufferSize);

    //to_file_string, to write data in file
    to_file_string = dataBuffer;
    input_converter = QString::fromStdString(to_file_string);


    if ((sizeFromInput >= 1) && (input_converter.toInt() <= 9999)) {
        if(input_converter.toInt() > 10) {
            ui->lcdNumber->display(input_converter);
            rpmNeedle->setCurrentValue(input_converter.toInt());
            input_file << to_file_string << std::endl;
        }

    }

    delete dataBuffer;
    bufferSize = 0;
    serial->flush();

}

 void ArduinoRpm::SerialInitializer()
{
    serial = new QSerialPort(this);
    serial->setPortName(serialPortValue); //COM-port your Arduino is connected to
    serial->open(QIODevice::ReadWrite);
    serial->setBaudRate(QSerialPort::Baud9600); //must be the same as your arduino-baudrate
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
}

for example 例如

27
1621
1621
1627
1627
1627
1627
1627
1627
1627
1627
1627
1627
1627
1627
1627
1621
1616
1616
1605
1605
1599
1594
1588
158
1583
158
1578
1578
1572
1572
1572

it should read 4 digits all the time, but sometimes it losts the last digit.. Any solution for this ? 它应该一直读取4位数字,但有时会丢失最后一位数字。对此有什么解决方案?

your function should be something like this: 您的函数应如下所示:

void ArduinoRpm::serialReciver()
{
    char dataBuffer[6];

    if(!serial->canReadLine()) //if there isn't a whole line available
        return; //return from this call (wait for the next readyRead() signal)
    serial->readLine(dataBuffer, 6)

    /* do whatever you want with dataBuffer here */

    if(serial->bytesAvailable() > 0) //if there are still unread bytes
        QMetaObject::invokeMethod(this, "serialReciver", Qt::QueuedConnection); //call function again as if readyRead() signal was emitted
}

PS this answer assumes that the input is guaranteed to be always like this: 4 chars of digits then a newline then 4 chars of digits then a newline again, etc. . PS此答案假定输入始终保证是这样的:4位数字然后是换行符,然后是4位数字然后又是换行符,依此类推。 . So if it did not follow this pattern, then this will cause undefined behavior. 因此,如果它不遵循此模式,则将导致未定义的行为。

PS I did not fix your typo in serialReciver . PS我没有在serialReciver解决您的错字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM