协能can协议
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ProtocolImpl.h 10KB

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