• 串口发送数据从TextEdit的编辑框中,读取16进值发送至串口。


    // 发送信息
    void MainWindow::on_SendMessageButton_clicked()
    {
        QString sdata= ui->SendDatatextEdit->toPlainText();//读取文本数据
    
    
        if(sdata.isEmpty())return;
        QByteArray sendBuf = "";
    
    
        sendBuf = QByteArray::fromHex(sdata.toLatin1().data());
        serial->write(sendBuf);
    }

    就是这样子,用LineEdit替换不行,它只能发送字符。


    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
        ui->FindAvailablePorts->setEnabled(1);
        ui->OpenSerialPortButton->setEnabled(0);
        ui->CloseSerialPortButton->setEnabled(0);
        ui->SendMessageButton->setEnabled(0);
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    // Test
    void MainWindow::on_pushButton_clicked()
    {
           alarm* open_alarm = new alarm;
           open_alarm->show();
    
    
           DriveTest* open_drivetest = new DriveTest;
           open_drivetest->show();
    
    
           Easy_model* open_easymodel = new Easy_model;
           open_easymodel->show();
    
    
           login* open_login = new login;
           open_login->show();
    
    
           parameter* open_parameter = new parameter;
           open_parameter->show();
    
    
           status* open_staus = new status;
           open_staus->show();
    
    
           StepData* open_stepdata = new StepData;
           open_stepdata->show();
    
    
           teaching* open_teaching = new teaching;
           open_teaching->show();
           //*****************************************
    }
    
    
    // 查找可用串口
    void MainWindow::on_FindAvailablePorts_clicked()
    {
        ui->PortBox->clear();//清掉上一次的,避免重复。
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        {
            QSerialPort serial;
            serial.setPort(info);
            if(serial.open(QIODevice::ReadWrite))
            {
                ui->PortBox->addItem(serial.portName());
                serial.close();
            }
        }
        ui->OpenSerialPortButton->setEnabled(1);
    }
    // 打开串口
    void MainWindow::on_OpenSerialPortButton_clicked()
    {
        serial = new QSerialPort;
    
    
        serial->setPortName(ui->PortBox->currentText());
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
    
    
        connect(serial,SIGNAL(readyRead()),this,SLOT(ReadMySerialPortMessage()));
        QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadMySerialPortMessage);
    
    
        ui->OpenSerialPortButton->setEnabled(0);
        ui->CloseSerialPortButton->setEnabled(1);
        ui->FindAvailablePorts->setEnabled(0);
        ui->SendMessageButton->setEnabled(1);
    
    
    }
    
    
    // 接收信息 并 显示
    void MainWindow::ReadMySerialPortMessage()
    {
        QByteArray buf;
        buf = serial->readAll();
        if(!buf.isEmpty())
        {
            /*字符显示*/
            /*
             QString str = ui->textEdit->toPlainText();
             str+=tr(buf);
             ui->textEdit->clear();
             ui->textEdit->append(str);
            */
            /*十六进制显示*/
             ui->textEdit->insertPlainText(buf.toHex());
             ui->textEdit->insertPlainText("
    ");
             ui->textEdit->moveCursor(QTextCursor::End);
        }
        buf.clear();
    }
    
    
    // 关闭串口
    void MainWindow::on_CloseSerialPortButton_clicked()
    {
        serial->close();
        ui->OpenSerialPortButton->setEnabled(1);
        ui->CloseSerialPortButton->setEnabled(0);
        ui->FindAvailablePorts->setEnabled(1);
        ui->SendMessageButton->setEnabled(0);
    }
    
    
    // 发送信息
    void MainWindow::on_SendMessageButton_clicked()
    {
        QString sdata= ui->SendDatatextEdit->toPlainText();//读取文本数据
    
    
        if(sdata.isEmpty())return;
        QByteArray sendBuf = "";
    
    
        sendBuf = QByteArray::fromHex(sdata.toLatin1().data());
        serial->write(sendBuf);
    }



    Mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    
    #include <QMainWindow>
    #include "alarm/alarm.h"
    #include "drivetest/drivetest.h"
    #include "easymodel/easy_model.h"
    #include "login/login.h"
    #include "parameter/parameter.h"
    #include "status/status.h"
    #include "stepdata/stepdata.h"
    #include "teaching/teaching.h"
    
    
    #include "QSerialPort"
    #include "QSerialPortInfo"
    
    
    #include "QDebug"
    #include "QString"
    #include "QArrayData"
    
    
    namespace Ui {
    class MainWindow;
    }
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
        QSerialPort *serial; //全局变量
        void ReadMySerialPortMessage();
    private slots:
        void on_pushButton_clicked();
    
    
        void on_FindAvailablePorts_clicked(); //查找串口
        void on_OpenSerialPortButton_clicked();//打开串口
        void on_CloseSerialPortButton_clicked();//关闭串口
        void on_SendMessageButton_clicked();//发送按钮
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    
    #endif // MAINWINDOW_H



    .Pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-05-21T11:00:56
    #
    #-------------------------------------------------
    
    
    QT       += core gui
    
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    
    TARGET = ACTController
    TEMPLATE = app
    
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    
    
    SOURCES += main.cpp
            mainwindow.cpp 
        alarm/alarm.cpp 
        drivetest/drivetest.cpp 
        easymodel/easy_model.cpp 
        login/login.cpp 
        parameter/parameter.cpp 
        status/status.cpp 
        stepdata/stepdata.cpp 
        teaching/teaching.cpp
    
    
    HEADERS  += mainwindow.h 
        alarm/alarm.h 
        drivetest/drivetest.h 
        easymodel/easy_model.h 
        login/login.h 
        parameter/parameter.h 
        status/status.h 
        stepdata/stepdata.h 
        teaching/teaching.h
    
    
    FORMS    += mainwindow.ui 
        alarm/alarm.ui 
        drivetest/drivetest.ui 
        easymodel/easy_model.ui 
        login/login.ui 
        parameter/parameter.ui 
        status/status.ui 
        stepdata/stepdata.ui 
        teaching/teaching.ui
    QT += serialport #串口控件
    
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    准备用协程模拟经典的生产者消费者
    重载操作符号
    对象池
    ssh加key
    oracle存储过程(PL/SQL)
    获取服务器ip地址
    【STL系列】结构体排序
    strtol sort snprintf snscanf strtok
    cxGrid动态创建带CheckBox列时遇到的问题...
    Windows2003 安装PostgreSQL9.0 UUID解决‘ERROR:无法载入程式库’问题
  • 原文地址:https://www.cnblogs.com/jiuqiang/p/10907192.html
Copyright © 2020-2023  润新知