接上篇,我们的软件如何实现呢?
1、通信
通信口不局限于串口,如果有多种接口可以抽象。
以下为CPort头文件:
#ifndef CPORT_H
#define CPORT_H
#include <QObject>
#include <QSerialPort>
#include <QThread>
#include <QTimer>
class Message;//当然这个结构得自己定义并注册,这里使用了前置申明
class CPort : public QObject
{
Q_OBJECT
public:
explicit CPort(QObject *parent = 0);
~CPort();
signals:
//向上层传送消息
void signalUpwardMsg(const Message&);
private slots:
//上层传过来的消息发送到串口
void onDownwardMsg(const Message&);
//读串口数据
void onReadData();
//在这里定时的解析收到的数据
void onParse();
private:
QSerialPort *port;
QThread worker;
QTimer *timer;
QByteArray data;
};
#endif // CPORT_H
以下是CPort的源文件:
#include "CPort.h"
#include "message.h"
#include <QDebug>
CPort::CPort(QObject *parent) : QObject(parent)
{
port = new QSerialPort(this);
#ifdef __arm__
port->setPortName("/dev/ttymxc1");
#else
port->setPortName("COM2");
#endif
port->setBaudRate(QSerialPort::Baud115200);
port->setDataBits(QSerialPort::Data8);
port->setParity(QSerialPort::EvenParity);
port->setStopBits(QSerialPort::OneStop);
connect(port,SIGNAL(readyRead()),this,SLOT(onReadData()));
if(!port->open(QIODevice::ReadWrite))
{
qDebug()<<"Port Can't open";
}
timer = new QTimer(this);
timer->setInterval(10000);
connect(timer,SIGNAL(timeout()),SLOT(onParse()));
timer->start();
this->moveToThread(&worker);
worker.start(QThread::HighPriority);
}
CPort::~CPort()
{
}
void CPort::onDownwardMsg(const Message & msg)
{
QByteArray array(msg.content);
port->write(array);
}
void CPort::onReadData()
{
data.append(port->readAll());
}
void CPort::onParse()
{
for(int i = 0 ; i < data.size();i++)
{
//协议解析后得到 message
}
Message msg;
signalUpwardMsg(msg);
}
2、业务逻辑
业务逻辑就因项目而异了
#ifndef CMSGBUSINESS_H
#define CMSGBUSINESS_H
#include <QObject>
class Message;
class CMsgBusiness : public QObject
{
Q_OBJECT
public:
explicit CMsgBusiness(QObject *parent = 0);
~CMsgBusiness();
public:
//这里可以在UI里来直接调用
QString get()const;
signals:
//处理完的Msg发到界面上去显示
void signalSomethingComing(QString);
private slots:
//收到下层来的msg
void onCommingMsg(const Message&);
};
#endif // CMSGBUSINESS_H
#include "CMsgBusiness.h"
CMsgBusiness::CMsgBusiness(QObject *parent) : QObject(parent)
{
}
CMsgBusiness::~CMsgBusiness()
{
}
void CMsgBusiness::onCommingMsg(const Message &msg)
{
Q_UNUSED(msg)
//这里可以对收到的msg进行处理
signalSomethingComing(QString("sss"));
}
3 bridge
这个类是一个承上启下的使用类
#ifndef CBRIDGE_H
#define CBRIDGE_H
#include <QObject>
class CMsgBusiness;
class CPort;
class CBridge : public QObject
{
Q_OBJECT
public:
explicit CBridge(QObject *parent = 0);
~CBridge();
//这里可以得到所有的实例
CMsgBusiness *business()const;
CPort *port()const;
signals:
//这里可以转发信号
public slots:
private:
CMsgBusiness *pBusiness;
CPort *pPort;
};
#endif // CBRIDGE_H
#include "CBridge.h"
#include "CPort.h"
#include "CMsgBusiness.h"
CBridge::CBridge(QObject *parent) : QObject(parent)
{
pBusiness = new CMsgBusiness(this);
pPort = new CPort();//这里的实例化得注意了
//通信层与业务层的通信
connect(pPort,SIGNAL(signalUpwardMsg(Message))
,pBusiness,SLOT(onCommingMsg(Message)));
}
CBridge::~CBridge()
{
}
CMsgBusiness *CBridge::business() const
{
return pBusiness;
}
CPort *CBridge::port() const
{
return pPort;
}
主界面
#include "mainwindow.h"
#include "CBridge.h"
#include "CMsgBusiness.h"
MainWindow::MainWindow(QWidget *parent,CBridge* bridge)
: QMainWindow(parent)
,pb(bridge)
{
//这里完成业务层与UI层的通信
connect(pb->business(),SIGNAL(signalSomethingComing(QString))
,SLOT(onComing(QString)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::onComing(QString content)
{
Q_UNUSED(content)
//SHOW ON UI
}
客户端
#include "mainwindow.h"
#include <QApplication>
#include "CBridge.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w(0,new CBridge);
w.show();
return a.exec();
}
以上是我在项目使用的架构,其实架构没有最好的,只有最合适的。