Qt5下串口编程中“QIODevice::write (QSerialPort): device not open”问题的分析处理总结

一、实现功能说明:

在主窗口中先配置好串口并“打开串口”,在菜单中点击“测量”子窗口,进入子窗口画面,点击子窗口的“测量”按钮,实现读取传感器指令的数据发送。Qt5下串口编程中“QIODevice::write (QSerialPort): device not open”问题的分析处理总结_第1张图片

二、程序说明:

主窗口:1、MainWidget.h:定义类MainWidget,构造函数Mainwidget,公有变量 Qserialport serial,公有函数 void  serial_write(const char *buf);

              2、MainWidget.cpp中,有serial_init(),serial_open(),serial_close(),serial_write(),serial_readyread()等串口相关函数的程序,以及打开子画面的槽函数ShowMeasureWindow()和measureAction

子窗口:1、Measure.h:定义类Measure,构造函数Measure,按钮QPushButton *measureBtn以及d对应的槽函数virtual void measureread();

              2、Measure.cpp:画面布局,构造函数内添加:connect(measureBtn,SIGNAL(clicked()),this,SLOT(measureread()));

void measureread()内调用Mainwidget下的serial_write(const char *buf)函数

详细的measureread()如下:

void Measure::measureread()
{  
        char Data_1[8]={0x01,0x03,0x00,0x0F,0x00,0x1C,0x75,0x09};
        char *buf=Data_1;
        MainWidget mainwidget;
        mainwidget.serial_write(buf);
}

三、问题及分析:

编译没问题,但是点击"测量"按钮,数据没有发送成功,应用程序输出:

QIODevice::write (QSerialPort): device not open

error code = QSerialPort::SerialPortError(DeviceNotFoundError)

error string = "No such file or directory"

通过进一步分析,通过增加调试程序:

    //serial_init()下增加程序
    qDebug() << "portName() = " << info.portName();
    qDebug() << "serial description = " << info.description();

    //serial_open()下增加程序
    qDebug() << "read portName() = " << showWidget->portBox->currentText();
    qDebug() << "serial rateBox = " << showWidget->rateBox->currentText().toInt();
    qDebug() << "serial dataBox= " << showWidget->dataBox->currentIndex();
    qDebug() << "serial checkBox= " <checkBox->currentIndex();
    qDebug() << "serial stopBox= " <stopBox->currentIndex();

    qDebug() << "isopen = " << serial.isOpen();
     //seral_write()函数下增加程序
    qDebug() << "isopen state = " << serial.isOpen(); //measureread()函数中调用

程序运行时输出窗口有:

portName() = "COM1"

serial description = "Virtual Serial Port (Eltima Softwate)"

read portName() = "COM1"

serial rateBox = 9600

serial dataBox= 3

serial checkBox= 0

serial stopBox= 0

isopen = true

isopen state = false

打开测量子画面时,isopen = true,但是当点击“测量”按钮时,isopen state = false. 说明measureread()函数中虽然实现了对serial_write()的调用,但是此时串口的确关闭了,正如QIODevice::write (QSerialPort): device not open所提示的。

说明measureread()中对serial_write()的调用有问题。

四、问题解决:

研究measureread()中的程序,发现是主窗口对子窗口内函数的调用方法,而不是子窗口对主窗口的调用方法,因此,重新修改程序如下(最后两句):

void Measure::measureread()
{  
        char Data_1[8]={0x01,0x03,0x00,0x0F,0x00,0x1C,0x75,0x09};
        char *buf=Data_1;

        MainWidget *ptr =(MainWidget*)parentWidget();
        ptr->serial_write(buf);

}

程序修改后,编译运行,能正常实现数据的发送。

五、总结

1、主窗口程序中调用子窗口函数sub():

    ClassSub  *classsub;  //子窗口的ClassSub

    classsub=new ClassA(this);

    classsub>sub();

或者:ClassA aaaa;

aaaa.sub();

2、主窗口程序中调用子窗口控件btn:

ClassSub  *classsub;

classsub->btn->setEnabled(false);

3、子窗口程序中调用主窗口函数read():

ClassMain  *ptr=(ClassMain*)parentWidget();

ptr->read();

 

你可能感兴趣的:(工业控制)