简体   繁体   English

连接到QT中的串行端口

[英]Connecting to Serial port in QT

I'd like to connect to a microcontroller using QSerialPort . 我想使用QSerialPort连接到微控制器。 I've added the line serial port to my .pro file, included QSerialPort in my source file and ran qmake . 我已经将线路串行端口添加到我的.pro文件中,在我的源文件中包含QSerialPort并运行了qmake My code is below: 我的代码如下:

    serial.setPortName("COM3");
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    serial.open(QIODevice::ReadWrite);
    serial.write("ok*");

When I run the code I get a message saying the device is not open though I've confirmed it's open with TeraTerm . 当我运行代码时,我收到一条消息,指出该设备未打开,尽管我已确认它已使用TeraTerm打开。 What am I missing? 我想念什么? The error message is below: 错误消息如下:

QIODevice::write: device not open

First of all, you should check is open returns true . 首先,您应该检查open是否返回true If no, then tell to user about error and call errorString() 如果否,则告诉用户错误并调用errorString()

if(serial.open(QIODevice::ReadWrite))
    serial.write("ok*");
else
{
    //error
    qDebug() << serial.errorString();
}

You try to open one port in different programs. 您尝试在不同的程序中打开一个端口。 It is forbidden in Windows. Windows禁止使用。 So you can't use this. 所以你不能使用它。 In your video author open com3 in Qt but com4 in teraterm , it is different ports, so you should use same thing, not one port for few programs. 在您的视频的作者打开com3Qt ,但com4teraterm ,它是不同的端口,因此你应该使用同样的事情,而不是一个端口几个节目。

Pay attention on this program: com0com 注意此程序: com0com

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

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