QT串口程序


1.qextserialport-1.2win-alpha在Win7下Qt 5.3编译遇到的问题及解决方法


1)、 'class QString' has no member named 'toAscii'
toAscii 替换为 toLatin1


2)、 错误:undefined reference to `GUID_DEVINTERFACE_COMPORT'
#include <ntddser.h>
在文件qextserialenumerator.cpp中添加上面这个头文件引用语句


3)、错误:'PHYSICAL_ADDRESS' does not name a type
#undef PHYSICAL_ADDRESS
#define PHYSICAL_ADDRESS LARGE_INTEGER
添加上面两行到文件ntddser.h中


2.新建工程SerialPort,添加以下8个文件


qextserialbase.cpp

qextserialbase.h

qextserialenumerator.cpp

qextserialenumerator.h

qextserialport.cpp

qextserialport.h

win_qextserialport.cpp

win_qextserialport.h

如图所示


3.添加文件内容

/*********************************************************************

在mainwindow.h的相应位置添加头文件#include"qextserialport.h",添加私有成员对象声明QextSerialPort *myCom;,添加槽函数声明voidreadMyCom();,添加完后,如下图。

*******************************************************************/


4.在mainwindow.cpp的类的构造函数中添加如下语句。

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
 
    ui->setupUi(this);
    struct PortSettings myComSetting = {BAUD9600,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,500};
    //定义一个结构体,用来存放串口各个参数
    //***************************************************************************
    myCom = new QextSerialPort("com3",myComSetting,QextSerialPort::EventDriven);
    //********************************************************************************/
    //定义串口对象,并传递参数,在构造函数里对其进行初始化
    myCom ->open(QIODevice::ReadWrite);
    //以可读写方式打开串口
    connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
    //信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
 
}
 
void MainWindow::readMyCom()     //读串口函数
{
QByteArray temp = myCom->readAll();
//读取串口缓冲区的所有数据给临时变量temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}
 

5.添加按键,并添加按键单击函数处理,添加代码。

void MainWindow::on_sendButton_clicked()
{
    //以ASCII码形式将行编辑框中的数据写入串口
    myCom->write(ui->sendMsgLineEdit->text().toLatin1());
 
}












评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值