snow_starzz
2nd December 2013, 09:03
Hi guys..
Previously i'm using external library for accessing RS232 serial port. Now i want to try to use Qserialport in Qt5.1.
however, i have some problems as i'm using multithreading program (Qthread).
Main thread
void MainWindow::on_plotButton_clicked()
{
//Initialize thread for acc
Thread_Acc = new QThread;
Sensors* Acc = new Sensors();
Acc->moveToThread(Thread_Acc);
connect(Thread_Acc, SIGNAL(started()), Acc, SLOT(FetchData_AW()));
connect(Acc, SIGNAL(finished()), Thread_Acc, SLOT(quit()));
connect(Acc, SIGNAL(finished()), Acc, SLOT(deleteLater()));
connect(Thread_Acc, SIGNAL(finished()), Thread_Acc, SLOT(deleteLater()));}
Worker thread
void Sensors::FetchData_AW()
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
if(info.portName() == "COM24")
{
cport_nr_Acc.setPort(info);
cport_nr_Acc.close();
if (cport_nr_Acc.open(QIODevice::ReadWrite))
{
cport_nr_Acc.setBaudRate(57600);
cport_nr_Acc.setDataBits(QSerialPort::Data8);
cport_nr_Acc.setParity(QSerialPort::NoParity);
cport_nr_Acc.setStopBits(QSerialPort::OneStop);
cport_nr_Acc.setFlowControl(QSerialPort::NoFlowCon trol);
}else
return;
break;
}
}
Fetch_ACC = new QTimer (this);
connect(Fetch_ACC,SIGNAL(timeout()),this,SLOT(Loop _DataAW()));
Fetch_ACC->start(55);
}
the program can run but it display warning as below and the input from the device is also not as expected (value not stabilize) comparing if i use external rs232 library.
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
is there any interruption of the port if I'm using it from other than main thread?
i already make the Qserialport as the variable in worker-thread class instead of global variable.
what can i do to solve this issues because i really want to use readyRead signal.
Thanks in advance... =)
Previously i'm using external library for accessing RS232 serial port. Now i want to try to use Qserialport in Qt5.1.
however, i have some problems as i'm using multithreading program (Qthread).
Main thread
void MainWindow::on_plotButton_clicked()
{
//Initialize thread for acc
Thread_Acc = new QThread;
Sensors* Acc = new Sensors();
Acc->moveToThread(Thread_Acc);
connect(Thread_Acc, SIGNAL(started()), Acc, SLOT(FetchData_AW()));
connect(Acc, SIGNAL(finished()), Thread_Acc, SLOT(quit()));
connect(Acc, SIGNAL(finished()), Acc, SLOT(deleteLater()));
connect(Thread_Acc, SIGNAL(finished()), Thread_Acc, SLOT(deleteLater()));}
Worker thread
void Sensors::FetchData_AW()
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
if(info.portName() == "COM24")
{
cport_nr_Acc.setPort(info);
cport_nr_Acc.close();
if (cport_nr_Acc.open(QIODevice::ReadWrite))
{
cport_nr_Acc.setBaudRate(57600);
cport_nr_Acc.setDataBits(QSerialPort::Data8);
cport_nr_Acc.setParity(QSerialPort::NoParity);
cport_nr_Acc.setStopBits(QSerialPort::OneStop);
cport_nr_Acc.setFlowControl(QSerialPort::NoFlowCon trol);
}else
return;
break;
}
}
Fetch_ACC = new QTimer (this);
connect(Fetch_ACC,SIGNAL(timeout()),this,SLOT(Loop _DataAW()));
Fetch_ACC->start(55);
}
the program can run but it display warning as below and the input from the device is also not as expected (value not stabilize) comparing if i use external rs232 library.
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
is there any interruption of the port if I'm using it from other than main thread?
i already make the Qserialport as the variable in worker-thread class instead of global variable.
what can i do to solve this issues because i really want to use readyRead signal.
Thanks in advance... =)