嵌入式多功能电子相册之上位机

来源:互联网 发布:华为盒子看电影软件 编辑:程序博客网 时间:2024/06/02 10:18

  前几天做了基于S3C2451裸机开发的多功能相册,其中有个很重要的功能,就是上位机对下位机的控制。例如:开启相册、切换相册、同步校准时间和设置字幕等功能。之前用的是一个网上下载的上位机,虽说用起来挺顺手的,但是这毕竟不是自己写的东西,总感觉对这个项目没有完全吃透。难得想做一个嵌入式项目,我不想留有任何的遗憾,况且我自认为对QT也是有一点了解了,所以我这两天用QT做了一个上位机,虽说功能上和网上下载的差不多,但是我对QT串口上位机编程有了一点了解了。

  首先看一下上位机效果:


  在功能上和上一篇博客中的上位机图片差不多,只是将每一个命令代表的十六进制显示在了界面上了。

  上位机的编写用到了qextserialbase这个第三方库,主要就是对QSerialPort类的使用。

做这个上位机遇到的困难总结:

  串口的初始化、打开关闭其实都很简单,最主要的就是数据的处理,怎么把那些十六进制指令发送出去?

串口初始化:

//打开关闭串口void MainWindow::on_opencom_clicked(){    if(ui->opencom->text() == "打开串口")    {        ui->opencom->setText("关闭串口");        ui->com->setEnabled(false);        ui->brand->setEnabled(false);        serial = new QSerialPort;        serial->setPortName(ui->com->currentText());      //设置串口号        serial->open(QIODevice::ReadWrite);               //打开串口        serial->setBaudRate(ui->brand->currentText().toInt());    //设置波特率        serial->setDataBits(QSerialPort::Data8);          //设置数据位        serial->setParity(QSerialPort::NoParity);         //设置校验位        serial->setStopBits(QSerialPort::OneStop);        //设置停止位        serial->setFlowControl(QSerialPort::NoFlowControl);   //设置流控制     }    else if(ui->opencom->text() == "关闭串口")    {        ui->opencom->setText("打开串口");        serial->close();                                     //关闭串口        ui->com->setEnabled(true);        ui->brand->setEnabled(true);    }}


数据处理:

//上一张   FE 03 F1 04 00 EFvoid MainWindow::on_upone_clicked(){    str.clear();              // str为QString类型    str.append(QChar(0xFE));    str.append(QChar(0x03));    str.append(QChar(0xf1));    str.append(QChar(0x04));    str.append(QChar(0x00));    str.append(QChar(0xef));    serial->write(str.toLatin1());     //将QString转化为字节流}


详细代码:

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtSerialPort/QtSerialPort>#include <QtSerialPort/QSerialPortInfo>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private slots:    void on_opencom_clicked();    void on_upone_clicked();    void on_downone_clicked();    void on_openphoto_clicked();    void on_timechangebtn_clicked();    void on_settime_clicked();    void on_nowtime_clicked();    void on_openclosebtn_clicked();    void on_leftbtn_clicked();    void on_rightbtn_clicked();    void on_changesub_clicked();private:    Ui::MainWindow *ui;    QSerialPort *serial;    QString str;                //存放指令};#endif // MAINWINDOW_H

#include "mainwindow.h"#include "ui_mainwindow.h"#include <qdebug.h>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    setWindowIcon(QIcon(":/icon.bmp"));    ui->setupUi(this);    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())    {        QSerialPort serial;        serial.setPort(info);        if(serial.open(QIODevice::ReadWrite))        {            ui->com->addItem(serial.portName());            serial.close();        }    }    qDebug() << tr("界面设定成功!");}MainWindow::~MainWindow(){    delete ui;}//打开关闭串口void MainWindow::on_opencom_clicked(){    if(ui->opencom->text() == "打开串口")    {        ui->opencom->setText("关闭串口");        ui->com->setEnabled(false);        ui->brand->setEnabled(false);        serial = new QSerialPort;        serial->setPortName(ui->com->currentText());        serial->open(QIODevice::ReadWrite);        serial->setBaudRate(ui->brand->currentText().toInt());        serial->setDataBits(QSerialPort::Data8);        serial->setParity(QSerialPort::NoParity);        serial->setStopBits(QSerialPort::OneStop);        serial->setFlowControl(QSerialPort::NoFlowControl);    }    else if(ui->opencom->text() == "关闭串口")    {        ui->opencom->setText("打开串口");        serial->close();        ui->com->setEnabled(true);        ui->brand->setEnabled(true);    }}//上一张   FE 03 F1 04 00 EFvoid MainWindow::on_upone_clicked(){    str.clear();    str.append(QChar(0xFE));    str.append(QChar(0x03));    str.append(QChar(0xf1));    str.append(QChar(0x04));    str.append(QChar(0x00));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//下一张   FE 03 F1 04 01 EFvoid MainWindow::on_downone_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x03));    str.append(QChar(0xf1));    str.append(QChar(0x04));    str.append(QChar(0x01));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//开启相册  FE 03 F1 01 01 EFvoid MainWindow::on_openphoto_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x03));    str.append(QChar(0xf1));    str.append(QChar(0x01));    str.append(QChar(0x01));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//轮换时间配制    FE 03 F1 02 02 EFvoid MainWindow::on_timechangebtn_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x03));    str.append(QChar(0xf1));    str.append(QChar(0x02));    str.append(QChar(ui->timechange->text().toInt()));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//设置时钟  (2017-7-28 19:00:00)FE 07 F0 11 07 1C 13 00 00 EFvoid MainWindow::on_settime_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x07));    str.append(QChar(0xf0));    str.append(QChar(ui->year->text().toInt()));    str.append(QChar(ui->month->text().toInt()));    str.append(QChar(ui->day->text().toInt()));    str.append(QChar(ui->hour->text().toInt()));    str.append(QChar(ui->minute->text().toInt()));    str.append(QChar(ui->second->text().toInt()));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//同步标准时间void MainWindow::on_nowtime_clicked(){    QDateTime time = QDateTime::currentDateTime();    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x07));    str.append(QChar(0xf0));    str.append(QChar(time.toString("yy").toInt()));    str.append(QChar(time.toString("MM").toInt()));    str.append(QChar(time.toString("dd").toInt()));    str.append(QChar(time.toString("hh").toInt()));    str.append(QChar(time.toString("mm").toInt()));    str.append(QChar(time.toString("ss").toInt()));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//开启字幕FE 03 F2 01 01 EF//关闭字幕FE 03 F2 01 00 EFvoid MainWindow::on_openclosebtn_clicked(){    str.clear();    if(ui->openclosebtn->text() == "开启字幕")    {        str.append(QChar(0xfe));        str.append(QChar(0x03));        str.append(QChar(0xf2));        str.append(QChar(0x01));        str.append(QChar(0x01));        str.append(QChar(0xef));        ui->openclosebtn->setText("关闭字幕");    }    else if(ui->openclosebtn->text() == "关闭字幕")    {        str.append(QChar(0xfe));        str.append(QChar(0x03));        str.append(QChar(0xf2));        str.append(QChar(0x01));        str.append(QChar(0x00));        str.append(QChar(0xef));        ui->openclosebtn->setText("开启字幕");    }    serial->write(str.toLatin1());}//向左    FE 03 F2 02 00 EFvoid MainWindow::on_leftbtn_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x03));    str.append(QChar(0xf2));    str.append(QChar(0x02));    str.append(QChar(0x00));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//向右    FE 03 F2 02 01 EFvoid MainWindow::on_rightbtn_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x03));    str.append(QChar(0xf2));    str.append(QChar(0x02));    str.append(QChar(0x01));    str.append(QChar(0xef));    serial->write(str.toLatin1());}//修改字幕 hello    FE 07 F2 03 68 65 6C 6C 6F EFvoid MainWindow::on_changesub_clicked(){    str.clear();    str.append(QChar(0xfe));    str.append(QChar(0x07));    str.append(QChar(0xf2));    str.append(QChar(0x03));    str.append(ui->lineEdit->text());    str.append(QChar(0xef));    serial->write(str.toLatin1());}

原创粉丝点击