协能can协议
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ProtocolImpl.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. * @content:ModbusRTU组件定义头文件
  3. * @time:2016-8-23
  4. * @author:Mr_zhu
  5. * @version: V1.0
  6. * @describe:
  7. * 1#2016-8-23#V1.0#首次生成
  8. */
  9. #ifndef _INCLUDE_CMPNT_MODBUSRTU_H
  10. #define _INCLUDE_CMPNT_MODBUSRTU_H
  11. #include <cstdio>
  12. #include <map>
  13. #include <string>
  14. #include "../../common/Type.h"
  15. #include "../ProtocolI.h"
  16. class ProtocolImpl: public ProtocolI
  17. {
  18. private:
  19. virtual HRESULT queryInterface(const IID& iid, void** ppv);
  20. virtual ULONG addRef();
  21. virtual ULONG release();
  22. virtual ULONG getVersion();
  23. /**
  24. * 读数据
  25. */
  26. virtual HRESULT onRead(Device* pdevice, //----设备句柄
  27. Packet* ppacket, //----包句柄
  28. Item* pitem, //----点句柄
  29. PBYTE pbuf, //----数据缓冲区
  30. int& len //----数据长度
  31. );
  32. /**
  33. * 写数据
  34. */
  35. virtual HRESULT onWrite(Device* pdevice, //----设备句柄
  36. Packet* ppacket, //----包句柄
  37. Item* pitem, //----点句柄
  38. PBYTE pbuf, //----数据缓冲区
  39. int& len, //----数据长度
  40. std::string swritebuf, //----待写数据字符串
  41. const unsigned int writelen //----待写数据长度
  42. );
  43. /**
  44. * 判断回复数据是否完全
  45. */
  46. virtual HRESULT isResponseOK(Device* pdevice, //----设备句柄
  47. Packet* ppacket, //----包句柄
  48. Item* pitem, //----点句柄
  49. PBYTE pbuf, //----数据缓冲区
  50. const int len //----数据长度
  51. );
  52. /**
  53. * 解析数据
  54. */
  55. virtual HRESULT onResponse(Device* pdevice, //----设备句柄
  56. Packet* ppacket, //----包句柄
  57. Item* pitem, //----点句柄
  58. PBYTE pbuf, //----数据缓冲区
  59. const int len, //----数据长度
  60. int& deletelen //----删除数据长度
  61. );
  62. public:
  63. ProtocolImpl() :
  64. m_cRef(0), m_Level(0), VERSION(100)
  65. {
  66. if (!hasInit)
  67. {
  68. initLogger();
  69. hasInit = true;
  70. }
  71. }
  72. ~ProtocolImpl()
  73. {
  74. }
  75. private:
  76. struct candata
  77. {
  78. BYTE data0;
  79. BYTE data1;
  80. // BYTE data2;
  81. // BYTE data3;
  82. // BYTE data4;
  83. // BYTE data5;
  84. // BYTE data6;
  85. // BYTE data7;
  86. };
  87. static bool hasInit;
  88. struct canReqData
  89. {
  90. BYTE data0;
  91. BYTE data1;
  92. };
  93. private:
  94. volatile long m_cRef;
  95. int m_Level;
  96. static BYTE auchCRCHi[];
  97. static BYTE auchCRCLo[];
  98. std::map<std::string, struct candata> m_tmpwritedata;
  99. // std::map<std::string, std::multimap<int, Packet*> > m_tmppackets;
  100. std::map<std::string, std::multimap<int, Item*> > m_tmpItems;
  101. const ULONG VERSION;
  102. int findAddrFlag = 0;
  103. private:
  104. unsigned short getMinStartAddr(Packet* ppacket);
  105. unsigned short getMaxStartAddr(Packet* ppacket, unsigned short& regnum);
  106. unsigned short getRegNum(Packet* ppacket);
  107. unsigned short getExpectLen(Packet* ppacket);
  108. unsigned short GetCRC(PBYTE pchMsg, unsigned short usDataLen);
  109. uint8_t retBigEndian(uint32_t value, int offset);
  110. int retStateResponse(Packet* ppacket,Item* pitem,const int len);
  111. int getRealFrame (PBYTE pbuf,int count);
  112. int getRegAddrMax(Packet* ppacket,Item* pitem);
  113. int getRegAddrMin(Packet* ppacket,Item* pitem);
  114. int getRealValidLen(int maxRegNum);
  115. std::map<int,int>handleDataLess(PBYTE pbuf,int len,int regNum,int datasLen);
  116. std::map<int,int>handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen);
  117. std::map<int,int>retDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
  118. int iGetItemParaConfig(Item *pitem, std::string attribute);
  119. std::string sGetItemParaConfig(Item *pitem, std::string attribute);
  120. void setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,std::string t_sBits);
  121. std::vector<uint8_t> intToBytes(int value);
  122. inline std::string merge8(PBYTE pbuf, std::string byteorder)
  123. {
  124. BYTE t_sdata = 0;
  125. char t[256];
  126. std::string t_str;
  127. t_sdata = pbuf[0] & 0x00ff;
  128. snprintf(t, 256, "%d", t_sdata);
  129. t_str = t;
  130. return t_str;
  131. }
  132. inline std::string merge8_u(PBYTE pbuf, std::string byteorder)
  133. {
  134. unsigned char t_sdata = 0;
  135. char t[256];
  136. std::string t_str;
  137. t_sdata = (unsigned char) (pbuf[0] & 0x00ff);
  138. snprintf(t, 256, "%d", t_sdata);
  139. t_str = t;
  140. return t_str;
  141. }
  142. //inline std::string merge16(PBYTE pbuf, std::string byteorder)
  143. inline std::string merge16(int16_t pbuf, std::string byteorder)
  144. {
  145. short t_sdata = 0;
  146. char t[256];
  147. std::string t_str;
  148. if (byteorder == "BA")
  149. {
  150. // //t_sdata = ((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff);
  151. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  152. // snprintf(t, 256, "%d", t_sdata);
  153. //
  154. // t_str = t;
  155. // log_i("BA-merge16 t_str= %s",t_str.c_str());
  156. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  157. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  158. t_sdata = low_byte | (high_byte << 8) ;
  159. snprintf(t, 256, "%d", t_sdata);
  160. t_str = t;
  161. log_i("BA-merge16 t_str= %s", t_str.c_str());
  162. }
  163. else if (byteorder == "AB")
  164. {
  165. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  166. // snprintf(t, 256, "%d", t_sdata);
  167. // t_str = t;
  168. // log_i("AB-merge16 t_str= %s",t_str.c_str());
  169. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  170. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  171. t_sdata = (low_byte << 8) | high_byte;
  172. snprintf(t, 256, "%d", t_sdata);
  173. t_str = t;
  174. log_i("AB-merge16 t_str= %s", t_str.c_str());
  175. }
  176. return t_str;
  177. }
  178. //inline std::string merge16_u(PBYTE pbuf, std::string byteorder)
  179. inline std::string merge16_u(int16_t pbuf, std::string byteorder)
  180. {
  181. unsigned short t_sdata = 0;
  182. char t[256];
  183. std::string t_str;
  184. if (byteorder == "BA")
  185. {
  186. // t_sdata = (unsigned short) (((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff));
  187. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  188. snprintf(t, 256, "%d", t_sdata);
  189. t_str = t;
  190. }
  191. else if (byteorder == "AB")
  192. {
  193. // t_sdata = (unsigned short) (((pbuf[1] << 8) & 0xff00) | ((pbuf[0]) & 0x00ff));
  194. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  195. snprintf(t, 256, "%d", t_sdata);
  196. t_str = t;
  197. }
  198. return t_str;
  199. }
  200. inline std::string merge32(PBYTE pbuf, std::string byteorder)
  201. {
  202. int t_idata = 0;
  203. char t[256];
  204. std::string t_str;
  205. if (byteorder == "BADC")
  206. {
  207. t_idata = ((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000);
  208. snprintf(t, 256, "%d", t_idata);
  209. t_str = t;
  210. }
  211. else if (byteorder == "DCBA")
  212. {
  213. t_idata = ((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff);
  214. snprintf(t, 256, "%d", t_idata);
  215. t_str = t;
  216. }
  217. else if (byteorder == "ABCD")
  218. {
  219. t_idata = ((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000);
  220. snprintf(t, 256, "%d", t_idata);
  221. t_str = t;
  222. }
  223. else if (byteorder == "CDAB")
  224. {
  225. t_idata = ((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00);
  226. snprintf(t, 256, "%d", t_idata);
  227. t_str = t;
  228. }
  229. else
  230. {
  231. }
  232. return t_str;
  233. }
  234. inline std::string merge32_u(PBYTE pbuf, std::string byteorder)
  235. {
  236. unsigned int t_idata = 0;
  237. char t[256];
  238. std::string t_str;
  239. if (byteorder == "BADC")
  240. {
  241. t_idata =
  242. (unsigned int) (((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000));
  243. snprintf(t, 256, "%d", t_idata);
  244. t_str = t;
  245. }
  246. else if (byteorder == "DCBA")
  247. {
  248. t_idata =
  249. (unsigned int) (((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff));
  250. snprintf(t, 256, "%d", t_idata);
  251. t_str = t;
  252. }
  253. else if (byteorder == "ABCD")
  254. {
  255. t_idata =
  256. (unsigned int) (((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000));
  257. snprintf(t, 256, "%d", t_idata);
  258. t_str = t;
  259. }
  260. else if (byteorder == "CDAB")
  261. {
  262. t_idata =
  263. (unsigned int) (((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00));
  264. snprintf(t, 256, "%d", t_idata);
  265. t_str = t;
  266. }
  267. else
  268. {
  269. }
  270. return t_str;
  271. }
  272. float intbitstofloat(int bits)
  273. {
  274. union
  275. {
  276. int i;
  277. float f;
  278. } u;
  279. u.i = bits;
  280. return u.f;
  281. }
  282. int floatbitstoint(float bits)
  283. {
  284. union
  285. {
  286. int i;
  287. float f;
  288. } u;
  289. u.f = bits;
  290. return u.i;
  291. }
  292. inline void initLogger()
  293. {
  294. /**
  295. * LOG系统初始化
  296. */
  297. /* close printf buffer */
  298. setbuf(stdout, NULL);
  299. /* initialize EasyLogger */
  300. elog_init();
  301. /* set EasyLogger log format */
  302. elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
  303. elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  304. elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  305. elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  306. elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  307. elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  308. #ifdef ELOG_COLOR_ENABLE
  309. elog_set_text_color_enabled(true);
  310. #endif
  311. /* start EasyLogger */
  312. elog_start();
  313. }
  314. };
  315. #endif