PDA

View Full Version : QserialPort and QTcpSocket, led to a difference in the QByteArray sent through them



cstr
7th June 2013, 21:05
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:


-21 / 235
-112 / 144

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:


107 'k'
16


Why? How to convert from

107 'k'
16
to

-21 / 235
-112 / 144
?

Thanks for the attention

ChrisW67
8th June 2013, 01:34
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.

cstr
13th June 2013, 04:02
In case of the serial, it was noted that when writing
-16 / 240 what is read is
112 'p'.

Why can that happen?

A QByteArray is being used to write/read.

Thanks for the attention.

ChrisW67
13th June 2013, 07:55
The byte you send has the most significant bit set, and what you receive does not.


240 = 11110000 in binary
112 = 01110000 in binary

Have you eliminated the possibility that your serial data channel is configured for 7 data bits?

cstr
19th June 2013, 19:10
Hi ChrisW67,

The serial port is configured with
setDataBits(QSerialPort::Data8), 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.

kuzulis
19th June 2013, 19:41
Check returns value from


setDataBits(QSerialPort::Data8)

at successful should be "true".

cstr
19th June 2013, 20:01
Is true.

Thank you kuzulis.

kuzulis
19th June 2013, 20:10
Is true.

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

cstr
19th June 2013, 21:15
SettingsDialog::Settings s = settingsDialog->settings();
serial->setPortName(s.name);
serial->open(QIODevice::ReadWrite);
if (!serial->setBaudRate(s.baudRate)
&& serial->setDataBits(s.dataBits)
&& serial->setParity(s.parity)
&& serial->setStopBits(s.stopBits)
&& serial->setFlowControl(s.flowControl))
return; // Failed

kuzulis
20th June 2013, 07:28
Please replace part of code to:



bool success = serial->open(QIODevice::ReadWrite)
&& serial->setBaudRate(s.baudRate)
&& serial->setDataBits(s.dataBits)
&& serial->setParity(s.parity)
&& serial->setStopBits(s.stopBits)
&& serial->setFlowControl(s.flowControl);

if (!success ) {
return; // Failed
}


and check now.

cstr
20th June 2013, 11:34
Still true.

Thank you kuzulis.

kuzulis
20th June 2013, 13:02
In this case, check the correctness of installation of databits in DCB in debugger after setDataBits() call.

cstr
20th June 2013, 14:02
My fault! Everything is working now. May be a full rebuild/make was required to effectively apply the setDataBits.

Sorry.