2,640
社区成员
发帖
与我相关
我的任务
分享
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//下面两句一定要放在构造函数才起作用,不明白为什么
struct PortSettings myComSetting = {BAUD9600,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,500};
//定义一个结构体,用来存放串口各个参数
myCom = new Win_QextSerialPort("com1",myComSetting,QextSerialBase::EventDriven);
//定义串口对象,并传递参数,在构造函数里对其进行初始化
connect(ui.OpenBtn, SIGNAL(clicked()), this, SLOT(on_OpenBtn_clicked()) );
connect(ui.CloseBtn, SIGNAL(clicked()), this, SLOT(on_CloseBtn_clicked()) );
connect(ui.SendBtn, SIGNAL(clicked()), this, SLOT(on_SendBtn_clicked()) );
}
void MainWindow::on_OpenBtn_clicked(){
myCom ->open(QIODevice::ReadWrite);
//以可读写方式打开串口
connect(myCom, SIGNAL(readyRead()), this, SLOT(readMyCom()));
}
void MainWindow::readMycom(){
QByteArray temp = myCom->readAll();
//读取串口缓冲区的所有数据给临时变量temp
ui.DisplayBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}