0

I'm trying to set up a serial connection with a device which runs on a 1996 processor. This means that it can take several second for the data to be transmitted back to me. I know that the readyRead signal is generated every time new data is available, but my question is how long is generated for. Also is their a way that I can test for the ready read being low because if it stops being emitted when their is no longer any data left to read in, that would be really helpful.

 do{
      ui->label_5->setText("readyRead");    
 } while (readyRead() == true);
    
 QString output = serial->readAll();
 ui->label->setText(output);
 }

I hope this code segment illustrates what i'm trying to do. I haven't been able to test it as I haven't been able to work out how to call readyRead. I think that my readAll command might be in the wrong place but i'm not sure?

If anybody could help that would be greatly appreciated. Also if anybody knows a better way of doing this that would be greater still.

edit

I've added connect(serial, SIGNAL(readyRead()),this,SLOT(myReceivedData())); to my code but i've still got issues. I think their occurring because the processor is so slow that it takes several seconds to transmit the data, and that because of this my program is exiting the void loop only to return again because the readyRead flag is still true because their is still data left to read. I have tried adding a "sleep" command but this pauses the gui and seams to affect the reading time of the data.

My main question is am I correct about the readyRead signal still being true mid way through the data transition or am I missing a key fact?

Thanks for the replies.

Community
  • 1
  • 1
Jon CO
  • 25
  • 2
  • 10

3 Answers3

2
  1. There is no time duration for a signal. A signal is emitted once for a corresponding event (In this case when data is available to be read).

  2. You should use the QSerialPort::readyRead() signal by connecting to a slot as shown below:

    connect(serial, SIGNAL(readyRead()),this,SLOT(readSerialData()));
    
    void SomeClass::readSerialData(){
    QByteArray readData= serial->readAll();
    // Do something with readData
    
    }
    
  3. You can refer to Qt terminal example and QSerialPort documentation for using QSerialPort.

techneaz
  • 998
  • 6
  • 9
  • Hi, thanks for the reply, i've implemented your answer but its still got the same problem. I think that this might be because the readyRead flag gets triggered as soon as data is available, this means that i'm reading only half the data, then i'm exiting this void loop, and the readyRead flag is still high because there is still data left to transmit, so i'm re-entering the void loop. I've tried adding "sleep" commands but these freze the gui and seam to affect the read time of data. Any further ideas ? – Jon CO Jul 24 '15 at 08:39
  • How large is the data being received from the serial device? – techneaz Jul 24 '15 at 08:40
  • 30 Data Characters. I don't think its the size of the data that's the issue, its more that the processor is so slow that it can take up to 5 seconds to send it all. – Jon CO Jul 24 '15 at 08:45
  • If i understood you right! Are you receiving each character at every readyRead() signal? Try adding this to your slot `serial->waitForReadyRead(5000);` – techneaz Jul 24 '15 at 09:15
0

For this kind of requirement QT has Signal and Slots. You have a Signal which will be emitted and consumed by assigned SLOT method.

As you want to read Serial Data you should have Signal readyRead and connected to Slot which is done as below:

YourClass :: YourClass(QObject* parent): QObject(parent)
{
    connect(serial, SIGNAL(readyRead()),this,SLOT(myReceivedData()));       
}

As this code in your Class Constructor.

Implement this method as below:

void YourClass :: myReceivedData()
{
    QByteArray readData= serial->readAll();
    qDebug() << readCurData;   // Here I am printing the received Serial Data
} 
Amol Saindane
  • 1,568
  • 10
  • 19
0

It's really simple: When you act on the readyRead signal, you must read all data available. You won't be notified again. Also keep in mind that the signal is emitted when there is any non-zero amount of data available to read - so you should expect one or more bytes, but don't expect any particular number. For example, if your communications partner sends packets of a certain size, you should not expect to receive full packets unless the protocol guarantees that (UDP is about the only one).

You need to set up your code so that entire data is read, and you'll be OK.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313