Important: Please read the Qt Code of Conduct -

QSerialPort problem (between windows and linux)

  • Hi. I,m new in Qt . i try to write a serial port application to communicate from serial port between windows and linux system.

    void SerialPort::openSerialPort()
       if(portToUse.isNull() || !portToUse.isValid())
           qDebug() << "port is not valid:" << portToUse.portName();
       // Enumerate the serial port
       // Open it if it isn't busy
       qDebug()<<"Port Name " << portToUse.portName();
       if (serial->open(QIODevice::ReadWrite)) {
           qDebug() << "Connected to" << portToUse.description() << "on" << portToUse.portName();
       } else {
           qCritical() << "Serial Port error:" << serial->errorString();
           qDebug() << tr("Open error");
    void SerialPort::closeSerialPort()
       qDebug() << tr("Disconnected");
    void SerialPort::writeData(const QByteArray &data)

    and this is abstaction of my code.
    when data write from a linux system and recive from another linux system every thing is ok , but when recive with windows data is not correct.

    any body can tell me what is the problem?

  • @iRenji said in QSerialPort problem (between windows and linux):

    any body can tell me what is the problem?

    Probably endianness.

    Do not use the raw serial->write ( and serial->read) use QDataStream.

    void SerialPort::writeData(const QByteArray &data)
    QDataStream stream(serial);
       stream << data;

  • it,s not worked data not correct. when i use datastream to send simple character "h" i get long hex data in windows client

  • @iRenji Yep, that's correct and it's what it's supposed to be. could you show me an example of how you read and how you write?

  • Qt Champions 2020

    Maybe reason is in:


    Currently, the QSP does not support QIODevice::OpenMode::Text flag, which adds when you call this method. QSP works only in binary mode.

  • the only effect that flag has is treatment of newlines, if you do not send newline it should be fine

  • @kuzulis no i remove that line and it,s not worked

  • Qt Champions 2020

    @iRenji , then, most likelly, a problem is in your code, your device and so on.

  • @kuzulis when i use windows to windows connection it,s ok
    so as linux to linux . but with linux to windows there is problem.

  • @VRonin said in QSerialPort problem (between windows and linux):

    Probably endianness.

    Windows is little-endian, linux is (often) big-endian.

    QDataStream is safe to use as it always use big-endian (unless you force it otherwise).

    So bottom line, your possible solutions:

    • Use QDataStream both for reading and writing
    • force an endianness convention manually.
      • you can detect what your platform is using with (C code from source, can be converted to constexpr in C++11)
    int is_big_endian()
        union {
            uint32_t i;
            char c[4];
        } bint = {0x01020304};
        return bint.c[0] == 1; 

  • I use the serial ports from GNU/Linux, OSX, and Windows to talk to each other and the other devices. You shouldn't have issues specific to GNU/Linux.

    Are you actually writing binary versions of numbers through serial? If not, then endianness shouldn't be an issue. If you write "1.2345\r\n" that is what you will get at the other end (6 bytes + <cr> + <lf>). It will be treated as text.

    Maybe there is a problem with your USB/Serial device. Maybe try a baud rate of something slow like 1200 baud. Some of them are not very reliable.

    Maybe when sending text you are sending the unicode version unintentionally. Something like ' 0x00 0x65 0x00 0x66 0x00 0x67' instead of '0x65 0x66 0x67'. Both are for the text 'ABC' but the first one is unicode or two bytes for each character. I don't know what it would look like at the receiving end but very likely it will just show blocks for every other character (?).

  • Lifetime Qt Champion

    I would test with a linux serial program and a window ditto to check that
    it will send as expected. If you are using USB serial on windows or any such difference
    its good to verify that it work as expected.

  • @mrjj usb serial port in windows work correctly ,
    When i send a simple character "M" i get hex "fc". i check the binary and i did,nt think it,s endian problem. setting parameter is the same at windows compile and linux compile side.

  • Lifetime Qt Champion

    ok super.

    Now try
    in both ends.

    Then we know if its Qt related or simply something in your app.

  • void SerialPort::writeData(const QByteArray &data)


    i change the data to char* and it,s worked good.

Log in to reply