Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem transmitting '0x20' via serial port



  • Hi. I am developing an application to update the firmware of an embedded device. It has a custom bootloader which requires an encrypted file to be sent via serial. The process works with TeraTerm when i submit a binary file at 9600 baud rate and updates the firmware on the device.

    Problem:

    When I try sending the same file via code through a GUI qt interface, the resulting file isn't the same. I have used a logic analyzer to determine that it is skipping all the 0x20 values, which equal to a space in ASCII. The resulting file that is transmitted is EXACTLY the same number of bytes less as the number of 0x20 values in the original file.

    if(!firmwareFile.open(QIODevice::ReadOnly)){
            qDebug() << "Cannot open file";
        }
    
     QByteArray firmwareData;
     firmwareData = firmwareFile.readAll();
     qDebug()<< portName.write(firmwareData);
     portName.waitForBytesWritten(-1);
    qDebug() <<portName.bytesToWrite();
     portName.close();
    
    

    Serial port parameters:

    setBaudRate(QSerialPort::Baud9600);
    setStopBits(QSerialPort::OneStop);
    setParity(QSerialPort::NoParity);
    setFlowControl(QSerialPort::NoFlowControl);
    setDataBits(QSerialPort::Data8);

    The received resulting file has exactly 104 bytes less. And there are 104 times 0x20 values in the original binary file. I have tried to use QByteArray::toHex as well but that doesn't work too.

    alt text



  • @armorex

    QSerialPort mSerial;
    QByteArray data;

    ...
    mSerial.write( data.data(), data.size() );

    Hi.
    I don't know!
    I've never problem to send data with this code, try it.



  • @armorex said in Problem transmitting '0x20' via serial port:

    I have tried to use QByteArray::toHex as well but that doesn't work too.

    What exactly do you mean by this? It sounds like you are suggesting QByteArray::toHex() doesn't cope with space character in the bytes??


  • Qt Champions 2020

    You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.



  • @CP71 Ok i will try it and let you know



  • @kuzulis said in Problem transmitting '0x20' via serial port:

    You can use any of serial port sniffer to see a real output data flow to the serial port driver. Seems, you do something wrong, or, maybe, there are some noise on a line. You should not use serial communication without of CRC calculation.

    I have already included a screenshot of a file that was made by the logic analyzer. Which gets gets the output from a physical serial port. You can see that the top file does not contain 0x20. The bottom (original) file, however does. I have tired it multiple times with the same output which eliminates the possibility of noise


  • Lifetime Qt Champion

    Hi @armorex,

    99% surely the problem is in your code.

    But please tell us, which operating system, compiler and Qt version you use.

    I'd try to separate the problem in two parts:

    1. Make sure the file is read correctly. Check it before writing it to the serial port. BTW: How big is this file?
    2. Make sure the serial writing is done correctly. Start by writing a small buffer containing spaces and verify they are sent.

    If I were you, I'd not use waitForBytesWritten. It is unrelyable anyway, as the data is still buffered in the OS or driver level when Qt's buffer get empty. A better handshake is to send ACKs from the bootloader when when (a chunk of/all) data arrived.

    Regards



  • @CP71 said in Problem transmitting '0x20' via serial port:

    @armorex

    QSerialPort mSerial;
    QByteArray data;

    ...
    mSerial.write( data.data(), data.size() );

    Hi.
    I don't know!
    I've never problem to send data with this code, try it.

    This worked! But now i don't understand why can't we use QByteArray directly and have to use data(). But at least it works now. Thanks!



  • @aha_1980 said in Problem transmitting '0x20' via serial port:

    Hi @armorex,

    99% surely the problem is in your code.

    But please tell us, which operating system, compiler and Qt version you use.

    I'd try to separate the problem in two parts:

    1. Make sure the file is read correctly. Check it before writing it to the serial port. BTW: How big is this file?
    2. Make sure the serial writing is done correctly. Start by writing a small buffer containing spaces and verify they are sent.

    If I were you, I'd not use waitForBytesWritten. It is unrelyable anyway, as the data is still buffered in the OS or driver level when Qt's buffer get empty. A better handshake is to send ACKs from the bootloader when when (a chunk of/all) data arrived.

    Regards

    I had to use the blocking function because it wasn't transmitting all the data. It would stop in the middle. The file is 36349 bytes.



  • @armorex

    You are welcome!
    It is old code, so I don’t remember why I have used this way instead of .write( QbyteData ), maybe I have had same result! But I can’t say now because I don’t remember ;)

    I don’t know how .write() functions work at low level, but QByteData is unicode chars container. This call receives a no unicode chars. Maybe the difference is this.


  • Lifetime Qt Champion

    @CP71

    I don’t know how .write() functions work at low level, but QByteData is unicode chars container.

    No, that's wrong. QByteArray just holds Bytes without interpreting them.

    So I really wonder why using the other write makes a difference.

    Still the OP didn't answer my questions.

    Regards



  • @aha_1980
    Yes, sorry!
    You are right, my mistake.
    I don’t know why I was thinking at QChar :(


Log in to reply