本节我们使用 Qt 来编写一个简单的上位机。
实验介绍:组装 ui 界面,使用 Qt 提供的串口类来实现串口收发功能,需要掌握的相关 Qt 知识有以下几点:
QSerialPort 是 Qt5 中的附加模块,提供了基本的功能,包括配置、I/O 操作、获取和设置 RS-232 引脚的信号,要链接 QtSerialPort 模块,需要在.pro 文件中添加+=serialport。
QSerialPort 封装了丰富的成员变量来对应串口属性,还有很多操作串口的成员函数,常用的成员函数有setPort()或 setPortName(),setBaudRate(),setDataBits(),setStopBits(),setParity()等,可以用这些函数设置要访问的串口设备。本实验使用了 readyRead()信号,当有数据到来时会触发类对象的 readyRead()信号,然后利用它的成员函数 readAll()读取。
类 QSerialPortInfo 可以获取可用的串口信息,如端口名称,系统位置,产品号,描述,制造商等信息。我们把它获取到的端口信息交给 QSerialPort 类对象。
77.1 界面布局
步骤一:将控件拖到 ui 界面上
接收框使用 Plain Text Edit,发送框使用 lineEdit,属性选择组件使用 Combo Box。

步骤二:属性设置栏布局,以串口号为例,依次水平布局属性选择位。

然后全部选中属性选择框,点垂直布局

效果如图 :

步骤三:功能栏布局,在按钮间添加弹簧,点击水平布局。

选中 Lbel,发送框和功能按钮,点击垂直布局

如图 :

选中属性栏和右侧组件,然后点击水平布局,如图:

完成后:

再仿照上边的方法将下方的功能部分和接收框垂直布局:

添加完组件后,更改接收框为只读:点击接收框,在 QTextEdit 里标记 readOnly。
在右上角更改 ui 界面对象名,界面组装完成后可以根据需要自行修改,
双击属性选择框添加属性:

如图:


哔哩哔哩搜索学习更多内容
|