我想用QSerialport、QThread实现,在子线程里:run(){serial->init();connect(serial,&QSerialPort::readReady,this,&readData);serial->write(data);//data为查询指令exec();}//slotre...
我想用QSerialport、QThread实现, 在子线程里:
run()
{
serial->init();
connect(serial, &QSerialPort::readReady, this, &readData);
serial->write(data); //data为查询指令
exec();
}
//slot
readData()
{
buf = serial->readAll();
emit receiveData(buf); //提交信号给主线程UI显示数据
sleep(1); //数据响应隔一秒再次查询
serial->write(data);
}
主要是想每隔一秒收一次指令,但是程序运行后,发现在通讯过程中,UI会卡住,查看资料后才知道SLOT也是在主线程里运行的,在SLOT里用sleep就是主线程sleep。那我该怎么实现我想要的效果。
我是新手,请给个明白一点的思路。
我自己用QTimer定时发送就不会卡UI了
run()
{
serial->init();
connect(serial, &QSerialPort::readReady, this, &readData);
QTimer *timer = new QTimer();
connect( timer, SIGNAL(timeout()), this, SLOT(writeData()) );
timer->start(1000);
exec();
}
//slot
writeData()
{
serial->write(data);
}
//slot
readData()
{
buf = serial->readAll();
emit receiveData(buf); //提交信号给主线程UI显示数据
}
展开