Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort problem (between windows and linux)
QtWS: Super Early Bird Tickets Available!

QSerialPort problem (between windows and linux)

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 6.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iRenji
    wrote on 15 Jun 2017, 09:50 last edited by VRonin
    #1

    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();
           return;
       }
    
    
       // Enumerate the serial port
       // Open it if it isn't busy
       qDebug()<<"Port Name " << portToUse.portName();
       serial->setPortName(portToUse.portName());
       serial->setTextModeEnabled(true);
       serial->setBaudRate(QSerialPort::Baud19200);
       serial->setDataBits(QSerialPort::Data8);
       serial->setParity(QSerialPort::NoParity);
       serial->setStopBits(QSerialPort::OneStop);
       serial->setFlowControl(QSerialPort::NoFlowControl);
       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()
    {
       serial->close();
       qDebug() << tr("Disconnected");
    }
    
    void SerialPort::writeData(const QByteArray &data)
    {
    
       serial->write(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?

    V 1 Reply Last reply 15 Jun 2017, 09:58
    0
  • V Offline
    V Offline
    VRonin
    replied to iRenji on 15 Jun 2017, 09:58 last edited by VRonin
    #2

    @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;
    }
    

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    V 1 Reply Last reply 15 Jun 2017, 13:43
    0
  • I Offline
    I Offline
    iRenji
    wrote on 15 Jun 2017, 10:48 last edited by
    #3

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

    V 1 Reply Last reply 15 Jun 2017, 13:06
    0
  • V Offline
    V Offline
    VRonin
    replied to iRenji on 15 Jun 2017, 13:06 last edited by
    #4

    @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?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    0
  • K Offline
    K Offline
    kuzulis Qt Champions 2020
    wrote on 15 Jun 2017, 13:09 last edited by
    #5

    Maybe reason is in:

    serial->setTextModeEnabled(true);
    

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

    I 1 Reply Last reply 15 Jun 2017, 13:16
    0
  • V Offline
    V Offline
    VRonin
    wrote on 15 Jun 2017, 13:11 last edited by
    #6

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

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    0
  • I Offline
    I Offline
    iRenji
    replied to kuzulis on 15 Jun 2017, 13:16 last edited by
    #7

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

    1 Reply Last reply
    0
  • K Offline
    K Offline
    kuzulis Qt Champions 2020
    wrote on 15 Jun 2017, 13:21 last edited by
    #8

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

    I 1 Reply Last reply 15 Jun 2017, 13:40
    0
  • I Offline
    I Offline
    iRenji
    replied to kuzulis on 15 Jun 2017, 13:40 last edited by
    #9

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

    1 Reply Last reply
    0
  • V Offline
    V Offline
    VRonin
    replied to VRonin on 15 Jun 2017, 13:43 last edited by VRonin
    #10

    @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; 
    }
    

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    2
  • R Offline
    R Offline
    Rondog
    wrote on 15 Jun 2017, 16:09 last edited by
    #11

    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 (?).

    1 Reply Last reply
    2
  • mrjjM Offline
    mrjjM Offline
    mrjj Lifetime Qt Champion
    wrote on 15 Jun 2017, 19:47 last edited by
    #12

    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.

    I 1 Reply Last reply 16 Jun 2017, 07:58
    2
  • I Offline
    I Offline
    iRenji
    replied to mrjj on 16 Jun 2017, 07:58 last edited by
    #13

    @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.

    mrjjM 1 Reply Last reply 16 Jun 2017, 10:55
    0
  • mrjjM Offline
    mrjjM Offline
    mrjj Lifetime Qt Champion
    replied to iRenji on 16 Jun 2017, 10:55 last edited by
    #14

    @iRenji
    ok super.

    Now try
    http://doc.qt.io/qt-5/qtserialport-terminal-example.html
    in both ends.

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

    1 Reply Last reply
    2
  • I Offline
    I Offline
    iRenji
    wrote on 20 Jun 2017, 16:17 last edited by
    #15

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

    serial->write(data);
    }

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

    1 Reply Last reply
    0

1/15

15 Jun 2017, 09:50

14 unread
  • Login

  • Login or register to search.
1 out of 15
  • First post
    1/15
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved