协能can协议
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. private:
  129. unsigned short getMinStartAddr(Packet* ppacket);
  130. unsigned short getMaxStartAddr(Packet* ppacket, unsigned short& regnum);
  131. unsigned short getRegNum(Packet* ppacket);
  132. unsigned short getExpectLen(Packet* ppacket);
  133. unsigned short GetCRC(PBYTE pchMsg, unsigned short usDataLen);
  134. uint8_t retBigEndian(uint32_t value, int offset);
  135. int retStateResponse(Packet* ppacket,Item* pitem,const int len);
  136. int getRealFrame (PBYTE pbuf,int count);
  137. int getRegAddrMax(Packet* ppacket,Item* pitem);
  138. int getRegAddrMin(Packet* ppacket,Item* pitem);
  139. int getRealValidLen(int maxRegNum);
  140. std::map<int,int>handleDataLess(PBYTE pbuf,int len,int regNum,int datasLen);
  141. std::map<int,int>handleDataGreater(PBYTE pbuf,int len,int regNum,int datasLen);
  142. std::map<int,int>retDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
  143. int iGetItemParaConfig(Item *pitem, std::string attribute);
  144. std::string sGetItemParaConfig(Item *pitem, std::string attribute);
  145. void setData(Item *pitem,int16_t pbuf,int receiveData,int paraData,std::string t_sDataType,std::string t_sByteOrder,std::string t_sBits);
  146. int setGpioDetrict();
  147. void proceessReplyFrame(int frame,int datas);
  148. void sendAllocFrame(PBYTE pbuf, int id,int &len);
  149. void checkAllocStatus() ;
  150. void reportAlarm() ;
  151. int set_gpio_value(const std::string& gpio_path, const std::string& value) ;
  152. void initIO();
  153. void receiveAllocFrameRes();
  154. void ending();
  155. void ioFrameDeal(PBYTE pbuf,int len);
  156. void ioResponse(Packet *ppacket, Item *pitem,PBYTE pbuf, const int len);
  157. int normalDatasResponse(Device* pdevice,Packet* ppacket,Item* pitem,PBYTE pbuf,const int len);
  158. std::map<int,int> retIoDataSet(Packet *ppacket, Item *pitem,PBYTE pbuf,int len);
  159. std::map<int,int> handleIoData(PBYTE pbuf);
  160. std::vector<uint8_t> intToBytes(int value);
  161. inline std::string merge8(PBYTE pbuf, std::string byteorder)
  162. {
  163. BYTE t_sdata = 0;
  164. char t[256];
  165. std::string t_str;
  166. t_sdata = pbuf[0] & 0x00ff;
  167. snprintf(t, 256, "%d", t_sdata);
  168. t_str = t;
  169. return t_str;
  170. }
  171. inline std::string merge8_u(PBYTE pbuf, std::string byteorder)
  172. {
  173. unsigned char t_sdata = 0;
  174. char t[256];
  175. std::string t_str;
  176. t_sdata = (unsigned char) (pbuf[0] & 0x00ff);
  177. snprintf(t, 256, "%d", t_sdata);
  178. t_str = t;
  179. return t_str;
  180. }
  181. //inline std::string merge16(PBYTE pbuf, std::string byteorder)
  182. inline std::string merge16(int16_t pbuf, std::string byteorder)
  183. {
  184. short t_sdata = 0;
  185. char t[256];
  186. std::string t_str;
  187. if (byteorder == "BA")
  188. {
  189. // //t_sdata = ((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff);
  190. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  191. // snprintf(t, 256, "%d", t_sdata);
  192. //
  193. // t_str = t;
  194. // log_i("BA-merge16 t_str= %s",t_str.c_str());
  195. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  196. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  197. t_sdata = low_byte | (high_byte << 8) ;
  198. snprintf(t, 256, "%d", t_sdata);
  199. t_str = t;
  200. log_i("BA-merge16 t_str= %s", t_str.c_str());
  201. }
  202. else if (byteorder == "AB")
  203. {
  204. // t_sdata = ((pbuf << 8) & 0xff00) | (pbuf & 0x00ff);
  205. // snprintf(t, 256, "%d", t_sdata);
  206. // t_str = t;
  207. // log_i("AB-merge16 t_str= %s",t_str.c_str());
  208. uint8_t high_byte = static_cast<uint8_t>((pbuf >> 8) & 0xff);
  209. uint8_t low_byte = static_cast<uint8_t>(pbuf & 0xff);
  210. t_sdata = (low_byte << 8) | high_byte;
  211. snprintf(t, 256, "%d", t_sdata);
  212. t_str = t;
  213. log_i("AB-merge16 t_str= %s", t_str.c_str());
  214. }
  215. return t_str;
  216. }
  217. //inline std::string merge16_u(PBYTE pbuf, std::string byteorder)
  218. inline std::string merge16_u(int16_t pbuf, std::string byteorder)
  219. {
  220. unsigned short t_sdata = 0;
  221. char t[256];
  222. std::string t_str;
  223. if (byteorder == "BA")
  224. {
  225. // t_sdata = (unsigned short) (((pbuf[0] << 8) & 0xff00) | ((pbuf[1]) & 0x00ff));
  226. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  227. snprintf(t, 256, "%d", t_sdata);
  228. t_str = t;
  229. }
  230. else if (byteorder == "AB")
  231. {
  232. // t_sdata = (unsigned short) (((pbuf[1] << 8) & 0xff00) | ((pbuf[0]) & 0x00ff));
  233. t_sdata = (unsigned short) (((pbuf << 8) & 0xff00) | ((pbuf) & 0x00ff));
  234. snprintf(t, 256, "%d", t_sdata);
  235. t_str = t;
  236. }
  237. return t_str;
  238. }
  239. inline std::string merge32(PBYTE pbuf, std::string byteorder)
  240. {
  241. int t_idata = 0;
  242. char t[256];
  243. std::string t_str;
  244. if (byteorder == "BADC")
  245. {
  246. t_idata = ((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000);
  247. snprintf(t, 256, "%d", t_idata);
  248. t_str = t;
  249. }
  250. else if (byteorder == "DCBA")
  251. {
  252. t_idata = ((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff);
  253. snprintf(t, 256, "%d", t_idata);
  254. t_str = t;
  255. }
  256. else if (byteorder == "ABCD")
  257. {
  258. t_idata = ((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000);
  259. snprintf(t, 256, "%d", t_idata);
  260. t_str = t;
  261. }
  262. else if (byteorder == "CDAB")
  263. {
  264. t_idata = ((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00);
  265. snprintf(t, 256, "%d", t_idata);
  266. t_str = t;
  267. }
  268. else
  269. {
  270. }
  271. return t_str;
  272. }
  273. inline std::string merge32_u(PBYTE pbuf, std::string byteorder)
  274. {
  275. unsigned int t_idata = 0;
  276. char t[256];
  277. std::string t_str;
  278. if (byteorder == "BADC")
  279. {
  280. t_idata =
  281. (unsigned int) (((pbuf[0] << 8) & 0x0000ff00) | ((pbuf[1]) & 0x000000ff) | ((pbuf[2] << 24) & 0xff000000) | ((pbuf[3] << 16) & 0x00ff0000));
  282. snprintf(t, 256, "%d", t_idata);
  283. t_str = t;
  284. }
  285. else if (byteorder == "DCBA")
  286. {
  287. t_idata =
  288. (unsigned int) (((pbuf[0] << 24) & 0xff000000) | ((pbuf[1] << 16) & 0x00ff0000) | ((pbuf[2] << 8) & 0x0000ff00) | ((pbuf[3]) & 0x000000ff));
  289. snprintf(t, 256, "%d", t_idata);
  290. t_str = t;
  291. }
  292. else if (byteorder == "ABCD")
  293. {
  294. t_idata =
  295. (unsigned int) (((pbuf[0]) & 0x000000ff) | ((pbuf[1] << 8) & 0x0000ff00) | ((pbuf[2] << 16) & 0x00ff0000) | ((pbuf[3] << 24) & 0xff000000));
  296. snprintf(t, 256, "%d", t_idata);
  297. t_str = t;
  298. }
  299. else if (byteorder == "CDAB")
  300. {
  301. t_idata =
  302. (unsigned int) (((pbuf[0] << 16) & 0x00ff0000) | ((pbuf[1] << 24) & 0xff000000) | ((pbuf[2]) & 0x000000ff) | ((pbuf[3] << 8) & 0x0000ff00));
  303. snprintf(t, 256, "%d", t_idata);
  304. t_str = t;
  305. }
  306. else
  307. {
  308. }
  309. return t_str;
  310. }
  311. float intbitstofloat(int bits)
  312. {
  313. union
  314. {
  315. int i;
  316. float f;
  317. } u;
  318. u.i = bits;
  319. return u.f;
  320. }
  321. int floatbitstoint(float bits)
  322. {
  323. union
  324. {
  325. int i;
  326. float f;
  327. } u;
  328. u.f = bits;
  329. return u.i;
  330. }
  331. inline void initLogger()
  332. {
  333. /**
  334. * LOG系统初始化
  335. */
  336. /* close printf buffer */
  337. setbuf(stdout, NULL);
  338. /* initialize EasyLogger */
  339. elog_init();
  340. /* set EasyLogger log format */
  341. elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
  342. elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  343. elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  344. elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  345. elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  346. elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  347. #ifdef ELOG_COLOR_ENABLE
  348. elog_set_text_color_enabled(true);
  349. #endif
  350. /* start EasyLogger */
  351. elog_start();
  352. }
  353. };
  354. #endif