协能can协议
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ProtocolImpl.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. enum state
  94. {
  95. INIT= 0,
  96. SEND ,
  97. RECEIVED ,
  98. END,
  99. };
  100. private:
  101. volatile long m_cRef;
  102. int m_Level;
  103. static BYTE auchCRCHi[];
  104. static BYTE auchCRCLo[];
  105. std::map<std::string, struct candata> m_tmpwritedata;
  106. // std::map<std::string, std::multimap<int, Packet*> > m_tmppackets;
  107. std::map<std::string, std::multimap<int, Item*> > m_tmpItems;
  108. const ULONG VERSION;
  109. int findAddrFlag = 0;
  110. int idSuccessFlag = 0;
  111. int sendCurrentId = 0;
  112. int currentId = 1; //分配ID编号
  113. int maxId = 0; //最大分配ID
  114. int orderMode = 0; //分配模式
  115. int orderState = 0; //分配状态
  116. int firstResponSuccessFlag = 0;
  117. int timeOut = 0;
  118. std::string devPath = "/sys/class/gpio/gpio436/value"; //输入输出电平值设备
  119. std::string exportPath = "/sys/class/gpio/export"; //GPIO设备导出设备
  120. std::string directPath = "/sys/class/gpio/gpio436/direction"; //GPIO输入输出控制设备
  121. std::string out ="out";
  122. std::string in = "in";
  123. std::string gpio = "436";
  124. std::string highLevel = "1";
  125. std::string lowLevel = "0";
  126. int count = 0;
  127. int currentState = 0;
  128. int sendDelay = 0;
  129. private:
  130. unsigned short getMinStartAddr(Packet* ppacket);
  131. unsigned short getMaxStartAddr(Packet* ppacket, unsigned short& regnum);
  132. unsigned short getRegNum(Packet* ppacket);
  133. unsigned short getExpectLen(Packet* ppacket);
  134. unsigned short GetCRC(PBYTE pchMsg, unsigned short usDataLen);
  135. uint8_t retBigEndian(uint32_t value, int offset);
  136. int retStateResponse(Packet* ppacket,Item* pitem,const int len);
  137. int getRealFrame (PBYTE pbuf,int count);
  138. int getRegAddrMax(Packet* ppacket,Item* pitem);
  139. int getRegAddrMin(Packet* ppacket,Item* pitem);
  140. int getRealValidLen(int maxRegNum);
  141. std::map<int,int>handleDataLess(PBYTE pbuf,int len,int regNum,int datasLen);
  142. std::map<int,int>handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen);
  143. std::map<int,int>retDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
  144. int iGetItemParaConfig(Item *pitem, std::string attribute);
  145. std::string sGetItemParaConfig(Item *pitem, std::string attribute);
  146. void setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,std::string t_sBits);
  147. int setGpioDetrict();
  148. void proceessReplyFrame(int frame,int datas);
  149. void sendAllocFrame(PBYTE pbuf, int id,int &len);
  150. void checkAllocStatus() ;
  151. void reportAlarm() ;
  152. int set_gpio_value(const std::string& gpio_path, const std::string& value) ;
  153. void initIO();
  154. void receiveAllocFrameRes();
  155. void ending();
  156. void ioFrameDeal(PBYTE pbuf,int len);
  157. void ioResponse(Packet *ppacket, Item *pitem,PBYTE pbuf, const int len);
  158. int normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pitem,PBYTE pbuf,const int len);
  159. std::map<int,int> retIoDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
  160. std::map<int,int> handleIoData(PBYTE pbuf);
  161. std::vector<uint8_t> intToBytes(int value);
  162. inline std::string merge8(PBYTE pbuf, std::string byteorder)
  163. {
  164. BYTE t_sdata = 0;
  165. char t[256];
  166. std::string t_str;
  167. t_sdata = pbuf[0] & 0x00ff;
  168. snprintf(t, 256, "%d", t_sdata);
  169. t_str = t;
  170. return t_str;
  171. }
  172. inline std::string merge8_u(PBYTE pbuf, std::string byteorder)
  173. {
  174. unsigned char t_sdata = 0;
  175. char t[256];
  176. std::string t_str;
  177. t_sdata = (unsigned char) (pbuf[0] & 0x00ff);
  178. snprintf(t, 256, "%d", t_sdata);
  179. t_str = t;
  180. return t_str;
  181. }
  182. //inline std::string merge16(PBYTE pbuf, std::string byteorder)
  183. inline std::string merge16(int16_t pbuf, std::string byteorder)
  184. {
  185. short t_sdata = 0;
  186. char t[256];
  187. std::string t_str;
  188. if (byteorder == "BA")
  189. {
  190. // //t_sdata = ((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff);
  191. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  192. // snprintf(t, 256, "%d", t_sdata);
  193. //
  194. // t_str = t;
  195. // log_i("BA-merge16 t_str= %s",t_str.c_str());
  196. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  197. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  198. t_sdata = low_byte | (high_byte << 8) ;
  199. snprintf(t, 256, "%d", t_sdata);
  200. t_str = t;
  201. log_i("BA-merge16 t_str= %s", t_str.c_str());
  202. }
  203. else if (byteorder == "AB")
  204. {
  205. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  206. // snprintf(t, 256, "%d", t_sdata);
  207. // t_str = t;
  208. // log_i("AB-merge16 t_str= %s",t_str.c_str());
  209. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  210. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  211. t_sdata = (low_byte << 8) | high_byte;
  212. snprintf(t, 256, "%d", t_sdata);
  213. t_str = t;
  214. log_i("AB-merge16 t_str= %s", t_str.c_str());
  215. }
  216. return t_str;
  217. }
  218. //inline std::string merge16_u(PBYTE pbuf, std::string byteorder)
  219. inline std::string merge16_u(int16_t pbuf, std::string byteorder)
  220. {
  221. unsigned short t_sdata = 0;
  222. char t[256];
  223. std::string t_str;
  224. if (byteorder == "BA")
  225. {
  226. // t_sdata = (unsigned short) (((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff));
  227. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  228. snprintf(t, 256, "%d", t_sdata);
  229. t_str = t;
  230. }
  231. else if (byteorder == "AB")
  232. {
  233. // t_sdata = (unsigned short) (((pbuf[1] << 8) & 0xff00) | ((pbuf[0]) & 0x00ff));
  234. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  235. snprintf(t, 256, "%d", t_sdata);
  236. t_str = t;
  237. }
  238. return t_str;
  239. }
  240. inline std::string merge32(PBYTE pbuf, std::string byteorder)
  241. {
  242. int t_idata = 0;
  243. char t[256];
  244. std::string t_str;
  245. if (byteorder == "BADC")
  246. {
  247. t_idata = ((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000);
  248. snprintf(t, 256, "%d", t_idata);
  249. t_str = t;
  250. }
  251. else if (byteorder == "DCBA")
  252. {
  253. t_idata = ((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff);
  254. snprintf(t, 256, "%d", t_idata);
  255. t_str = t;
  256. }
  257. else if (byteorder == "ABCD")
  258. {
  259. t_idata = ((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000);
  260. snprintf(t, 256, "%d", t_idata);
  261. t_str = t;
  262. }
  263. else if (byteorder == "CDAB")
  264. {
  265. t_idata = ((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00);
  266. snprintf(t, 256, "%d", t_idata);
  267. t_str = t;
  268. }
  269. else
  270. {
  271. }
  272. return t_str;
  273. }
  274. inline std::string merge32_u(PBYTE pbuf, std::string byteorder)
  275. {
  276. unsigned int t_idata = 0;
  277. char t[256];
  278. std::string t_str;
  279. if (byteorder == "BADC")
  280. {
  281. t_idata =
  282. (unsigned int) (((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000));
  283. snprintf(t, 256, "%d", t_idata);
  284. t_str = t;
  285. }
  286. else if (byteorder == "DCBA")
  287. {
  288. t_idata =
  289. (unsigned int) (((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff));
  290. snprintf(t, 256, "%d", t_idata);
  291. t_str = t;
  292. }
  293. else if (byteorder == "ABCD")
  294. {
  295. t_idata =
  296. (unsigned int) (((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000));
  297. snprintf(t, 256, "%d", t_idata);
  298. t_str = t;
  299. }
  300. else if (byteorder == "CDAB")
  301. {
  302. t_idata =
  303. (unsigned int) (((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00));
  304. snprintf(t, 256, "%d", t_idata);
  305. t_str = t;
  306. }
  307. else
  308. {
  309. }
  310. return t_str;
  311. }
  312. float intbitstofloat(int bits)
  313. {
  314. union
  315. {
  316. int i;
  317. float f;
  318. } u;
  319. u.i = bits;
  320. return u.f;
  321. }
  322. int floatbitstoint(float bits)
  323. {
  324. union
  325. {
  326. int i;
  327. float f;
  328. } u;
  329. u.f = bits;
  330. return u.i;
  331. }
  332. inline void initLogger()
  333. {
  334. /**
  335. * LOG系统初始化
  336. */
  337. /* close printf buffer */
  338. setbuf(stdout, NULL);
  339. /* initialize EasyLogger */
  340. elog_init();
  341. /* set EasyLogger log format */
  342. elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
  343. elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  344. elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  345. elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  346. elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  347. elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  348. #ifdef ELOG_COLOR_ENABLE
  349. elog_set_text_color_enabled(true);
  350. #endif
  351. /* start EasyLogger */
  352. elog_start();
  353. }
  354. };
  355. #endif