Unsolved Serialport Timeout Error
-
Hi,
I created an application to send command to a printer. But after writing, some times it shows QSerialPort::SerialPortError(TimeoutError).while(1) { u_int8_t u_arr_n8ArraytoSend[]={0x1b,0x4a,0}; QTextStream textstream(&file); u_arr_n8ArraytoSend[2]=20; u_int8_t u_nRxBytes; u_int8_t u_n8BytesReturn; QThread::sleep(0.5); u_nRxBytes=serial->write((char *)u_arr_n8ArraytoSend,3); u_n8BytesReturn=serial->waitForBytesWritten(1000); qDebug()<<"u_n8BytesReturn"<<u_n8BytesReturn; if(WRITESUCCESS==u_n8BytesReturn) { textstream<<"Write Success"<<u_n8BytesReturn<<"\n"; u_int8_t u_arr_n8ArraytoSend2[]={0x1b,0x69}; QThread::sleep(0.5); u_nRxBytes=serial->write((char *)u_arr_n8ArraytoSend2,2); u_n8BytesReturn=serial->waitForBytesWritten(1000); if(WRITESUCCESS==u_n8BytesReturn) { textstream<<"Write Success"<<u_n8BytesReturn<<"\n"; ncount++; qDebug()<<"ncount"<<ncount; } else { qDebug() <<"error is"<<serial->error(); textstream<<"Write Fail"<<u_n8BytesReturn<<"\n"; } } else { qDebug() <<"error is"<<serial->error(); textstream<<"Write Fail"<<u_n8BytesReturn<<"\n"; } QThread::sleep(10); textstream<<"count:"<<ncount<<"\n"; }
This is the code i wrote. Is there any problem with this code??
Thanks.
-
Hi @QTlearner90,
First question, why sending the command in a while loop ? what are you trying to achieve exactly ?
Here is the possible causes I can see:
- Maybe the delay between the commands is too short (about 10ms). The printer may receive a command when the processing of the previous one is not terminated. Or having his serial buffer full because commands are coming too fast.
- The printer probably send an acknowledge to indicate the command has been received and processed correctly. Reading and checking this acknowledge could help to synchronize correctly the communication. Or at least clearing the output buffer of the printer.
And another thing,
QThread::sleep(0.5);
will be implicitly converted toQThread::sleep(0)
by the compiler as this function does not support real number, only unsigned long. -
hi @Gojir4 ,
For checking printers life, i wrote all these commands in while loop. In normal operation i am giving QThread::sleep(15) sec . But that time also this error happened.
-
It seems source code written by a firmware developer instead of a software developer.
Please don't use things like
while(1)
or
QThread::sleep(0.5)
if you aren't in a real thread. -
You are right, QThread::sleep is in second, not in milliseconds. So 10 seconds is far enough to process the command.
How do you know the printer has received and understood the command ?
-
QSerialPort::SerialPortError(TimeoutError)
This means that the WAIT_TIMEOUT error occurred, if we say about windows (it happens on system WaitForSingleObject function); or when the system poll() function fails with 0 code, if we say about *nix. It comes from the driver/hardware itself... So, check our HW.
-
Hi @Gojir4 ,
Thanks for your response. I changed my code by writing qthread::sleep(15). again error happens. -
You should rethink your code globally.
you can't use QThread::sleep(0.5) out of a QThread.
Probably QThread::sleep(0.5) stops the application and the serial buffers.
Please use QTimer if you have to send / receive continuosly.