简体   繁体   English

虚拟 COM 端口 STM32 和 Qt 串行端口

[英]Virtual COM Port STM32 and Qt Serial Port

My aim is to enable communication via USB CDC HS on STM32 with Ubuntu 20.04 based PC in Qt app created in QtCreator.我的目标是通过 STM32 上的 USB CDC HS 与在 QtCreator 中创建的 Qt 应用程序中的基于 Ubuntu 20.04 的 PC 进行通信。

So far I've managed to run communication via UART and everything is working fine.到目前为止,我已经设法通过 UART 运行通信,并且一切正常。 Then I decided to switch to USB and I still can read incoming data (but only in CuteCom) and in my Qt app nothing appears.然后我决定切换到 USB 并且我仍然可以读取传入的数据(但只能在 CuteCom 中)并且在我的 Qt 应用程序中没有任何显示。

To be honest I have no idea what is going on and where to look for mistakes.老实说,我不知道发生了什么以及在哪里寻找错误。 Here I put the code:我把代码放在这里:

    void MainWindow::on_pushButtonConnect_clicked()
{
 if (ui->comboBoxDevices->count() == 0){
     this->addToLogs("No devices found.");
             return;
 }
 QString portName = ui->comboBoxDevices->currentText().split(" ").first();
 this->device->setPortName(portName);
 this->device->setBaudRate(QSerialPort::Baud115200);
 this->device->setDataBits(QSerialPort::Data8);
 this->device->setParity(QSerialPort::NoParity);
 this->device->setStopBits(QSerialPort::OneStop);
 this->device->setFlowControl(QSerialPort::NoFlowControl);

 if(device->open(QIODevice::ReadWrite)){
     this->addToLogs("Port opened. Setting the connection params...");
     this->addToLogs("UART enabled.");
     qDebug() << "Writing down the parameters...";
     qDebug() << "Baud rate:" << this->device->baudRate();
     qDebug() << "Data bits:" << this->device->dataBits();
     qDebug() << "Stop bits:" << this->device->stopBits();
     qDebug() << "Parity:" << this->device->parity();
     qDebug() << "Flow control:" << this->device->flowControl();
     qDebug() << "Read buffer size:" << this->device->readBufferSize();
     qDebug() << "Read buffer size:" << this->device->portName();
     connect(this->device, SIGNAL(readyRead()), this, SLOT(readFromPort()));
 } else {
     this->addToLogs("The port can not be opened.");
 }

And the readFromPort() function:和 readFromPort() 函数:

void MainWindow::readFromPort()
{
    while(this->device->canReadLine()){
        QString line = this->device->readLine();
        qDebug() << line;
        QString terminator = "\r";
        int pos = line.lastIndexOf(terminator);
        qDebug()<<line.left(pos);
        this->addToLogs(line.left(pos));
    }
}

Do you have any idea what might be wrong or not set properly?您知道什么可能是错误的或设置不正确吗? Would be thankful for all help.将不胜感激所有的帮助。

As it seems, in my code I put commands to read the port in if (with function canReadLine() ).看起来,在我的代码中,我在if (使用函数canReadLine() )中放置了读取端口的命令。 When I commented the whole condition out leaving just the reading, everything worked fine.当我评论整个条件而只留下阅读时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM