PDA

View Full Version : QtSerialport windows problem



danics
7th September 2013, 14:12
hi i write a code for reading serial port in qt 5.1.1 and my code work great in linux but not in windows xp/7, i send a packet to my device and wait for response sync like below code



QByteArray packet;
if(m_serial.waitForReadyRead(readTimeout))
{
QByteArray header = m_serial.read(3);
if(header.length() == 3)
{
int len = header.at(2);
if (len <= 0)
{
emit errorhappend("Timeout get header, happend in get header");
return QByteArray();
}

packet = m_serial.read(len + 1);
int pos = 0;

if(packet.length() == len + 1)
{
packet.prepend(header);
return packet;
}

while (m_serial.waitForReadyRead(readTimeout)) {
pos = packet.length();
packet += m_serial.read(len + 1 - pos);
if(packet.length() == len + 1)
{
packet.prepend(header);
return packet;
}
}

emit errorhappend("Timeout get responce, happend in get packet ("
+ QString::number(packet.length())
+ "," +
QString::number(pos) + "," + QString::number(len) + ")");
}
else
emit errorhappend("Timeout get responce, cant get header");
}

return QByteArray();

if i get errorstring() of serial port after first waitforreadyread condition and i get "Unknown Error" in windows
serial port seems open correctly and write correctly too this is my write code


if(!m_serial.isOpen())
return false;

m_serial.write(writeBlocks(blockNo, bcount, data, devAddr));
if(m_serial.waitForBytesWritten(writeTimeout))
{
StatusCodes status = Error_Other;
bool val = responceNormal(&status);

if(status == Normal && val)
return true;
else
{
emit errorhappend("Previously error happend in Write function, Status(" + statusName(status) + ")");
}
}
else
emit timeout("Write, write data time out");

return false;

Lesiok
7th September 2013, 18:25
waitForReadyRaed() == true means that you have received at least one character. As well it can be two or more characters but this does not mean that you can read the three characters. So You must change logic of lines 2, 3, 4 and 5.

danics
8th September 2013, 04:28
tnx for your comment but its not important now, my program in windows dont get even one character :(, i have "Unknown error" on my serial port , why???? this seems like a bug, i read somewhere waitForReadyRaed() in windows must be run in another thread!! is it true?? i wonder nobody have this problem before

Lesiok
8th September 2013, 08:04
...i read somewhere waitForReadyRaed() in windows must be run in another thread!! is it true??
No, it is not true.

danics
8th September 2013, 08:45
seems its a bug in this library for windows, i currently test with mingw and vs2012 too but no difference, some times its read serial but after restart system its not read anything

kuzulis
8th September 2013, 19:10
Hi.

This "Unknown Error" with an SerialPortError::NoError mean that occured a Timeout error. In Qt 5.2 will be introduced an TimeoutError code.

Thus, at present it is "bug" which shall be fixed in future Qt 5.2.

I would advise to you to rewrite your code on non-blocking approach since blocking's approach implementation in QSerialPort has still some errors.

danics
10th September 2013, 07:13
Hi,
are you sure if i change my code (many code) to non-blocking, that will be work in windows??
i dont want create any pressure here :D, i mean non-blocking approch isnt a threaded blocking way?? am i wrong?
tnx

kuzulis
10th September 2013, 08:23
Non-blocking approach is use of signals/slots (event-based), e.g. see Terminal example. There is no need for the waitForXXX() methods and additional threads.

danics
10th September 2013, 08:25
i know that, i mean behind the scene in main code of qt serial how implement non-blocking approch, it is not a threaded blocking approch and then emit a signal?

kuzulis
10th September 2013, 10:28
I don't understand, what it isn't clear to you?

Do not use waitForXX methods, and for:

* reading - just use an readyRead() signal -> bytesAvailable(), if need -> read()
* writing - just use write() -> bytesWritten() singal, if need
* for waiting response - just use an QTimer to detect a wait timeout

1. write()
2. run a wait timer
3. handle or readyRead() or timeout() signal

.. what is problems?

danics
10th September 2013, 14:33
:D sorry for my bad english if you confused and tnx for your consideration,
i know how implement non-blocking approach in my code but as i said i must change so many code!!!!
so i ask myself why non-blocking approch must work in windows? i mean whats the code of qtserialport library written with some skilled programmers, for emitting a signal like readyRead() for me? :)
Did they create a thread and listen on port until some bytes come and send a signal? so they listen function is'nt waitForReadyRead() ? :)
if they use this function for emitting a signal then non-blocking approach must not work in windows too !! i think. (cause in blocking approach dont work)
if they usen't this technic i can use non-blocking approach, right?. now you understand why i ask? :)
if you see my replies Lesiok, he says to me running a blocking approach in another thread dont effect on my result.

kuzulis
10th September 2013, 15:15
so i ask myself why non-blocking approch must work in windows?

And why not? o_O


i mean whats the code of qtserialport library written with some skilled programmers, for emitting a signal like readyRead() for me?

Yes, it is a main Qt (QIODevice) feature. Any classes, derived from QIODevice do it. Please, look documentation.


Did they create a thread and listen on port until some bytes come and send a signal?

Not, thread is not used. Is used an Qt-event loop. Please see how it work in sources of QtSerialPort...


so they listen function is'nt waitForReadyRead() ?

Are "listening" an Win32 events from the device. Though, the word "listening" here isn't pertinent...


if they use this function for emitting a signal then non-blocking approach must not work in windows too !! i think. (cause in blocking approach dont work)

Here I didn't understand your thought.


if they usen't this technic i can use non-blocking approach, right?. now you understand why i ask?

No, I don't understand. Please, read documentation on QIODevice and so on.


if you see my replies Lesiok, he says to me running a blocking approach in another thread dont effect on my result.

The methods waitForXX() usually shall be used in other thread because if they are used in main thread - that main thread will be blocked (will be blocked an main GUI Qt-event loop and so on) and you got freezes.

Besides, I say to you, as the QtSerialPort developer - that the waitForXXX() methods still have some errors in implementation. Therefore I recommend not to use them.

danics
10th September 2013, 16:03
i mean behind the scene in main code of qt serial how implement non-blocking approch, it is not a threaded blocking approch and then emit a signal?


Not, thread is not used. Is used an Qt-event loop. Please see how it work in sources of QtSerialPort...


thanks for your usefull answer :D

danics
12th September 2013, 14:08
just for others known :(
non-blocking approach dont work either in windosw xp/7 but run in linux

^NyAw^
12th September 2013, 14:51
Hi,



non-blocking approach dont work either in windosw xp/7 but run in linux


I have an application working on Windows XP using QtSerialPort on Qt 4.8.1 with non-blocking approach and it works perfectly.

danics
12th September 2013, 20:25
i m using Qt 5.1.1