/** * @content:ModbusRTU组件定义头文件 * @time:2016-8-23 * @author:Mr_zhu * @version: V1.0 * @describe: * 1#2016-8-23#V1.0#首次生成 */ #ifndef _INCLUDE_CMPNT_MODBUSRTU_H #define _INCLUDE_CMPNT_MODBUSRTU_H #include #include #include #include "../../common/Type.h" #include "../ProtocolI.h" class ProtocolImpl: public ProtocolI { private: virtual HRESULT queryInterface(const IID& iid, void** ppv); virtual ULONG addRef(); virtual ULONG release(); virtual ULONG getVersion(); /** * 读数据 */ virtual HRESULT onRead(Device* pdevice, //----设备句柄 Packet* ppacket, //----包句柄 Item* pitem, //----点句柄 PBYTE pbuf, //----数据缓冲区 int& len //----数据长度 ); /** * 写数据 */ virtual HRESULT onWrite(Device* pdevice, //----设备句柄 Packet* ppacket, //----包句柄 Item* pitem, //----点句柄 PBYTE pbuf, //----数据缓冲区 int& len, //----数据长度 std::string swritebuf, //----待写数据字符串 const unsigned int writelen //----待写数据长度 ); /** * 判断回复数据是否完全 */ virtual HRESULT isResponseOK(Device* pdevice, //----设备句柄 Packet* ppacket, //----包句柄 Item* pitem, //----点句柄 PBYTE pbuf, //----数据缓冲区 const int len //----数据长度 ); /** * 解析数据 */ virtual HRESULT onResponse(Device* pdevice, //----设备句柄 Packet* ppacket, //----包句柄 Item* pitem, //----点句柄 PBYTE pbuf, //----数据缓冲区 const int len, //----数据长度 int& deletelen //----删除数据长度 ); public: ProtocolImpl() : m_cRef(0), m_Level(0), VERSION(100) { if (!hasInit) { initLogger(); hasInit = true; } } ~ProtocolImpl() { } private: struct candata { BYTE data0; BYTE data1; // BYTE data2; // BYTE data3; // BYTE data4; // BYTE data5; // BYTE data6; // BYTE data7; }; static bool hasInit; struct canReqData { BYTE data0; BYTE data1; }; private: volatile long m_cRef; int m_Level; static BYTE auchCRCHi[]; static BYTE auchCRCLo[]; std::map m_tmpwritedata; // std::map > m_tmppackets; std::map > m_tmpItems; const ULONG VERSION; int findAddrFlag = 0; private: unsigned short getMinStartAddr(Packet* ppacket); unsigned short getMaxStartAddr(Packet* ppacket, unsigned short& regnum); unsigned short getRegNum(Packet* ppacket); unsigned short getExpectLen(Packet* ppacket); unsigned short GetCRC(PBYTE pchMsg, unsigned short usDataLen); uint8_t retBigEndian(uint32_t value, int offset); int retStateResponse(Packet* ppacket,Item* pitem,const int len); int getRealFrame (PBYTE pbuf,int count); int getRegAddrMax(Packet* ppacket,Item* pitem); int getRegAddrMin(Packet* ppacket,Item* pitem); int getRealValidLen(int maxRegNum); std::maphandleDataLess(PBYTE pbuf,int len,int regNum,int datasLen); std::maphandleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen); std::mapretDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len); int iGetItemParaConfig(Item *pitem, std::string attribute); std::string sGetItemParaConfig(Item *pitem, std::string attribute); void setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,std::string t_sBits); std::vector intToBytes(int value); inline std::string merge8(PBYTE pbuf, std::string byteorder) { BYTE t_sdata = 0; char t[256]; std::string t_str; t_sdata = pbuf[0] & 0x00ff; snprintf(t, 256, "%d", t_sdata); t_str = t; return t_str; } inline std::string merge8_u(PBYTE pbuf, std::string byteorder) { unsigned char t_sdata = 0; char t[256]; std::string t_str; t_sdata = (unsigned char) (pbuf[0] & 0x00ff); snprintf(t, 256, "%d", t_sdata); t_str = t; return t_str; } //inline std::string merge16(PBYTE pbuf, std::string byteorder) inline std::string merge16(int16_t pbuf, std::string byteorder) { short t_sdata = 0; char t[256]; std::string t_str; if (byteorder == "BA") { // //t_sdata = ((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff); // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff); // snprintf(t, 256, "%d", t_sdata); // // t_str = t; // log_i("BA-merge16 t_str= %s",t_str.c_str()); uint8_t high_byte = static_cast((pbuf >> 8) & 0xff); uint8_t low_byte = static_cast(pbuf & 0xff); t_sdata = low_byte | (high_byte << 8) ; snprintf(t, 256, "%d", t_sdata); t_str = t; log_i("BA-merge16 t_str= %s", t_str.c_str()); } else if (byteorder == "AB") { // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff); // snprintf(t, 256, "%d", t_sdata); // t_str = t; // log_i("AB-merge16 t_str= %s",t_str.c_str()); uint8_t high_byte = static_cast((pbuf >> 8) & 0xff); uint8_t low_byte = static_cast(pbuf & 0xff); t_sdata = (low_byte << 8) | high_byte; snprintf(t, 256, "%d", t_sdata); t_str = t; log_i("AB-merge16 t_str= %s", t_str.c_str()); } return t_str; } //inline std::string merge16_u(PBYTE pbuf, std::string byteorder) inline std::string merge16_u(int16_t pbuf, std::string byteorder) { unsigned short t_sdata = 0; char t[256]; std::string t_str; if (byteorder == "BA") { // t_sdata = (unsigned short) (((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff)); t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff)); snprintf(t, 256, "%d", t_sdata); t_str = t; } else if (byteorder == "AB") { // t_sdata = (unsigned short) (((pbuf[1] << 8) & 0xff00) | ((pbuf[0]) & 0x00ff)); t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff)); snprintf(t, 256, "%d", t_sdata); t_str = t; } return t_str; } inline std::string merge32(PBYTE pbuf, std::string byteorder) { int t_idata = 0; char t[256]; std::string t_str; if (byteorder == "BADC") { t_idata = ((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "DCBA") { t_idata = ((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "ABCD") { t_idata = ((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "CDAB") { t_idata = ((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00); snprintf(t, 256, "%d", t_idata); t_str = t; } else { } return t_str; } inline std::string merge32_u(PBYTE pbuf, std::string byteorder) { unsigned int t_idata = 0; char t[256]; std::string t_str; if (byteorder == "BADC") { t_idata = (unsigned int) (((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000)); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "DCBA") { t_idata = (unsigned int) (((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff)); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "ABCD") { t_idata = (unsigned int) (((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000)); snprintf(t, 256, "%d", t_idata); t_str = t; } else if (byteorder == "CDAB") { t_idata = (unsigned int) (((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00)); snprintf(t, 256, "%d", t_idata); t_str = t; } else { } return t_str; } float intbitstofloat(int bits) { union { int i; float f; } u; u.i = bits; return u.f; } int floatbitstoint(float bits) { union { int i; float f; } u; u.f = bits; return u.i; } inline void initLogger() { /** * LOG系统初始化 */ /* close printf buffer */ setbuf(stdout, NULL); /* initialize EasyLogger */ elog_init(); /* set EasyLogger log format */ elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL); elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); #ifdef ELOG_COLOR_ENABLE elog_set_text_color_enabled(true); #endif /* start EasyLogger */ elog_start(); } }; #endif