用qt寫的一個簡單到不能在簡單的上位機

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   os   

學QT時,寫的一個簡單得不能再簡單的串口上位機,用來控制單片機上的2個LED。假設一個是只有開和關的狀態。一個可以調節亮度。上位機的介面如:

其中,傳輸速率,資料位元,停止位下拉值在設計師裡面添加。剩下的功能,基本由代碼實現。通訊使用的協議也是隨便寫的。很簡單和隨意。圖片是老弟手繪的。

下面貼代碼 (*^__^*) 

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtSerialPort>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    Pic = tr("://Resource/關燈.jpg"); //放置一張圖片    ui->lightLabel->setPixmap(Pic);    ui->RTextEdit->setReadOnly(false);    ui->STextEdit->setReadOnly(false);    ui->onButton->setEnabled(true); //預設關燈    ui->offButton->setEnabled(false);    ui->lightDial->setEnabled(false);    serial = new QSerialPort(this);    OpenFlag = false;   //串口未開啟    connect(ui->lightDial, SIGNAL(valueChanged(int)), this, SLOT(on_light_chance()));    UsartUi();}MainWindow::~MainWindow(){    delete ui;}void MainWindow::UsartUi()  //完善串口配置UI{    ui->parityBox->addItem(QStringLiteral("None"),QSerialPort::NoParity);    ui->parityBox->addItem(QStringLiteral("偶校正"),QSerialPort::EvenParity);    ui->parityBox->addItem(QStringLiteral("奇數同位"),QSerialPort::OddParity);    ui->parityBox->addItem(QStringLiteral("Mark  1"),QSerialPort::MarkParity);    ui->parityBox->addItem(QStringLiteral("Space 0"),QSerialPort::SpaceParity);    ui->baundRateBox->setItemData(0,QSerialPort::Baud4800);    ui->baundRateBox->setItemData(1,QSerialPort::Baud9600);    ui->baundRateBox->setItemData(2,QSerialPort::Baud19200);    ui->baundRateBox->setItemData(3,QSerialPort::Baud38400);    ui->baundRateBox->setItemData(4,QSerialPort::Baud57600);    ui->baundRateBox->setItemData(5,QSerialPort::Baud115200);    ui->dataBitBox->setItemData(0,QSerialPort::Data5);    ui->dataBitBox->setItemData(1,QSerialPort::Data6);    ui->dataBitBox->setItemData(2,QSerialPort::Data7);    ui->dataBitBox->setItemData(3,QSerialPort::Data8);    ui->stopBitBox->setItemData(0,QSerialPort::OneStop);    ui->stopBitBox->setItemData(1,QSerialPort::OneAndHalfStop);    ui->stopBitBox->setItemData(2,QSerialPort::TwoStop);    PortInfo();}void MainWindow::PortInfo(){    ui->uNameBox->clear();    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {        QStringList list;        list << info.portName()             << info.description()             << info.manufacturer()             << info.systemLocation()             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());        ui->uNameBox->addItem(list.first(), list);    }}//槽函數void MainWindow::on_startButton_clicked()   //開啟串口{    if(!OpenFlag)//執行開啟串口    {        currentConfig.name = ui->uNameBox->currentText();        currentConfig.baudRate = static_cast<QSerialPort::BaudRate>               (ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toInt());        currentConfig.dataBits = static_cast<QSerialPort::DataBits>               (ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toInt());        currentConfig.stopBits = static_cast<QSerialPort::StopBits>               (ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toInt());        currentConfig.parity = static_cast<QSerialPort::Parity>               (ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());        serial->setPortName(currentConfig.name);        if(serial->open(QIODevice::ReadWrite))        {            if( serial->setBaudRate(currentConfig.baudRate)                && serial->setDataBits(currentConfig.dataBits)                && serial->setStopBits(currentConfig.stopBits)                && serial->setParity(currentConfig.parity) )            {                OpenFlag = true;                ui->startButton->setText(tr("關閉串口"));            }            else            {                serial->close();                OpenFlag = false;                ui->startButton->setText(tr("開啟串口"));            }        }        else        {            OpenFlag = false;            ui->startButton->setText(tr("開啟串口"));        }    }    else    {        OpenFlag = false;        ui->startButton->setText(tr("開啟串口"));        serial->close();    }/*    ui->STextEdit->setText(tr("校正位:%1 \n傳輸速率:%2 \n資料位元:%3 \n停止位:%4")                             .arg(ui->parityBox->itemData(ui->parityBox->currentIndex()).toString())                             .arg(ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toString())                             .arg(ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toString())                             .arg(ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toString()));*/}void MainWindow::on_offButton_clicked() //關燈{    QByteArray data;    QString Pdata;    data.resize(3);    data[0] = 0xF0;    data[1] = 0X0F;    data[2] = 0x80;    ui->offButton->setEnabled(false);    ui->onButton->setEnabled(true);    ui->lightDial->setValue(1);    ui->lightDial->setEnabled(false);    Pdata = data.toHex().toUpper();    int Size = Pdata.size();    for(int i = Size; i > 0; i--)    {        Pdata.insert(i * 2, " ");    }    ui->STextEdit->insertPlainText(Pdata);    //發送資料    serial->write(data);}void MainWindow::on_onButton_clicked()  //開燈  0XF0 0X0F 0X81{    QByteArray data;    QString Pdata;    data.resize(3);    data[0] = 0xF0;    data[1] = 0X0F;    data[2] = 0X81;    ui->onButton->setEnabled(false);    ui->offButton->setEnabled(true);    ui->lightDial->setEnabled(true);    Pdata = data.toHex().toUpper();    int Size = Pdata.size() / 2;    for(int i = Size; i > 0; i--)    {        Pdata.insert(i * 2, " ");    }    ui->STextEdit->insertPlainText(Pdata);    //發送資料    serial->write(data);}void MainWindow::on_light_chance(){    QByteArray data;    QString Pdata;    data.resize(5);    data[0] = 0xF0;    data[1] = 0x0F;    data[2] = 0xC0;    data[3] = 0x01;    data[4] = ui->lightDial->value();    Pdata = data.toHex().toUpper();    int Size = Pdata.size() / 2;    for(int i = Size; i > 0; i--)    {        Pdata.insert(i * 2, " ");    }    ui->STextEdit->insertPlainText(Pdata);    serial->write(data);}void MainWindow::on_freshButton_clicked()   //重新整理狀態{    QByteArray Rdata = serial->readAll();    QString Pdata = Rdata.toHex().toUpper();    //轉換成HEX格式且大寫    int Size = Pdata.size()/2;    for(int i = Size; i > 0; i--)   //插入空格 使格式如 xx xx xx    {        Pdata.insert(i * 2, " ");    }    ui->RTextEdit->insertPlainText(Pdata);    int i = 0;    if(Rdata.at(i) && 0xF0 == 0xF0)    {        i++;        if(Rdata.at(i) && 0x0F == 0x0F)        {            while(Rdata.at(i) != ‘\0‘) //解析指令            {                if(Rdata.at(i) && 0x80 == 0x80)currentStatus.lightOnOff = false;                if(Rdata.at(i) && 0x81 == 0x81)currentStatus.lightOnOff = true;                if(Rdata.at(i) && 0xc0 == 0xc0)                {                    i = i + 2;                    currentStatus.lightValue = Rdata.at(i);                }                i++;            }        }    }    if(currentStatus.lightOnOff)Pic = tr("://Resource/開燈.jpg");    else Pic = tr("://Resource/關燈.jpg");    ui->lightLabel->setPixmap(Pic);    ui->lightLcdNumber->display(currentStatus.lightValue);}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.