Qt串口操作遇到的问题

2 篇文章 0 订阅

1.QtSerialport 是我自己定义的串口操作的类,里面的成员有QSerialPort *serialport;

在主程序里,我把串口操作又放到了一个线程里

        serialPort = new QtSerialport();
        serialPortThread = new QThread();
        serialPort->moveToThread(serialPortThread); //移动到线程里
        connect(serialPort,SIGNAL(setBrightnessResult(bool)),this,SLOT(OnSetBrightnessResult(bool))); //主程序通过发信号触发操作
        connect(this,SIGNAL(setBrightness(int)),serialPort,SLOT(OnSetScreenBrightness(int)));//通过信号,串口设置屏的亮度操作
        connect(serialPort,SIGNAL(destroyed()),serialPortThread,SLOT(quit()));//串口操作类用deleterLater()销毁后,触发线程退出
        connect(serialPortThread, SIGNAL(finished()), serialPortThread, SLOT(deleteLater()));//线程退出,触发线程对象销毁
        serialPortThread->start();//开启线程

串口操作类Init函数里有new的操作,但这个函数不能在主程序里调用,而是要放在线程里,不然会报错

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x4a6f9c0), parent's thread is QThread(0x178e950), current thread is QThread(0x4a89e70)

,好像没影响

我放在了OnSetScreenBrightness槽函数里调用

void QtSerialport::Init(QString name,int bitrate,int databits,int parity,int stopbits)
{
    serialport = new QSerialPort();

    connect(serialport,SIGNAL(readyRead()),this,SLOT(OnReadyRead()));//有数据时,会触发读数据,但可能读不全

    serialport->setPortName(name);

}

2.QSerialPort串口读数据可能一次读不全,有数据时,要用校验如异或校验,来确保读的完整,不全就保存数据退出,下次

再读,

char getOneXor(QByteArray buf)//异或校验
{
    char tmp = buf[0];
    for(int i = 0; i < buf.count(); i++)
    {
        tmp = tmp ^ buf[i+1];
    }
    return tmp;
}

void QtSerialport::OnReadyRead()
{
    usleep(50*1000);//等50毫秒再读
    QByteArray buf = serialport->readAll();
    tmpBuf = tmpBuf.append(buf);//保存数据

    applog::LOGGER->Log(applog::kLogInfo, "QtSerialport::OnReadyRead tmpBuf= %s", tmpBuf.toHex().constData());
    QByteArray tmp = tmpBuf.left(tmpBuf.count() -1);
    char lastByte = tmpBuf.at(tmpBuf.count() -1);
    if(lastByte != getOneXor(tmp))//异或校验
    {
        applog::LOGGER->Log(applog::kLogInfo, "QtSerialport::OnReadyRead buffer= %s,but not correct", tmpBuf.toHex().constData());
        return;
    }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值