求助想实现在pc上运行一个qt程序用一个按钮点击实现将textedit中的数据写入到串口缓冲区中
#ifndef WIDGET_H
#define WIDGET_H
#include "posix_qextserialport.h"
#include<QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
Posix_QextSerialPort *myCom;
private slots:
// void on_ReceivePushButton_clicked();
void on_SendPushButton_clicked();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//定义一个结构体,用来存放串口各个参数
struct PortSettings myComSetting = {BAUD115200,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,50};
//定义串口对象,并传递参数,在构造函数里对其进行初始化
myCom = new Posix_QextSerialPort("com1",myComSetting,QextSerialBase::EventDriven);
//以可读写方式打开串口
myCom->open(QIODevice::ReadWrite);
//信号和槽函数关联,写数据到串口缓冲区
connect(myCom,SIGNAL(readyWrite()),this,SLOT(on_SendPushButton_clicked()));
connect(ui->SendPushButton, SIGNAL(clicked()), this, SLOT(Widget()));
// connect(ui->ReceivePushButton, SIGNAL(clicked()), this, SLOT(Widget()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_SendPushButton_clicked()
{
myCom->write(ui->SendtextEdit->toPlainText().toAscii());
}
我想用一个按钮按一下 就可以把textedit中的数据发送到串口缓冲区中去
我现在想问 我用了网上的第三方串口类,我想编写一个小的程序就一个textedit和一个按钮,点击按钮能将textedit中的数据发送到串口缓冲区中,这上面的是我自己加上去的代码,这样写行吗??下面这个是我写的读串口程序代码,也有一个按钮和一个textedit,点击按钮能够读取串口缓冲区的数据显示在textedit中,这样写行吗???
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->setupUi(this);
//定义一个结构体,用来存放串口各个参数
struct PortSettings myComSetting = {BAUD115200,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,50};
//定义串口对象,并传递参数,在构造函数里对其进行初始化
myCom = new Posix_QextSerialPort("com1",myComSetting,QextSerialBase::EventDriven);
//以可读写方式打开串口
myCom->open(QIODevice::ReadWrite);
//信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
connect(myCom,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(Widget()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
//读取串口缓冲区的所有数据给临时变量temp
QByteArray temp = myCom->readAll();
//将串口的数据显示在窗口的文本浏览器中
ui->textEdit->setPlainText(temp);
QFile file_1("output.txt");
if (!file_1.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file_1);
out <<ui->ReceivetextEdit->toPlainText()<<"\n";
file_1.close();
}
我就是想编写2个qt程序实现pc机与开发板通过串口通信 pc机上的程序通过串口发数据 开发板上的qt程序能够接收到