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. Serialport Timeout Error

Serialport Timeout Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.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.
  • Q Offline
    Q Offline
    QTlearner90
    wrote on last edited by
    #1

    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.

    Gojir4G 1 Reply Last reply
    0
    • Q QTlearner90

      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.

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      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:

      1. 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.
      2. 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 to QThread::sleep(0) by the compiler as this function does not support real number, only unsigned long.

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        QTlearner90
        wrote on last edited by QTlearner90
        #3

        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.
        
        Gojir4G 1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #4

          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.

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          3
          • Q QTlearner90

            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.
            
            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #5

            @QTlearner90

            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 ?

            Q 1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • Gojir4G Gojir4

                @QTlearner90

                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 ?

                Q Offline
                Q Offline
                QTlearner90
                wrote on last edited by
                #7

                Hi @Gojir4 ,
                Thanks for your response. I changed my code by writing qthread::sleep(15). again error happens.

                1 Reply Last reply
                0
                • mrdebugM Offline
                  mrdebugM Offline
                  mrdebug
                  wrote on last edited by
                  #8

                  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.

                  Need programmers to hire?
                  www.labcsp.com
                  www.denisgottardello.it
                  GMT+1
                  Skype: mrdebug

                  1 Reply Last reply
                  1

                  • Login

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