qt 连接扫码枪,检查串口插拔

复制代码
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(result);
    Q_UNUSED(eventType);
    MSG* pMsg = reinterpret_cast<MSG*>(message);

    if (pMsg->message == WM_KEYUP)
    {
        ushort key  = pMsg->wParam;
        if (key == '\r')
        {
            ui->sn_lineEdit->setText(m_scanSN);
            m_scanSN = "";
        }
        else
        {
            //可见字符
            if (key >=  32  && key < 127)
            {
                m_scanSN = m_scanSN + QChar(key);
            }
        }
    }

    if(pMsg->message == WM_DEVICECHANGE){
        PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)pMsg->lParam;
        switch (pMsg->wParam) {
        case DBT_DEVICEARRIVAL:
            if(lpdb->dbch_devicetype == DBT_DEVTYP_PORT){
                qDebug()<<"插入串口设备!";
                emit signalUpdataSerialPort();
            }
            break;
        case DBT_DEVICEREMOVECOMPLETE:
            if(lpdb->dbch_devicetype == DBT_DEVTYP_PORT){
                qDebug()<<"串口设备离开";
                m_SerialPort.closeSerialPort();
                ui->openSerialport_Btn->setText("打开串口");
                m_serialPortStatus = false;
                logMessage("关闭串口");
                emit signalUpdataSerialPort();
            }
        }
    }

    return false;
}
复制代码

 

posted @   雾枫  阅读(119)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
历史上的今天:
2022-07-26 Qt_——关联xml文件(获取xml文件里的节点数据)
点击右上角即可分享
微信分享提示