QtSerialPort中如何判断数据发送完毕?
我在做个串口isp的例程,基于主从模式通信。我用的qt5.9 serialport 模块。port->write()方法后port->waitForBytesWritten(3000)判断发送完成与否。实际调试中发现不稳定,超时判断一直返回的true,实际上串口没有发送出数据。如何解决?代码如下:while(write_addr<size){ send_erase_sector(write_addr); port->write((char*)isp_buf,qint64(isp_num)); write_addr+=512; if(!port->waitForBytesWritten(3000)){ emit isp_error("串口超时"); //实际发现程序从没有跑到这里,可是实际串口没有发出数据 ISPStop(); return; } if(read_loop()==0){ //这里阻塞方式读取串口 qDebug("擦除:%d",write_addr); } else{ emit isp_error("擦除超时"); ISPStop(); return; } emit isp_progress((float)write_addr/size*10); }
write()函数会返回实际写入的个数,你可以用返回值验证是否写入。
qint64 QIODevice::write(const char *data)
This is an overloaded function.
Writes data from a zero-terminated string of 8-bit characters to the device. Returns the number of bytes that were actually written, or -1 if an error occurred. This is equivalent to 不一定是接口的问题,鉴于串口的不稳定性,从协议上想办法,发出数据必有返回校验,超时未返回的重发
页:
[1]