1,qt中串口会用到
QCoreApplication;;processEventswhile (!isSendSuccess && --timeoutCnt) {QCoreApplication::processEvents(QEventLoop::AllEvents);QThread::msleep(5);}但是在程序中实例串口又是一个问题,如果初始化为全局,那么
QCoreApplication;;processEvents将不起作用,因为,全局变量是在程序最初就创建好的,是在mainwindow初始化之前,
这样,事件循环,就循环不到
QCoreApplication;;processEvents里面,
那么我们就不能全局实例串口:
这里我们通过在mainwindow实例后在实例串口为全局就可以解决:
class SerialPort : public QObject
{
Q_OBJECTpublic:explicit SerialPort(QObject *parent = 0);~SerialPort();
static SerialPort &GetSerialPort(void);
private:}
///
SerialPort &SerialPort::GetSerialPort()
{
static SerialPort SerialPortInterface;
return SerialPortInterface;
}
通过GetSerialPort将串口实例化,
然后我们可以在任何地方:通过这样的方式来获取串口,
SerialPort *printPort = &SerialPort::GetSerialPort();