This commit is contained in:
curtis
2024-11-25 17:15:44 +08:00
commit 32e5360a91
3749 changed files with 1432905 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
#include "ccommunicateapi.h"
#include "readconfig.h"
#include "ccommunicationinterface.h"
#include "cserialportinterface.h"
#include "cudpinterface.h"
#include "ctcpsocketinterface.h"
#include <QDebug>
CCommunicateAPI *CCommunicateAPI::m_API = NULL;
CCommunicateAPI::CCommunicateAPI(QObject *parent) :
QObject(parent),
m_interface(NULL)
{
init();
}
CCommunicateAPI::~CCommunicateAPI()
{
if(m_interface)
{
delete m_interface;
m_interface = NULL;
}
}
CCommunicateAPI *CCommunicateAPI::getInstance()
{
if(!m_API)
{
m_API = new CCommunicateAPI();
}
return m_API;
}
void CCommunicateAPI::init()
{
commuType = ReadConfig::getInstance()->getCommunicateType();
//根据通信方式生成不同的对象
if(0 == commuType) //串口
{
m_interface = new CSerialportInterface();
}
else if(1 == commuType) //udp
{
m_interface = new CUdpInterface();
}
else if(2 == commuType) //tcp
{
m_interface = new CTcpSocketInterface();
}
else if(3 == commuType) //can
{
//待添加
}
if(m_interface)
{
connect(m_interface,SIGNAL(signalReadyRead(QByteArray)),this,SIGNAL(signalReadyRead(QByteArray)));
connect(m_interface,SIGNAL(signalDisplayError(QString)),this,SIGNAL(signalCommunicateChanged(QString)));
}
}
void CCommunicateAPI::sendData(QByteArray sendArray)
{
char length = sendArray.size();
sendArray.prepend(length);
sendArray.prepend(PACKHEAD);
//添加校验
char sum = 0;
sendArray.append(sum);
sendArray.append(PACKTAIL);
m_interface->sendDataInterface(sendArray);
// qDebug() << "数据:" <<sendArray;
}
//在send_CMD-5个字节序号、发送ID、接收ID、CMD
void CCommunicateAPI::sendData(uint16_t cmdID,QByteArray arrayData)
{
QByteArray fixedArray;
fixedArray.resize(5);
fixedArray[0] = arrayData.length();
memcpy(fixedArray.data()+3,&cmdID,sizeof(uint8_t));
fixedArray.append(arrayData);
sendData(fixedArray);
}
//故障复位
void CCommunicateAPI::resetFault()
{
QByteArray sendArray;
sendArray.resize(4);
sendArray[0] = 0;
sendArray[1] = 0;
sendArray[2] = 1;
sendArray[3] = 0x06;
sendData(sendArray);
}
//发送心跳
void CCommunicateAPI::sendHeartBeat()
{
QByteArray array(4,0);
array[0] = SEND_HEARTBEAT_CMD; //指令
sendData(array);
}
void CCommunicateAPI::sendBicycleParam(const ST_BicycleParam &st_bicycleParam)
{
QByteArray array(13,0);
array[0] = BICYCLE_PARAM_CMD;
memcpy(array.data() + 1,&st_bicycleParam,sizeof(ST_BicycleParam));
sendData(array);
}
void CCommunicateAPI::sendRealTimeParam(E_REALTIMECMD sbuCmd,quint8 value)
{
QByteArray array(4,0);
array[0] = REALTIME_PARAM_CMD;
array[1] = sbuCmd;
array[2] = value;
sendData(array);
}
void CCommunicateAPI::SetquitCmd()
{
// uint16_t cmdId = QUIT_CMD;
// QByteArray array;
// array.resize(0);
// CCommunicateAPI::getInstance()->sendData(cmdId,array);
}
void CCommunicateAPI::setConfigParam()
{
m_interface->setConfigParam();
}

View File

@@ -0,0 +1,59 @@
#ifndef CCOMMUNICATEAPI_H
#define CCOMMUNICATEAPI_H
#include <QObject>
#include "dataformate.h"
class CCommunicationInterface;
class CCommunicateAPI : public QObject
{
Q_OBJECT
public:
static CCommunicateAPI *getInstance();
void sendData(QByteArray);
/************************************
*说明:发送数据函数【重载函数】
*参数:
*@uint8_t seq包的序号,默认为0当发送的数据需要拆包时才会使用
*@uint8_t id 自身设备ID号
*@uint8_t cmdID协议ID号
*@QByteArray arrayData有效数据
*返回值:无
***********************************/
void sendData(uint16_t cmdID,QByteArray arrayData);
//故障复位
void resetFault();
//心跳开关
void sendHeartBeat();
/****测试函数****/
//启动前参数
void sendBicycleParam(const ST_BicycleParam&);
//实时调节参数
void sendRealTimeParam(E_REALTIMECMD,quint8 value);
//退出当前状态
void SetquitCmd();
void setConfigParam();
signals:
//接收到实际数据
void signalReadyRead(QByteArray);
void signalCommunicateChanged(QString);
private:
explicit CCommunicateAPI(QObject *parent = nullptr);
~CCommunicateAPI();
void init();
private:
static CCommunicateAPI *m_API;
int commuType;
CCommunicationInterface *m_interface;
};
#endif // CCOMMUNICATEAPI_H

View File

@@ -0,0 +1,12 @@
#include "ccommunicationinterface.h"
CCommunicationInterface::CCommunicationInterface()
{
}
CCommunicationInterface::~CCommunicationInterface()
{
}
//解析数据
void CCommunicationInterface::analysisProtocal(QByteArray)
{}

View File

@@ -0,0 +1,31 @@
#ifndef CCOMMUNICATIONINTERFACE_H
#define CCOMMUNICATIONINTERFACE_H
#include <QObject>
class CCommunicationInterface : public QObject
{
Q_OBJECT
public:
CCommunicationInterface();
~CCommunicationInterface();
//配置参数
virtual bool setConfigParam() = 0;
//发送数据接口
virtual void sendDataInterface(QByteArray) = 0;
//解析数据
virtual void analysisProtocal(QByteArray);
signals:
//接收到实际数据
void signalReadyRead(QByteArray);
//通信对象发生错误
void signalDisplayError(QString);
//通信状态发生变化
void signalStateChanged(int);
public slots:
//接收数据接口
};
#endif // CCOMMUNICATIONINTERFACE_H

View File

@@ -0,0 +1,276 @@
#include "cserialportinterface.h"
#include <QApplication>
#include "readconfig.h"
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>
#include <QDateTime>
#include "languagemanager.h"
//#pragma execution_character_set("utf-8")
CSerialportInterface::CSerialportInterface():m_serialPort(NULL)
{
receiveArray.clear();
if(!m_serialPort)
{
m_serialPort = new QSerialPort();
connect(m_serialPort,&QSerialPort::readyRead,this,&CSerialportInterface::receiveDataInterface);
connect(m_serialPort,&QSerialPort::errorOccurred,this,&CSerialportInterface::displayError);
setConfigParam();
}
m_blockTimer = new QTimer();
m_blockTimer->setInterval(1000);
//connect(m_blockTimer,SIGNAL(timeout()),this,SLOT(slotHandleTimeout()));
connect(m_blockTimer,&QTimer::timeout,this,[=]
{
qDebug()<<"Hello"; //串口都没有哪来的jiekou
if(!m_serialPort->isOpen())
{
setConfigParamSerialError();
qDebug()<<"串口重新打开失败";
}
else
{
qDebug()<<"串口重新打开成功";
m_blockTimer->stop();
displayError(QSerialPort::NoError);
}
});
}
CSerialportInterface::~CSerialportInterface()
{
if(m_serialPort)
{
delete m_serialPort;
m_serialPort = NULL;
}
}
//配置参数
bool CSerialportInterface::setConfigParam()
{
m_serialPort->close();
ST_SerialPortConfig st_SerialConfig;
if(!ReadConfig::getInstance()->getSerialPortConfig(st_SerialConfig))
{
QMessageBox::warning(NULL,tr("警告"),tr("获取串口配置失败"),QMessageBox::Retry);
return false;
}
QSerialPortInfo m_SerialPortInfo;
QStringList serialPortNames;
#if 0
foreach(m_SerialPortInfo,QSerialPortInfo::availablePorts())
{
QSerialPort serialport;
serialport.setPort(m_SerialPortInfo);
if(serialport.open(QIODevice::ReadWrite))
{
serialPortNames.append(m_SerialPortInfo.portName());
serialport.close();
}
}
if(serialPortNames.isEmpty())
{
QMessageBox::warning(NULL,tr("警告"),tr("无可用串口"),QMessageBox::Retry);
return false;
}
#endif
if(m_serialPort)
{
m_serialPort->setPortName(st_SerialConfig.portName);
m_serialPort->setReadBufferSize(200);
if(m_serialPort->open(QIODevice::ReadWrite))
{
m_serialPort->setBaudRate(st_SerialConfig.baud);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
}
else
{
E_LANGUAGE language = LanguageManager::getInstance()->getCurrentLanguage();
if(language == Chinese_E)
QMessageBox::warning(NULL,tr("警告"),tr("串口打开失败"),QMessageBox::Retry);
else //if(language == English_E)
QMessageBox::warning(NULL,tr("Warning"),tr("Failed to open the serial port"),QMessageBox::Retry);
qDebug() <<"current language index:"<< language;
//QMessageBox::warning(NULL,tr("警告"),tr("串口打开失败"),QMessageBox::Retry);
return false;
}
}
return true;
}
//发送数据接口
void CSerialportInterface::sendDataInterface(QByteArray sendArray)
{
if(m_serialPort)
{
if(m_serialPort->isOpen())
{
m_serialPort->write(sendArray);
// qDebug()<<"send"<<sendArray.toHex();
//qDebug() <<"发送时间:"<< QDateTime::currentDateTime();
}
else
{
// qDebug()<<tr("串口已关闭");
}
}
}
//接收数据接口
void CSerialportInterface::receiveDataInterface()
{
QByteArray buf;
buf = m_serialPort->readAll();
receiveArray.append(buf);
while(!receiveArray.isEmpty())
{
if(receiveArray[0] != (char)SLAVEPACKHEAD)
{
receiveArray.remove(0,1);
}
else
{
//获取有效数据长度
uint8_t datalen = 0;
memcpy(&datalen,receiveArray.constData()+1,sizeof(uint8_t));
if(receiveArray.length() >= datalen + 4)
{
//校验成功
if((uint8_t)receiveArray[datalen+3] == SLAVEPACKTAIL)
{
emit signalReadyRead(receiveArray.mid(0,datalen + 4));
// qDebug()<<receiveArray.toHex();
receiveArray.remove(0,datalen + 4);
}
else //校验失败
{
//方式1 丢弃本包
receiveArray.remove(0,datalen + 4);
}
}
else //数据不够,直接退出继续接收
break;
}
}
}
void CSerialportInterface::displayError(QSerialPort::SerialPortError error)
{
QString lastError("");
switch(error)
{
case QSerialPort::NoError:
lastError = "";
break;
case QSerialPort::DeviceNotFoundError:
lastError = "DeviceNotFoundError";
break;
case QSerialPort::PermissionError:
lastError = "PermissionError";
break;
case QSerialPort::OpenError:
lastError = "OpenError";
break;
case QSerialPort::NotOpenError:
lastError = "NotOpenError";
break;
case QSerialPort::WriteError:
//lastError = "WriteError";
handleSerialError(); //串口被拔了,重新检测串口
break;
case QSerialPort::ReadError:
lastError = "ReadError";
break;
case QSerialPort::UnknownError:
lastError = "UnknownError";
break;
default:
break;
}
emit signalDisplayError(lastError);
}
void CSerialportInterface::handleSerialError()
{
m_blockTimer->start();
qDebug() <<"定时器开始运行";
setConfigParam();
}
//解析数据
void CSerialportInterface::analysisProtocal(QByteArray)
{
}
//配置参数
bool CSerialportInterface::setConfigParamSerialError()
{
m_serialPort->close();
ST_SerialPortConfig st_SerialConfig;
if(!ReadConfig::getInstance()->getSerialPortConfig(st_SerialConfig))
{
QMessageBox::warning(NULL,tr("警告"),tr("获取串口配置失败"),QMessageBox::Retry);
return false;
}
QSerialPortInfo m_SerialPortInfo;
QStringList serialPortNames;
#if 0
foreach(m_SerialPortInfo,QSerialPortInfo::availablePorts())
{
QSerialPort serialport;
serialport.setPort(m_SerialPortInfo);
if(serialport.open(QIODevice::ReadWrite))
{
serialPortNames.append(m_SerialPortInfo.portName());
serialport.close();
}
}
if(serialPortNames.isEmpty())
{
QMessageBox::warning(NULL,tr("警告"),tr("无可用串口"),QMessageBox::Retry);
return false;
}
#endif
if(m_serialPort)
{
m_serialPort->setPortName(st_SerialConfig.portName);
m_serialPort->setReadBufferSize(200);
if(m_serialPort->open(QIODevice::ReadWrite))
{
m_serialPort->setBaudRate(st_SerialConfig.baud);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
}
/*else
{
QMessageBox::warning(NULL,tr("警告"),tr("串口打开失败"),QMessageBox::Retry);
return false;
}*/
}
return true;
}

View File

@@ -0,0 +1,36 @@
#ifndef CSERIALPORTINTERFACE_H
#define CSERIALPORTINTERFACE_H
#include "ccommunicationinterface.h"
#include <QObject>
#include <QSerialPort>
#include <QTimer>
class CSerialportInterface : public CCommunicationInterface
{
public:
CSerialportInterface();
~CSerialportInterface();
//配置参数
virtual bool setConfigParam();
//发送数据接口
virtual void sendDataInterface(QByteArray);
//解析数据
virtual void analysisProtocal(QByteArray);
public slots:
//接收数据接口
void receiveDataInterface();
//通信状态变化
void deviceStateChanged(int);
//处理串口断开
void handleSerialError();
//QSerialPort::SerialPortError
//设备错误
void displayError(QSerialPort::SerialPortError error);
bool setConfigParamSerialError();
private:
QSerialPort *m_serialPort;
QByteArray receiveArray;
QTimer *m_blockTimer;
};
#endif // CSERIALPORTINTERFACE_H

View File

@@ -0,0 +1,86 @@
#include "ctcpsocketinterface.h"
#include <QTcpSocket>
#include <QApplication>
#include <QTcpServer>
#include <QAbstractSocket>
CTcpSocketInterface::CTcpSocketInterface(int type):
m_tcpSocket(NULL),
m_tcpServer(NULL)
{
if(0 == type) //客户端
{
m_tcpSocket = new QTcpSocket();
connect(m_tcpSocket,&QTcpSocket::readyRead,this,&CTcpSocketInterface::receiveDataInterface);
}
else if(1 == type) //服务端
{
m_tcpServer = new QTcpServer();
connect(m_tcpServer, &QTcpServer::newConnection, this, &CTcpSocketInterface::newTcpConnection);
connect(m_tcpServer, &QTcpServer::acceptError, this, &CTcpSocketInterface::displayError);
}
}
//新的连接
void CTcpSocketInterface::newTcpConnection()
{
m_tcpSocket = m_tcpServer->nextPendingConnection();
if(m_tcpSocket)
{
connect(m_tcpServer, &QTcpServer::newConnection, this, &CTcpSocketInterface::newTcpConnection);
connect(m_tcpServer, &QTcpServer::acceptError, this, &CTcpSocketInterface::displayError);
}
}
//错误输出
void CTcpSocketInterface::displayError(QAbstractSocket::SocketError socketError)
{
QString lastError("");
switch(socketError)
{
case QAbstractSocket::ConnectionRefusedError:
lastError = "ConnectionRefusedError";
break;
case QAbstractSocket::RemoteHostClosedError:
lastError = "RemoteHostClosedError";
break;
case QAbstractSocket::HostNotFoundError:
lastError = "HostNotFoundError";
break;
case QAbstractSocket::SocketAccessError:
lastError = "SocketAccessError";
break;
case QAbstractSocket::UnknownSocketError:
lastError = "UnknownSocketError";
break;
default:
break;
}
emit signalDisplayError(lastError);
}
//配置参数
bool CTcpSocketInterface::setConfigParam()
{
//读取xml配置文件
QString path = QApplication::applicationDirPath();
//绑定端口
return true;
}
//发送数据接口
void CTcpSocketInterface::sendDataInterface(QByteArray sendArray)
{
if(m_tcpSocket)
{
m_tcpSocket->write(sendArray);
}
}
//解析数据
void CTcpSocketInterface::analysisProtocal(QByteArray dataArray)
{
Q_UNUSED(dataArray)
}
void CTcpSocketInterface::receiveDataInterface()
{
QByteArray sendArray;
emit signalReadyRead(sendArray);
}

View File

@@ -0,0 +1,34 @@
#ifndef CTCPSOCKETINTERFACE_H
#define CTCPSOCKETINTERFACE_H
#include "ccommunicationinterface.h"
#include <QObject>
#include <QAbstractSocket>
class QTcpSocket;
class QTcpServer;
class CTcpSocketInterface : public CCommunicationInterface
{
public:
//0-客户端 1-服务端
CTcpSocketInterface(int type = 0);
//配置参数
virtual bool setConfigParam();
//发送数据接口
virtual void sendDataInterface(QByteArray);
//解析数据
virtual void analysisProtocal(QByteArray);
public slots:
//接收数据接口
void receiveDataInterface();
void newTcpConnection();
void displayError(QAbstractSocket::SocketError socketError);
void deviceStateChanged(QAbstractSocket::SocketState);
private:
QTcpSocket *m_tcpSocket;
QTcpServer *m_tcpServer;
};
#endif // CTCPSOCKETINTERFACE_H

View File

@@ -0,0 +1,129 @@
#include "cudpinterface.h"
#include <QUdpSocket>
#include "dataFormate.h"
#include "readconfig.h"
//LOG4QT_DECLARE_STATIC_LOGGER(logger, CUdpInterface)
CUdpInterface::CUdpInterface():m_udpSocket(NULL)
{
if(!m_udpSocket)
{
m_udpSocket = new QUdpSocket();
connect(m_udpSocket,&QUdpSocket::readyRead,this,&CUdpInterface::receiveDataInterface);
connect(m_udpSocket,&QUdpSocket::stateChanged,this,&CUdpInterface::deviceStateChanged);
setConfigParam();
}
}
CUdpInterface::~CUdpInterface()
{
if(m_udpSocket)
{
delete m_udpSocket;
m_udpSocket = NULL;
}
}
//配置参数
bool CUdpInterface::setConfigParam()
{
//读取配置文件
int16_t port;
QString ip;
if(ReadConfig::getInstance()->getUdpServerAddress(port,ip))
{
if(!m_udpSocket->bind(QHostAddress(ip),port,QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint))
{
// logger()->debug(QString("UDP bind port %1 failed").arg(port));
}
else
{
qDebug()<<"bind successed"<<port<<ip;
}
}
//绑定端口
return true;
}
//发送数据接口
void CUdpInterface::sendDataInterface(QByteArray sendArray)
{
int16_t port;
QString ip;
if(ReadConfig::getInstance()->getUdpClientAddress(port,ip))
{
if(m_udpSocket)
{
m_udpSocket->writeDatagram(sendArray,QHostAddress(ip),port);
}
}
}
//接收数据接口
void CUdpInterface::receiveDataInterface()
{
while(m_udpSocket->hasPendingDatagrams())
{
QByteArray buf;
buf.resize(m_udpSocket->pendingDatagramSize());
m_udpSocket->readDatagram(buf.data(),buf.size());
if(buf[0] != (char)PACKHEAD)
{
//如果不是完成包则丢弃
break;
}
else
{
//获取有效数据长度
uint8_t datalen = 0;
memcpy(&datalen,buf.constData()+1,sizeof(uint8_t));
//当缓存中的数据长度大于等于一包数据长度时开始解析
if(buf.length() >= datalen + 8)
{
uint8_t CRC_H = 0;
uint8_t CRC_L = 0;
#if 0
uint8_t *checkData = (uint8_t *)malloc(datalen+7);
memset(checkData,0,datalen+7);
memcpy(checkData,buf.constData(),datalen+7);
Pressure_CheckCRC(checkData,datalen+7,&CRC_H,&CRC_L);
free(checkData);
checkData = NULL;
#else
// Pressure_CheckCRC((uint8_t*)buf.constData(),datalen+6,&CRC_H,&CRC_L);
#endif
//校验成功
if((CRC_L == (uint8_t)buf[6+datalen]) && (CRC_H == (uint8_t)buf[7+datalen]))
{
emit signalReadyRead(buf.mid(0,datalen + 8));
buf.clear();
}
else
{
buf.clear();
break;
}
}
}
}
}
void CUdpInterface::deviceStateChanged(QAbstractSocket::SocketState state)
{
switch(state)
{
case QAbstractSocket::BoundState:
emit signalStateChanged(BoundState_E);
break;
case QAbstractSocket::ClosingState:
emit signalStateChanged(ClosingState_E);
break;
default:
break;
}
}
//解析数据
void CUdpInterface::analysisProtocal(QByteArray)
{
}

View File

@@ -0,0 +1,30 @@
#ifndef CUDPINTERFACE_H
#define CUDPINTERFACE_H
#include "ccommunicationinterface.h"
#include <QObject>
#include <QAbstractSocket>
class QUdpSocket;
class CUdpInterface : public CCommunicationInterface
{
public:
CUdpInterface();
~CUdpInterface();
//配置参数
virtual bool setConfigParam();
//发送数据接口
virtual void sendDataInterface(QByteArray);
//解析数据
virtual void analysisProtocal(QByteArray);
public slots:
//接收数据接口
void receiveDataInterface();
//设备状态发生变化
void deviceStateChanged(QAbstractSocket::SocketState);
//设备错误
void displayError(QAbstractSocket::SocketError socketError);
private:
QUdpSocket *m_udpSocket;
};
#endif // CUDPINTERFACE_H

View File

@@ -0,0 +1,41 @@
#include "globalqueue.h"
#include <QMutexLocker>
QMutex GlobalQueue::m_mutex;
QQueue<QByteArray> GlobalQueue::m_queue;
GlobalQueue* GlobalQueue::m_globalQueue = NULL;
GlobalQueue::GlobalQueue(QObject *parent) : QObject(parent)
{
}
GlobalQueue* GlobalQueue::getInstance()
{
if(!m_globalQueue)
{
m_globalQueue = new GlobalQueue();
}
return m_globalQueue;
}
void GlobalQueue::gEnqueue(const QByteArray& array)
{
QMutexLocker lock(&m_mutex);
m_queue.enqueue(array);
}
QByteArray GlobalQueue::gDequeue()
{
QMutexLocker lock(&m_mutex);
return m_queue.dequeue();
}
int GlobalQueue::getSize()
{
QMutexLocker lock(&m_mutex);
return m_queue.size();
}
void GlobalQueue::clearQueue()
{
QMutexLocker lock(&m_mutex);
m_queue.clear();
}

View File

@@ -0,0 +1,28 @@
#ifndef GLOBALQUEUE_H
#define GLOBALQUEUE_H
#include <QObject>
#include <QMutex>
#include <QQueue>
class GlobalQueue : public QObject
{
Q_OBJECT
public:
static GlobalQueue* getInstance();
void gEnqueue(const QByteArray&);
QByteArray gDequeue();
int getSize();
void clearQueue();
private:
explicit GlobalQueue(QObject *parent = nullptr);
static QMutex m_mutex;
//全局队列,用于数据的传输
static QQueue<QByteArray> m_queue;
static GlobalQueue* m_globalQueue;
};
#endif // GLOBALQUEUE_H

View File

@@ -0,0 +1,378 @@
#include "readconfig.h"
#include <QXmlStreamReader>
#include <QFile>
#include <QApplication>
#include <QDebug>
ReadConfig *ReadConfig::m_pInstance = NULL;
QMutex ReadConfig::mutex;
//#pragma execution_character_set("utf-8")
ReadConfig::ReadConfig()
{
// readConfigFile();
}
bool ReadConfig::readConfigFile()
{
QString fileName = "./DependFile/ConfigFile/config.xml";
QFile configFile(fileName);
if(!configFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"配置文件打开失败";
return false;
}
QXmlStreamReader reader(&configFile);
while(!reader.atEnd())
{
QXmlStreamReader::TokenType nType = reader.readNext();
switch(nType)
{
case QXmlStreamReader::StartDocument: //开始元素
{
QString strVersion = reader.documentVersion().toString();
QString strEncoding = reader.documentEncoding().toString();
bool bAlone = reader.isStandaloneDocument();
break;
}
case QXmlStreamReader::Comment://注释
{
QString strComment = reader.text().toString();
break;
}
case QXmlStreamReader::ProcessingInstruction://处理指令
{
QString strProInstr = reader.processingInstructionData().toString();
break;
}
case QXmlStreamReader::DTD://DTD标识
{
QString strDTD = reader.text().toString();
QXmlStreamNotationDeclarations notations = reader.notationDeclarations();
QXmlStreamEntityDeclarations entity = reader.entityDeclarations();
//DTD声明
QString strDTDName = reader.dtdName().toString();
QString strDTDPublicId = reader.dtdPublicId().toString();//DTD公开标识
QString strDTDSystemId = reader.dtdSystemId().toString();//DTD系统标识
break;
}
case QXmlStreamReader::StartElement://开始元素
{
QString strElementName = reader.name().toString();
if(QString::compare(strElementName,"config") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
}
else if(QString::compare(strElementName,"tcpAddress") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.tcpIP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.tcpPort = attributes.value("port").toInt();
}
}
else if(QString::compare(strElementName,"udpServerAddress") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.udpServerIP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.udpServerPort = attributes.value("port").toInt();
}
}
else if(QString::compare(strElementName,"udpClientAddress") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.udpClientIP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.udpClientPort = attributes.value("port").toInt();
}
}
//与游戏通信的上位机
else if(QString::compare(strElementName,"udpGameServerAddress") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.udpGameServerIP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.udpGameServerPort = attributes.value("port").toInt();
}
}
//与上位机通信的游戏
else if(QString::compare(strElementName,"udpGameClientAddress") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.udpGameClientIP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.udpGameClientPort = attributes.value("port").toInt();
}
}
//与下位机通信的串口
else if(QString::compare(strElementName,"serialPort") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("portName"))
{
st_configData.serialConfig.portName = attributes.value("portName").toString();
}
if(attributes.hasAttribute("baud"))
{
st_configData.serialConfig.baud = attributes.value("baud").toInt();
}
if(attributes.hasAttribute("dataBits"))
{
st_configData.serialConfig.dataBits = attributes.value("dataBits").toInt();
}
if(attributes.hasAttribute("parity"))
{
st_configData.serialConfig.parity = attributes.value("parity").toInt();
}
if(attributes.hasAttribute("stopBit"))
{
st_configData.serialConfig.stopBit = attributes.value("stopBit").toInt();
}
if(attributes.hasAttribute("flowControl"))
{
st_configData.serialConfig.flowControl = attributes.value("flowControl").toInt();
}
}
//电刺激A串口
else if(QString::compare(strElementName,"serialPortA") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("portName"))
{
st_configData.fesASerialConfig.portName = attributes.value("portName").toString();
}
if(attributes.hasAttribute("baud"))
{
st_configData.fesASerialConfig.baud = attributes.value("baud").toInt();
}
if(attributes.hasAttribute("dataBits"))
{
st_configData.fesASerialConfig.dataBits = attributes.value("dataBits").toInt();
}
if(attributes.hasAttribute("parity"))
{
st_configData.fesASerialConfig.parity = attributes.value("parity").toInt();
}
if(attributes.hasAttribute("stopBit"))
{
st_configData.fesASerialConfig.stopBit = attributes.value("stopBit").toInt();
}
if(attributes.hasAttribute("flowControl"))
{
st_configData.fesASerialConfig.flowControl = attributes.value("flowControl").toInt();
}
}
else if(QString::compare(strElementName,"serialPortB") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("portName"))
{
st_configData.fesBSerialConfig.portName = attributes.value("portName").toString();
}
if(attributes.hasAttribute("baud"))
{
st_configData.fesBSerialConfig.baud = attributes.value("baud").toInt();
}
if(attributes.hasAttribute("dataBits"))
{
st_configData.fesBSerialConfig.dataBits = attributes.value("dataBits").toInt();
}
if(attributes.hasAttribute("parity"))
{
st_configData.fesBSerialConfig.parity = attributes.value("parity").toInt();
}
if(attributes.hasAttribute("stopBit"))
{
st_configData.fesBSerialConfig.stopBit = attributes.value("stopBit").toInt();
}
if(attributes.hasAttribute("flowControl"))
{
st_configData.fesBSerialConfig.flowControl = attributes.value("flowControl").toInt();
}
}
else if(QString::compare(strElementName,"dataBase") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("IP"))
{
st_configData.dataBaseConfig.IP = attributes.value("IP").toString();
}
if(attributes.hasAttribute("port"))
{
st_configData.dataBaseConfig.port = attributes.value("port").toInt();
}
if(attributes.hasAttribute("userName"))
{
st_configData.dataBaseConfig.userName = attributes.value("userName").toString();
}
if(attributes.hasAttribute("password"))
{
st_configData.dataBaseConfig.password = attributes.value("password").toString();
}
}
else if(QString::compare(strElementName,"communicateType") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("type"))
st_configData.communicateType = attributes.value("type").toInt();
}
else if(QString::compare(strElementName,"videoTips") == 0)
{
QXmlStreamAttributes attributes = reader.attributes();
if(attributes.hasAttribute("videoPath"))
{
st_configData.videoPath = attributes.value("videoPath").toString();
}
if(attributes.hasAttribute("videoNum"))
{
}
if(strElementName == "videoContent")
{
qDebug()<<"########";
}
}
break;
}
case QXmlStreamReader::EndDocument://结束文档
{
break;
}
default:
break;
}
}
configFile.close();
if(reader.hasError())
{
qDebug()<<QString::fromLocal8Bit("错误信息:%1 行号:%2 列号:%3 字符移位:%4").arg(reader.errorString())
.arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.characterOffset());
return false;
}
return true;
}
bool ReadConfig::getFesASerialPortConfig(ST_SerialPortConfig &serialConfig)
{
serialConfig = st_configData.fesASerialConfig;
if(!serialConfig.portName.isEmpty() && serialConfig.baud != 0)
{
return true;
}
else
return false;
}
//获取实例
ReadConfig* ReadConfig::getInstance()
{
QMutexLocker mutexLocker(&mutex);
if(!m_pInstance)
{
m_pInstance = new ReadConfig();
}
return m_pInstance;
}
//获取通信方式
int ReadConfig::getCommunicateType()
{
return st_configData.communicateType;
}
//获取UDP地址信息
bool ReadConfig::getUdpServerAddress(int16_t &port,QString &IP)
{
port = st_configData.udpServerPort;
IP = st_configData.udpServerIP;
if(port != 0 && !IP.isEmpty())
return true;
else
return false;
}
bool ReadConfig::getUdpClientAddress(int16_t &port,QString &IP)
{
port = st_configData.udpClientPort;
IP = st_configData.udpClientIP;
if(port != 0 && !IP.isEmpty())
return true;
else
return false;
}
//获取TCP地址信息
bool ReadConfig::getTcpAddress(int16_t &port,QString &IP)
{
port = st_configData.tcpPort;
IP = st_configData.tcpIP;
if(port != 0 && !IP.isEmpty())
return true;
else
return false;
}
//获取串口信息
bool ReadConfig::getSerialPortConfig(ST_SerialPortConfig &serialConfig)
{
serialConfig = st_configData.serialConfig;
if(!serialConfig.portName.isEmpty() && serialConfig.baud != 0)
{
return true;
}
else
return false;
}
//获取数据库配置
bool ReadConfig::getDataBaseConfig(ST_DataBaseConfig &databaseConfig)
{
databaseConfig = st_configData.dataBaseConfig;
if(!databaseConfig.IP.isEmpty())
return true;
else
return false;
}
bool ReadConfig::getGameServerAddress(int16_t &port,QString& IP)
{
port = st_configData.udpGameServerPort;
IP = st_configData.udpGameServerIP;
return true;
}
bool ReadConfig::getGameClientAddress(int16_t &port,QString& IP)
{
port = st_configData.udpGameClientPort;
IP = st_configData.udpGameClientIP;
return true;
}

View File

@@ -0,0 +1,76 @@
#ifndef READCONFIG_H
#define READCONFIG_H
#include <QObject>
#include "dataformate.h"
#include <QMutex>
#include <QMap>
//饿汉式单例
class ReadConfig
{
private:
ReadConfig();
static QMutex mutex;
static ReadConfig *m_pInstance;
typedef struct ST_AllConfigData
{
QString udpServerIP;
int16_t udpServerPort;
QString udpClientIP;
int16_t udpClientPort;
QString udpGameClientIP;
int16_t udpGameClientPort;
QString udpGameServerIP;
int16_t udpGameServerPort;
QString tcpIP;
int16_t tcpPort;
ST_SerialPortConfig serialConfig;
ST_SerialPortConfig fesASerialConfig;
ST_SerialPortConfig fesBSerialConfig;
ST_DataBaseConfig dataBaseConfig;
int communicateType = -1;
QString videoPath; //视频路径
QMap<int,QString> videoMap; //视频提示管理QMap<视频序号,视频名称>
ST_AllConfigData()
{
udpServerIP.clear();
udpServerPort = 0;
tcpIP.clear();
tcpPort = 0;
}
}ST_AllConfigData;
ST_AllConfigData st_configData;
public:
//获取实例
static ReadConfig* getInstance();
//上下位机通信地址
bool getUdpServerAddress(int16_t &port,QString &IP);
bool getUdpClientAddress(int16_t &port,QString &IP);
//获取TCP地址信息
bool getTcpAddress(int16_t &port,QString &IP);
//获取串口信息
bool getSerialPortConfig(ST_SerialPortConfig &serialConfig);
//获取数据库配置
bool getDataBaseConfig(ST_DataBaseConfig &databaseConfig);
//获取通信方式
int getCommunicateType();
bool readConfigFile();
//获取电刺激设备信息
bool getFesASerialPortConfig(ST_SerialPortConfig &serialConfig);
//与游戏通信地址
bool getGameServerAddress(int16_t &port,QString& IP);
//游戏地址
bool getGameClientAddress(int16_t &port,QString& IP);
};
#endif // READCONFIG_H