readAll();//取串口中的数据Por" />
qt采用readAll()串口一次性读不全信息 | 您所在的位置:网站首页 › qt串口接收数据乱码 › qt采用readAll()串口一次性读不全信息 |
qt串口一次性读不全信息,导致字符串无法处理,采用以下方案: 1,重复读,直到结束位(“\r”huo"\n"等)出现才结束(亲测可用) QByteArray data; data = serialPort->readAll();//取串口中的数据 Port_str.append(data); if (Port_str.contains('\r'))//只有等到\n的时候才能进入 { Port_str = Port_str.simplified();//去除空白字符 emit UpdateStatus(Port_str); Port_str.remove(0, 1); bool ok; double val; val = Port_str.toDouble(&ok); //ok=true;val;12.3456. if (ok) { val = val*SettingIni::GetInstance()->readMulriple(); ui.lineEdit_EA_Read->setText(QString::number(val, 'f', 4)); } else { ui.lineEdit_EA_Read->setText(Port_str); } Port_str.clear(); }
2,采用waitForReadyRead(10)(目前未用到) ::qApp->processEvents()函数的意思就是让调用此函数的线程执行其消息队列中的事件,直至没有事件可以执行为止。 while (1) { if (port.waitForReadyRead(10)) { qApp->processEvents(); port->readAll(); } }
|
CopyRight 2018-2019 实验室设备网 版权所有 |