qt read ready signal

浪子不回头ぞ 提交于 2019-12-25 04:48:12

问题


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.


回答1:


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.




回答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.




回答3:


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
} 


来源:https://stackoverflow.com/questions/31605314/qt-read-ready-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!