Results 1 to 13 of 13

Thread: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through them

  1. #1
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QserialPort and QTcpSocket, led to a difference in the QByteArray sent through them

    When sending 2 Bytes (a QByteArray) through a QTcpSocket, using the methods write(QByteArray) and readAll.
    In the write side, the debugger shows the values:
    Qt Code:
    1. -21 / 235
    2. -112 / 144
    To copy to clipboard, switch view to plain text mode 
    In the read side, the debugger shows the same values, as expected.
    But When the same Bytes are sent through the QSerialPort, using the methods write(QByteArray) and readAll, a difference is noted.
    In the write side, the debugger shows the values as above.
    And in the read side, it shows:
    Qt Code:
    1. 107 'k'
    2. 16
    To copy to clipboard, switch view to plain text mode 

    Why? How to convert from
    Qt Code:
    1. 107 'k'
    2. 16
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. -21 / 235
    2. -112 / 144
    To copy to clipboard, switch view to plain text mode 
    ?

    Thanks for the attention

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    235 - 128 == 107
    144 - 128 == 16
    My guess that your serial port is configured for 7 data bits, or the receiving software is, so the most significant bit (128) is being dropped.

  3. The following user says thank you to ChrisW67 for this useful post:

    cstr (8th June 2013)

  4. #3
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    In case of the serial, it was noted that when writing
    Qt Code:
    1. -16 / 240
    To copy to clipboard, switch view to plain text mode 
    what is read is
    Qt Code:
    1. 112 'p'
    To copy to clipboard, switch view to plain text mode 
    .

    Why can that happen?

    A QByteArray is being used to write/read.

    Thanks for the attention.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    The byte you send has the most significant bit set, and what you receive does not.
    Qt Code:
    1. 240 = 11110000 in binary
    2. 112 = 01110000 in binary
    To copy to clipboard, switch view to plain text mode 
    Have you eliminated the possibility that your serial data channel is configured for 7 data bits?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  6. #5
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Hi ChrisW67,

    The serial port is configured with
    Qt Code:
    1. setDataBits(QSerialPort::Data8)
    To copy to clipboard, switch view to plain text mode 
    , but actually I'm not sure of which variable to verify in the debugger in order to confirm. May be ByteSize, under currentDcb, d_ptr? The value of ByteSize is 7.

    Emulated serial ports are being utilized, could the issue be related to the emulator? The software being used is Virtual Serial Port Kit version 5.4.1 from FabulaTech (www.virtual-serial-port.com). Alternatively the Virtual Serial Ports Emulator 0.938.4.846 from Eterlogic (www.eterlogic.com) was used and it presents a different behavior.
    It shows the exact same data which is sent, as expected.
    Last edited by cstr; 19th June 2013 at 19:21.

  7. #6
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Check returns value from

    Qt Code:
    1. setDataBits(QSerialPort::Data8)
    To copy to clipboard, switch view to plain text mode 

    at successful should be "true".

  8. #7
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Is true.

    Thank you kuzulis.

  9. #8
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Is true.
    It is strange. Give a code where you configure and open port.

  10. #9
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Qt Code:
    1. SettingsDialog::Settings s = settingsDialog->settings();
    2. serial->setPortName(s.name);
    3. serial->open(QIODevice::ReadWrite);
    4. if (!serial->setBaudRate(s.baudRate)
    5. && serial->setDataBits(s.dataBits)
    6. && serial->setParity(s.parity)
    7. && serial->setStopBits(s.stopBits)
    8. && serial->setFlowControl(s.flowControl))
    9. return; // Failed
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Please replace part of code to:

    Qt Code:
    1. bool success = serial->open(QIODevice::ReadWrite)
    2. && serial->setBaudRate(s.baudRate)
    3. && serial->setDataBits(s.dataBits)
    4. && serial->setParity(s.parity)
    5. && serial->setStopBits(s.stopBits)
    6. && serial->setFlowControl(s.flowControl);
    7.  
    8. if (!success ) {
    9. return; // Failed
    10. }
    To copy to clipboard, switch view to plain text mode 

    and check now.

  12. #11
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    Still true.

    Thank you kuzulis.

  13. #12
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    In this case, check the correctness of installation of databits in DCB in debugger after setDataBits() call.

  14. #13
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QserialPort and QTcpSocket, led to a difference in the QByteArray sent through th

    My fault! Everything is working now. May be a full rebuild/make was required to effectively apply the setDataBits.

    Sorry.

Similar Threads

  1. Qt5 cmake and QSerialPort
    By Chris.Burner in forum Newbie
    Replies: 1
    Last Post: 21st April 2013, 16:13
  2. Replies: 1
    Last Post: 24th March 2013, 22:37
  3. Replies: 6
    Last Post: 23rd June 2010, 14:48
  4. QTcpSocket QDataStream QByteArray
    By Grimlock in forum Newbie
    Replies: 1
    Last Post: 14th December 2009, 22:47
  5. Difference between normal socket and QTcpSocket
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 16th June 2007, 04:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.