6.在 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("com1",myComSetting,QextSerialPort::EventDriven);
//********************************************************************************/
//定义串口对象,并传递参数,在构造函数里对其进行初始化
myCom ->open(QIODevice::ReadWrite);
//以可读写方式打开串口
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
//信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
}
在下面添加 readMyCom()函数的定义,添加如下代码。
void MainWindow::readMyCom() //读串口函数
{
QByteArray temp = myCom->readAll();
//读取串口缓冲区的所有数据给临时变量 temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}
评论1
最新资源