| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- /**
- * @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 <cstdio>
- #include <map>
- #include <string>
-
- #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<std::string, struct candata> m_tmpwritedata;
-
- // std::map<std::string, std::multimap<int, Packet*> > m_tmppackets;
- std::map<std::string, std::multimap<int, Item*> > 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::map<int,int>handleDataLess(PBYTE pbuf,int len,int regNum,int datasLen);
- std::map<int,int>handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen);
- std::map<int,int>retDataSet(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<uint8_t> 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<uint8_t>((pbuf >> 8) & 0xff);
- uint8_t low_byte = static_cast<uint8_t>(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<uint8_t>((pbuf >> 8) & 0xff);
- uint8_t low_byte = static_cast<uint8_t>(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
|