Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt串行端口错误-未读取数据_Qt_Serial Port_Qtserialport - Fatal编程技术网

Qt串行端口错误-未读取数据

Qt串行端口错误-未读取数据,qt,serial-port,qtserialport,Qt,Serial Port,Qtserialport,我正在尝试使用Qt SerialPort库读取串行端口。我可以用超级终端读取数据 在Qt中,我使用了以下代码来尝试做同样的事情。Qt表示端口已正确打开,但由于某些原因,从串行端口可用的字节数始终为0 serial.setPortName("COM20"); if (serial.open(QIODevice::ReadOnly)) qDebug() << "Opened port " << endl; else qDebug() << "Un

我正在尝试使用Qt SerialPort库读取串行端口。我可以用超级终端读取数据

在Qt中,我使用了以下代码来尝试做同样的事情。Qt表示端口已正确打开,但由于某些原因,从串行端口可用的字节数始终为0

serial.setPortName("COM20");

if (serial.open(QIODevice::ReadOnly))
    qDebug() << "Opened port " << endl;
else
    qDebug() << "Unable to open port" << endl;

serial.setDataBits(QSerialPort::Data8);

serial.setParity(QSerialPort::EvenParity);

serial.setBaudRate(QSerialPort::Baud115200);


qDebug() << "Is open?? " << serial.isOpen();


// Wait unit serial port data is ready
while (!serial.bytesAvailable())
{
    //qDebug() << serial.bytesAvailable()<<endl;
    continue;
}

QByteArray data = serial.read(100);

qDebug() << "This is the data -" << data << endl;

serial.close();
serial.setPortName(“COM20”);
if(serial.open(QIODevice::ReadOnly))
qDebug()1)它的错误:

推荐解决方案:


我将qserialport_win.cpp中的第161行更改为:

return error;

现在,我的简化示例起作用了


2) 我还建议:

一步:打开端口:

            if (this->open(QIODevice::ReadWrite)) {
            } else {
                qDebug() << "\n Can't open port | " << this->errorString();
            }
if(此->打开(QIODevice::ReadWrite)){
}否则{
qDebug()setBaudRate(此->波特率)
&&此->设置数据位(此->数据位)
&&此->设置奇偶校验(此->奇偶校验)
&&此->设置停止位(此->停止位)
&&此->设置流量控制(此->流量控制)){

qDebug()握手通常理解得很差,只是被忽略了。您不能忽略对setFlowControl()的调用,传递QSerialPort::HardwareControl。setTopBits()也是awol。
return !error;
            if (this->open(QIODevice::ReadWrite)) {
            } else {
                qDebug() << "\n Can't open port | " << this->errorString();
            }
            if (
                    this->setBaudRate(this->baudRate)
                    && this->setDataBits(this->dataBits)
                    && this->setParity(this->parity)
                    && this->setStopBits(this->stopBits)
                    && this->setFlowControl(this->flowControl)) {
                qDebug() << "\n[ info ]        Port settings successfully";
            } else {
                qDebug() << "\n[ error ]        Port settings failed";
            }