写入串行端口时Qt崩溃

Qt crashing when writing to serial port

本文关键字:Qt 崩溃 串行端口      更新时间:2023-10-16

嗨,QT中有一个程序,每当我向串行端口写入时,它似乎都会崩溃。我使用的是Mac OSx 15英寸retina。以下是相关代码:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    //set central widget for the Main Window
    centralWidget = new QWidget(this);
    this->setCentralWidget(centralWidget);
    //creation and attribution of slider
    slider = new QSlider();
    slider->resize(255, 20);
    slider->setOrientation(Qt::Horizontal);
    slider->setRange(0, 255); //0-255 is range we can read
    //layout with slider and lcd
    main_layout = new QVBoxLayout();
    main_layout->addWidget(slider);
    //set layout to the widget inside MainWindow
    centralWidget->setLayout(main_layout);
    /*Connection Events*/
    //connection between the slider event and the transmission function
    QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(transmitCmd(int)));
}
void MainWindow::init_port()
{
    port = new QSerialPort("COM3");     //create port
    port->open(QIODevice::ReadWrite | QIODevice::Unbuffered); //open port
    if(!port->isOpen())
    {
        QMessageBox::warning(this, "port error", "Can't open port!");
    }
    //set port properties
    port->setBaudRate(QSerialPort::Baud9600); //9600 FOR ARDUINO
    port->setFlowControl(QSerialPort::NoFlowControl);
    port->setParity(QSerialPort::NoParity);
    port->setDataBits(QSerialPort::Data8);
    port->setStopBits(QSerialPort::OneStop);
}
void MainWindow::transmitCmd(int value)
{
    //if value wasn't between 0-255, no transmission
    if(value < 0 || value > 255)
        return;
    char *buffer = (char*) new int(value);
    //send buffer to serial port
    port->write(buffer);
}

它在线路端口->写入(缓冲区)上崩溃。我使用的是QT 5.5和QTSerialPort。

尝试

port->write (buffer, sizeof (int));

您使用了QIODevice::write(const char* data)重载,它需要一个以null结尾的字符串(您的缓冲区不是)。很自然,io设备不知道何时停止。。。

无论如何,这应该可以修复您的崩溃。顺便说一句,同样可以说:

port->write (reinterpret_cast<const char*> (&value), sizeof (int))

但是,请注意,上面的两个命令都将通过您的端口发送4个字节(sizeof int)的数据(按照系统的字节顺序,可能是little endian)。也许(从你在函数开始时的0-255检查来看),这实际上不是你想要的。如果你只想发送一个字节:

unsigned char valueCh = static_cast<unsigned char> (value);
port->write (reinterpret_cast<const char*> (&valueCh), 1)

附录:

正如您所写的,您只是忘记了init调用。很好的捕获@perencia!但是,仍然值得理解为什么transmitCmd()确实有效——因为乍一看,它不应该。

我仍然认为您使用了错误的write()调用,但事实上,它可以正常工作。发生的是:

假设我们有value == 17。然后,在一个小的endian体系结构上,您的缓冲区看起来是这样的:

// "17" little-endian
11 00 00 00
^
|
buffer

并且您对write(buffer)的调用将看到您想要发送的正确数据字节,后面跟着一个导致其停止的nul字节。

您没有调用init_port。至少在您提交的代码中是这样。