#include "ccommunicateapi.h" #include "readconfig.h" #include "ccommunicationinterface.h" #include "cserialportinterface.h" #include "cudpinterface.h" #include "ctcpsocketinterface.h" #include 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() << "数据:" <sendData(cmdId,array); } void CCommunicateAPI::setConfigParam() { m_interface->setConfigParam(); }