ProtocolModbus.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // ProtocolPMC916.cpp: implementation of the CProtocolModbus class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "ProtocolModbus.h"
  6. #include "winsock2.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CProtocolModbus::CProtocolModbus() : CProtocol()
  16. {
  17. InitializeCriticalSection( &m_csReadFinished );
  18. InitializeCriticalSection( &m_csWrFinished );
  19. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) );
  20. }
  21. CProtocolModbus::~CProtocolModbus()
  22. {
  23. DeleteCriticalSection( &m_csReadFinished );
  24. DeleteCriticalSection( &m_csWrFinished );
  25. MTVERIFY( CloseHandle( m_hSemComm ) );
  26. }
  27. int CProtocolModbus::WorkMain( int nRequestLen, //请求数据总长度
  28. int nResponseLen, //响应数据总长度
  29. int nCmd, //命令
  30. int nCmdPos, //变量索引
  31. int nCmdLen, //变量长度
  32. int nParam, //参数
  33. char chMsg[80] ) //读到变量值
  34. {
  35. if(!m_pComm)
  36. return ERR_CODE_UPS_POWERWARE_COM_FAULT; // 串口通信故障
  37. int nRet;
  38. #if 1
  39. nRet = RequestStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam);
  40. if( nRet != 0 )
  41. {
  42. return nRet; // 串口忙
  43. }
  44. nRet = ResponseStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam, chMsg);
  45. #else
  46. if( nCmd == 0x33 || nCmd == 0x34 )
  47. {
  48. nRet = ResponseStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam, chMsg);
  49. }
  50. else
  51. {
  52. nRet = -1;
  53. }
  54. #endif
  55. return nRet;
  56. }
  57. int CProtocolModbus:: WriteCommand(SETBASEPARAM SetBasePara, double dbData, int nDataLen)
  58. {
  59. int iResult = 0;
  60. // if(!m_pComm)
  61. // return ERR_CODE_MODBUS_RTU_COM_FAULT;
  62. // iResult = RequestWrStatus(SetBasePara, dbData, nDataLen);
  63. // if (iResult == 0)
  64. // return ResponseWrStatus(SetBasePara.nAddr, SetBasePara.nRegNum, nDataLen);
  65. // else
  66. return iResult;
  67. }
  68. int CProtocolModbus::ResponseStatus( int nRequestLen, //请求数据总长度
  69. int nResponseLen, //响应数据总长度
  70. int nCmd, //命令
  71. int nCmdPos, //变量索引
  72. int nCmdLen, //变量长度
  73. int nParam, //参数
  74. char chMsg[80] ) //读到变量值
  75. {
  76. RESPONSE_STRUCT tagResponse;
  77. memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
  78. int nReadLen = 0, nHeadLen = 0, nIndex = 4;
  79. int nLen;
  80. char *pTotolBuffer = NULL, *pBuffer = NULL;
  81. if( nCmd == 0x33 )
  82. {
  83. pTotolBuffer = new char[ 24 ];
  84. pBuffer = new char[ 24 ];
  85. memset(pTotolBuffer, 0, 24);
  86. memset(pBuffer, 0, 24);
  87. nLen = 24;
  88. }
  89. else if( nCmd == 0x34 )
  90. {
  91. pTotolBuffer = new char[ 147 ];
  92. pBuffer = new char[147];
  93. memset(pTotolBuffer, 0, 147);
  94. memset(pBuffer, 0, 147);
  95. nLen = 147;
  96. }
  97. int nTotalLen = 0;
  98. #if 1
  99. nReadLen = ReadMessage( (BYTE *)pTotolBuffer, nLen );
  100. if( nReadLen <= 0)
  101. {
  102. // 串口没有读到数据
  103. TRACE("串口没有读到数据!\r\n");
  104. SetEvent( m_hSemComm );
  105. if( pTotolBuffer != NULL)
  106. {
  107. delete[] pTotolBuffer;
  108. pTotolBuffer = NULL;
  109. }
  110. if( pBuffer != NULL)
  111. {
  112. delete[] pBuffer;
  113. pBuffer = NULL;
  114. }
  115. return ERR_CODE_UPS_POWERWARE_COM_READ_NO_DATA;
  116. }
  117. else if( nReadLen < nLen )
  118. {
  119. SetEvent( m_hSemComm );
  120. if( pTotolBuffer != NULL)
  121. {
  122. delete[] pTotolBuffer;
  123. pTotolBuffer = NULL;
  124. }
  125. if( pBuffer != NULL)
  126. {
  127. delete[] pBuffer;
  128. pBuffer = NULL;
  129. }
  130. return ERR_CODE_UPS_POWERWARE_COM_READ_NO_DATA;
  131. }
  132. else if( nReadLen > nLen )
  133. {
  134. SetEvent( m_hSemComm );
  135. if( pTotolBuffer != NULL)
  136. {
  137. delete[] pTotolBuffer;
  138. pTotolBuffer = NULL;
  139. }
  140. if( pBuffer != NULL)
  141. {
  142. delete[] pBuffer;
  143. pBuffer = NULL;
  144. }
  145. return ERR_CODE_UPS_POWERWARE_COM_READ_LEN_OVER;
  146. }
  147. #else
  148. //AB 03 13 81 F0 1B 00 10 05 01 00 00 00 00 00 01
  149. //00 00 00 00 00 00 00 9C
  150. if( nCmd == 0x33 )
  151. {
  152. pTotolBuffer[0] = 0xAB;
  153. pTotolBuffer[1] = 0x03;
  154. pTotolBuffer[2] = 0x13;
  155. pTotolBuffer[3] = 0x81;
  156. pTotolBuffer[4] = 0xF0;
  157. pTotolBuffer[5] = 0x1B;
  158. pTotolBuffer[6] = 0x00;
  159. pTotolBuffer[7] = 0x10;
  160. pTotolBuffer[8] = 0x05;
  161. pTotolBuffer[9] = 0x01;
  162. pTotolBuffer[10] = 0x00;
  163. pTotolBuffer[11] = 0x00;
  164. pTotolBuffer[12] = 0x00;
  165. pTotolBuffer[13] = 0x00;
  166. pTotolBuffer[14] = 0x00;
  167. pTotolBuffer[15] = 0x01;
  168. pTotolBuffer[16] = 0x00;
  169. pTotolBuffer[17] = 0x00;
  170. pTotolBuffer[18] = 0x00;
  171. pTotolBuffer[19] = 0x00;
  172. pTotolBuffer[20] = 0x00;
  173. pTotolBuffer[21] = 0x00;
  174. pTotolBuffer[22] = 0x00;
  175. pTotolBuffer[23] = 0x9C;
  176. }
  177. else if( nCmd == 0x34 )
  178. {
  179. //AB 04 40 01 00 00 D0 40 00 00 D0 40 00 00 D0 40 ?.@...?@..?@..?@
  180. //00 00 00 00 00 80 3B 45 00 00 00 00 00 60 86 45 .....?;E.....`?E
  181. //00 00 00 00 00 A3 30 3F 00 00 48 42 00 00 48 42 .....?0?..HB..HB
  182. //AB 04 40 01 C0 CC 04 41 C0 CC 04 41 C0 CC 04 41
  183. //00 00 00 00 00 E0 92 45 00 00 00 00 00 00 AF 45
  184. //00 00 00 00 00 7A 54 3F 00 00 48 42 00 00 48 42
  185. pTotolBuffer[0] = 0xAB;
  186. pTotolBuffer[1] = 0x04;
  187. pTotolBuffer[2] = 0x40;
  188. pTotolBuffer[3] = 0x01;
  189. pTotolBuffer[4] = 0xC0;
  190. pTotolBuffer[5] = 0xCC;
  191. pTotolBuffer[6] = 0x04;
  192. pTotolBuffer[7] = 0x41;
  193. pTotolBuffer[8] = 0xC0;
  194. pTotolBuffer[9] = 0xCC;
  195. pTotolBuffer[10] = 0x04;
  196. pTotolBuffer[11] = 0x41;
  197. pTotolBuffer[12] = 0xC0;
  198. pTotolBuffer[13] = 0xCC;
  199. pTotolBuffer[14] = 0x04;
  200. pTotolBuffer[15] = 0x41;
  201. pTotolBuffer[16] = 0x00;
  202. pTotolBuffer[17] = 0x00;
  203. pTotolBuffer[18] = 0x00;
  204. pTotolBuffer[19] = 0x00;
  205. pTotolBuffer[20] = 0x00;
  206. pTotolBuffer[21] = 0xE0;
  207. pTotolBuffer[22] = 0x92;
  208. pTotolBuffer[23] = 0x45;
  209. pTotolBuffer[24] = 0x00;
  210. pTotolBuffer[25] = 0x00;
  211. pTotolBuffer[26] = 0x00;
  212. pTotolBuffer[27] = 0x00;
  213. pTotolBuffer[28] = 0x00;
  214. pTotolBuffer[29] = 0x00;
  215. pTotolBuffer[30] = 0xAF;
  216. pTotolBuffer[31] = 0x45;
  217. pTotolBuffer[32] = 0x00;
  218. pTotolBuffer[33] = 0x00;
  219. pTotolBuffer[34] = 0x00;
  220. pTotolBuffer[35] = 0x00;
  221. pTotolBuffer[36] = 0x00;
  222. pTotolBuffer[37] = 0x7A;
  223. pTotolBuffer[38] = 0x54;
  224. pTotolBuffer[39] = 0x3F;
  225. pTotolBuffer[40] = 0x00;
  226. pTotolBuffer[41] = 0x00;
  227. pTotolBuffer[42] = 0x48;
  228. pTotolBuffer[43] = 0x42;
  229. pTotolBuffer[44] = 0x00;
  230. pTotolBuffer[45] = 0x00;
  231. pTotolBuffer[46] = 0x48;
  232. pTotolBuffer[47] = 0x42;
  233. //98 99 47 42 00 66 66 BF 66 86 0A 44 00 00 C6 42 ??GB.ff?f?.D..?B
  234. //F7 70 00 00 A1 AB 04 40 02 33 B3 60 43 00 00 60 ÷p..??.@.3?`C..`
  235. //43 33 33 61 43 CC 4C 60 43 CC 4C 60 43 66 66 61 C33aC?L`C?L`Cffa
  236. //43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4 C..............?
  237. //42 00 00 E4 42 00 00 E4 42 22 01 10 A0 48 36 43 B..?B..?B"..?H6C
  238. //15 00 00 63 43 99 19 62 43 DB AB 04 04 83 66 66 ...cC?.bC??..?ff
  239. //61 43 5A
  240. //00 00 48 42 00 99 19 3F 33 F3 05 44 00 00 C6 42
  241. //F7 70 00 00 78 AB 04 40 02 99 19 65 43 33 B3 64
  242. pTotolBuffer[48] = 0x00;
  243. pTotolBuffer[49] = 0x00;
  244. pTotolBuffer[50] = 0x48;
  245. pTotolBuffer[51] = 0x42;
  246. pTotolBuffer[52] = 0x00;
  247. pTotolBuffer[53] = 0x99;
  248. pTotolBuffer[54] = 0x19;
  249. pTotolBuffer[55] = 0x3F;
  250. pTotolBuffer[56] = 0x33;
  251. pTotolBuffer[57] = 0xF3;
  252. pTotolBuffer[58] = 0x05;
  253. pTotolBuffer[59] = 0x44;
  254. pTotolBuffer[60] = 0x00;
  255. pTotolBuffer[61] = 0x00;
  256. pTotolBuffer[62] = 0xC6;
  257. pTotolBuffer[63] = 0x42;
  258. pTotolBuffer[64] = 0xF0;
  259. pTotolBuffer[65] = 0x70;
  260. pTotolBuffer[66] = 0x00;
  261. pTotolBuffer[67] = 0x00;
  262. pTotolBuffer[68] = 0x78;
  263. pTotolBuffer[69] = 0xAB;
  264. pTotolBuffer[70] = 0x04;
  265. pTotolBuffer[71] = 0x40;
  266. pTotolBuffer[72] = 0x02;
  267. pTotolBuffer[73] = 0x99;
  268. pTotolBuffer[74] = 0x19;
  269. pTotolBuffer[75] = 0x65;
  270. pTotolBuffer[76] = 0x43;
  271. pTotolBuffer[77] = 0x33;
  272. pTotolBuffer[78] = 0xB3;
  273. pTotolBuffer[79] = 0x64;
  274. //43 66 66 65 43 66 E6 64 43 99 19 65 43 33 B3 64
  275. //43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4
  276. pTotolBuffer[80] = 0x43;
  277. pTotolBuffer[81] = 0x66;
  278. pTotolBuffer[82] = 0x66;
  279. pTotolBuffer[83] = 0x65;
  280. pTotolBuffer[84] = 0x43;
  281. pTotolBuffer[85] = 0x66;
  282. pTotolBuffer[86] = 0xE6;
  283. pTotolBuffer[87] = 0x64;
  284. pTotolBuffer[88] = 0x43;
  285. pTotolBuffer[89] = 0x99;
  286. pTotolBuffer[90] = 0x19;
  287. pTotolBuffer[91] = 0x65;
  288. pTotolBuffer[92] = 0x43;
  289. pTotolBuffer[93] = 0x33;
  290. pTotolBuffer[94] = 0xB3;
  291. pTotolBuffer[95] = 0x64;
  292. pTotolBuffer[96] = 0x43;
  293. pTotolBuffer[97] = 0x00;
  294. pTotolBuffer[98] = 0x00;
  295. pTotolBuffer[99] = 0x00;
  296. pTotolBuffer[100] = 0x00;
  297. pTotolBuffer[101] = 0x00;
  298. pTotolBuffer[102] = 0x00;
  299. pTotolBuffer[103] = 0x00;
  300. pTotolBuffer[104] = 0x00;
  301. pTotolBuffer[105] = 0x00;
  302. pTotolBuffer[106] = 0x00;
  303. pTotolBuffer[107] = 0x00;
  304. pTotolBuffer[108] = 0x00;
  305. pTotolBuffer[109] = 0x00;
  306. pTotolBuffer[110] = 0x00;
  307. pTotolBuffer[111] = 0xE4;
  308. //42 00 00 E4 42 00 00 E4 42 06 02 10 A0 31 48 59
  309. //18 99 19 62 43 00 00 61 43 CB AB 04 04 83 00 00
  310. //60 43 27
  311. pTotolBuffer[112] = 0x42;
  312. pTotolBuffer[113] = 0x00;
  313. pTotolBuffer[114] = 0x00;
  314. pTotolBuffer[115] = 0xE4;
  315. pTotolBuffer[116] = 0x42;
  316. pTotolBuffer[117] = 0x00;
  317. pTotolBuffer[118] = 0x00;
  318. pTotolBuffer[119] = 0xE4;
  319. pTotolBuffer[120] = 0x42;
  320. pTotolBuffer[121] = 0x06;
  321. pTotolBuffer[122] = 0x02;
  322. pTotolBuffer[123] = 0x10;
  323. pTotolBuffer[124] = 0xA0;
  324. pTotolBuffer[125] = 0x31;
  325. pTotolBuffer[126] = 0x48;
  326. pTotolBuffer[127] = 0x59;
  327. pTotolBuffer[128] = 0x18;
  328. pTotolBuffer[129] = 0x99;
  329. pTotolBuffer[130] = 0x19;
  330. pTotolBuffer[131] = 0x62;
  331. pTotolBuffer[132] = 0x43;
  332. pTotolBuffer[133] = 0x00;
  333. pTotolBuffer[134] = 0x00;
  334. pTotolBuffer[135] = 0x61;
  335. pTotolBuffer[136] = 0x43;
  336. pTotolBuffer[137] = 0xCB;
  337. pTotolBuffer[138] = 0xAB;
  338. pTotolBuffer[139] = 0x04;
  339. pTotolBuffer[140] = 0x04;
  340. pTotolBuffer[141] = 0x83;
  341. pTotolBuffer[142] = 0x00;
  342. pTotolBuffer[143] = 0x00;
  343. pTotolBuffer[144] = 0x60;
  344. pTotolBuffer[145] = 0x43;
  345. pTotolBuffer[146] = 0x27;
  346. }
  347. #endif
  348. try
  349. {
  350. memcpy( &tagResponse, pTotolBuffer, sizeof(RESPONSE_STRUCT) );
  351. int nTempLen = tagResponse.chLen & 0x000000FF;
  352. if( nCmd == 0x33 )
  353. {
  354. // 判断校验
  355. char chCheckSum;
  356. GetCheckSum(pTotolBuffer, chCheckSum, nLen);
  357. if ( !(chCheckSum = pTotolBuffer[nLen -1]) )
  358. {
  359. //TRACE("校验失败!\r\n");
  360. if( pTotolBuffer != NULL)
  361. {
  362. delete[] pTotolBuffer;
  363. pTotolBuffer = NULL;
  364. }
  365. if( pBuffer != NULL)
  366. {
  367. delete[] pBuffer;
  368. pBuffer = NULL;
  369. }
  370. SetEvent( m_hSemComm );
  371. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  372. }
  373. if( nTempLen == 0x13 )
  374. {
  375. memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen );
  376. }
  377. }
  378. else if( nCmd == 0x34 )
  379. {
  380. if( nTempLen == 0x40 )
  381. {
  382. // 判断校验
  383. char chCheckSum;
  384. GetCheckSum(pTotolBuffer, chCheckSum, nTempLen + nIndex + 1);
  385. if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex]) )
  386. {
  387. //TRACE("校验失败!\r\n");
  388. if( pTotolBuffer != NULL)
  389. {
  390. delete[] pTotolBuffer;
  391. pTotolBuffer = NULL;
  392. }
  393. if( pBuffer != NULL)
  394. {
  395. delete[] pBuffer;
  396. pBuffer = NULL;
  397. }
  398. SetEvent( m_hSemComm );
  399. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  400. }
  401. if( nTotalLen + nTempLen >= nLen || nIndex + nTempLen >= nLen )
  402. {
  403. if( pTotolBuffer != NULL)
  404. {
  405. delete[] pTotolBuffer;
  406. pTotolBuffer = NULL;
  407. }
  408. if( pBuffer != NULL)
  409. {
  410. delete[] pBuffer;
  411. pBuffer = NULL;
  412. }
  413. SetEvent( m_hSemComm );
  414. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  415. }
  416. memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
  417. nTotalLen += nTempLen;
  418. nIndex += nTempLen;
  419. nIndex += 1; //去掉尾
  420. #if 0
  421. // 判断校验
  422. GetCheckSum(pTotolBuffer + nIndex, chCheckSum, nTempLen + nIndex + 4 + 1);
  423. if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex + 4]) )
  424. {
  425. //TRACE("校验失败!\r\n");
  426. AfxMessageBox("校验失败!\r\n");
  427. if( pTotolBuffer != NULL)
  428. {
  429. delete[] pTotolBuffer;
  430. pTotolBuffer = NULL;
  431. }
  432. if( pBuffer != NULL)
  433. {
  434. delete[] pBuffer;
  435. pBuffer = NULL;
  436. }
  437. SetEvent( m_hSemComm );
  438. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  439. }
  440. #endif
  441. memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
  442. if( nIndex + sizeof(RESPONSE_STRUCT) >= nLen )
  443. {
  444. if( pTotolBuffer != NULL)
  445. {
  446. delete[] pTotolBuffer;
  447. pTotolBuffer = NULL;
  448. }
  449. if( pBuffer != NULL)
  450. {
  451. delete[] pBuffer;
  452. pBuffer = NULL;
  453. }
  454. SetEvent( m_hSemComm );
  455. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  456. }
  457. memcpy( &tagResponse, pTotolBuffer + nIndex, sizeof(RESPONSE_STRUCT) );
  458. nTempLen = tagResponse.chLen & 0x000000FF;
  459. nIndex += sizeof(RESPONSE_STRUCT); //去掉头
  460. if( nTempLen == 0x40 )
  461. {
  462. if( nTotalLen + nTempLen >= nLen || nIndex + nTempLen >= nLen )
  463. {
  464. if( pTotolBuffer != NULL)
  465. {
  466. delete[] pTotolBuffer;
  467. pTotolBuffer = NULL;
  468. }
  469. if( pBuffer != NULL)
  470. {
  471. delete[] pBuffer;
  472. pBuffer = NULL;
  473. }
  474. SetEvent( m_hSemComm );
  475. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  476. }
  477. memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
  478. nTotalLen += nTempLen;
  479. nIndex += nTempLen;
  480. nIndex += 1; //去掉尾
  481. #if 0
  482. // 判断校验
  483. GetCheckSum(pTotolBuffer + nIndex, chCheckSum, nTempLen + nIndex + 4 + 1);
  484. if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex + 4]) )
  485. {
  486. //TRACE("校验失败!\r\n");
  487. AfxMessageBox("校验失败!\r\n");
  488. if( pTotolBuffer != NULL)
  489. {
  490. delete[] pTotolBuffer;
  491. pTotolBuffer = NULL;
  492. }
  493. if( pBuffer != NULL)
  494. {
  495. delete[] pBuffer;
  496. pBuffer = NULL;
  497. }
  498. SetEvent( m_hSemComm );
  499. return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
  500. }
  501. #endif
  502. memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
  503. if( nIndex + sizeof(RESPONSE_STRUCT) < nLen )
  504. {
  505. memcpy( &tagResponse, pTotolBuffer + nIndex, sizeof(RESPONSE_STRUCT) );
  506. }
  507. nTempLen = tagResponse.chLen & 0x000000FF;
  508. nIndex += sizeof(RESPONSE_STRUCT); //去掉头
  509. if( nTempLen == 0x04 )
  510. {
  511. if( nTotalLen + nTempLen < nLen && nIndex + nTempLen < nLen )
  512. {
  513. memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
  514. }
  515. }
  516. }
  517. }
  518. }
  519. //取变量数据
  520. EnterCriticalSection( &m_csWrFinished );
  521. for( int i = 0; i < nCmdLen; i++ )
  522. {
  523. if( nCmdPos + i >= nLen ) break;
  524. chMsg[i] = pBuffer[nCmdPos + i];
  525. }
  526. LeaveCriticalSection(&m_csWrFinished);
  527. }
  528. catch(...)
  529. {
  530. if( pTotolBuffer != NULL)
  531. {
  532. delete[] pTotolBuffer;
  533. pTotolBuffer = NULL;
  534. }
  535. if( pBuffer != NULL)
  536. {
  537. delete[] pBuffer;
  538. pBuffer = NULL;
  539. }
  540. // 设置串口等待事件为有信号
  541. SetEvent( m_hSemComm );
  542. return -1;
  543. }
  544. if( pTotolBuffer != NULL)
  545. {
  546. delete[] pTotolBuffer;
  547. pTotolBuffer = NULL;
  548. }
  549. if( pBuffer != NULL)
  550. {
  551. delete[] pBuffer;
  552. pBuffer = NULL;
  553. }
  554. // 设置串口等待事件为有信号
  555. SetEvent( m_hSemComm );
  556. return 0;
  557. }
  558. int CProtocolModbus::RequestStatus( int nRequestLen, //请求数据总长度
  559. int nResponseLen, //响应数据总长度
  560. int nCmd, //命令
  561. int nCmdPos, //变量索引
  562. int nCmdLen, //变量长度
  563. int nParam ) //参数
  564. {
  565. int nLen = sizeof(REQUESTPARAM);
  566. REQUESTPARAM RequestPara;
  567. memset( &RequestPara, 0, nLen );
  568. //起始位
  569. RequestPara.chSoi = 0xAB;
  570. //请求长度
  571. RequestPara.chLen = (BYTE)nRequestLen;
  572. //命令
  573. RequestPara.chCmd = (BYTE)nCmd;
  574. char chCheckSum;
  575. GetCheckSum((char *)&RequestPara, chCheckSum, nLen );
  576. RequestPara.chCheckSum = chCheckSum;
  577. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  578. {
  579. ResetEvent( m_hSemComm );
  580. int nResult = WriteMessage( (BYTE *)&RequestPara, nLen );
  581. if( nResult == nLen )
  582. {
  583. }
  584. else
  585. {
  586. SetEvent( m_hSemComm );
  587. return EER_CODE_UPS_POWERWARE_COM_WRITE_DATA;
  588. }
  589. }
  590. else
  591. {
  592. return ERR_CODE_UPS_POWERWARE_COM_BUSY;
  593. }
  594. return 0;
  595. }
  596. void CProtocolModbus::GetCheckSum(char *pBuf, char &chDest, int len)
  597. {
  598. WORD iSum = 0;
  599. for(int i=0; i<len; i++)//求和
  600. {
  601. iSum += pBuf[i];
  602. }
  603. WORD iCompliment = iSum;
  604. iCompliment = ~iCompliment;//取反
  605. iCompliment++;
  606. chDest = (BYTE)iCompliment;
  607. }
  608. BOOL CProtocolModbus::InitParam(PPORTPARAM pPortParam, CCommAsyn *pComm)
  609. {
  610. int addr=pPortParam->StartAddr;
  611. m_pComm=pComm;
  612. return TRUE;
  613. }
  614. int DigitToBinary(WORD wdSource, char* pDes, int iBit)
  615. {
  616. char pTmpBuf[16] = {0};
  617. char chBuffer[16] = {0};
  618. //wdSource =htonl(wdSource);
  619. itoa(wdSource, pTmpBuf, 2);
  620. int iLen = (int)strlen(pTmpBuf) - 1;
  621. char chValue[16] = {0};
  622. strcpy(chValue, pTmpBuf);
  623. for (int i =0; i<=iLen; i++)
  624. {
  625. pTmpBuf[i] = chValue[iLen - i];
  626. }
  627. for (int k = 0; k<iBit; k++)
  628. {
  629. if ( 0x00 == pTmpBuf[iBit - k - 1])
  630. chBuffer[k] = 0x30;
  631. else
  632. chBuffer[k] = pTmpBuf[iBit - k - 1];
  633. }
  634. memcpy(pDes, chBuffer, iBit);
  635. return 0;
  636. }
  637. void strReverse( char *str )
  638. {
  639. int l = (int)strlen(str);
  640. for( int i = 0; i < l; i++ )
  641. {
  642. for(int i = 0; i < l; i++)
  643. {
  644. if( str[i] >= 'A' && str[i] <= 'Z' )
  645. {
  646. str[i] += 32;
  647. }
  648. else if(str[i] >= 'a' && str[i] <= 'z')
  649. {
  650. str[i] -= 32;
  651. }
  652. }
  653. }
  654. }
  655. char lowercase2uppercase(BYTE btSrc)
  656. {
  657. if( btSrc >= 'a' && btSrc <= 'z' )
  658. {
  659. return btSrc - 'a' + 'A';
  660. }
  661. return btSrc;
  662. }
  663. char ByteToAscii(BYTE btSrc)
  664. {
  665. char chDest;
  666. if( btSrc < 10 )
  667. {
  668. chDest = (char)(btSrc % 10 + '0');
  669. chDest = lowercase2uppercase(chDest);
  670. return chDest;
  671. }
  672. else
  673. {
  674. chDest = ByteToAscii( btSrc / 10 ) + (char)( btSrc % 10 + '0' );
  675. chDest = lowercase2uppercase(chDest);
  676. return chDest;
  677. }
  678. }
  679. WORD AsciiToBYTE(BYTE btSrc)
  680. {
  681. WORD chDest = (WORD)btSrc;
  682. if ((btSrc >= 'A')&&(btSrc <= 'F'))
  683. {
  684. chDest = chDest - 'A' + 10;
  685. }
  686. else if ((btSrc >= 'a')&&(btSrc <= 'f'))
  687. {
  688. chDest = chDest - 'a' + 10;
  689. }
  690. else if ((btSrc >= '0')&&(btSrc <= '9'))
  691. {
  692. chDest -= '0';
  693. }
  694. return chDest;
  695. }
  696. /*
  697. Convert a binary data buffer to a hex string
  698. str: output (a string like “20ef9a“)
  699. bin: input (a data buffer)
  700. binlen: input (the length of the data buffer)
  701. */
  702. void bin2str(char *str, const unsigned char *bin, int binlen)
  703. {
  704. int i;
  705. for(i=0; i<binlen; i++)
  706. {
  707. str[2*i] = ((bin[i] & 0xF0) >> 4);
  708. str[2*i] = (str[2*i] > 9) ? (str[2*i] + 'A' - 10) : (str[2*i] + '0');
  709. str[2*i+1] = (bin[i] & 0x0F);
  710. str[2*i+1] = (str[2*i+1] > 9) ? (str[2*i+1] + 'A' - 10) : (str[2*i+1] + '0');
  711. }
  712. str[2*i] = '\0';
  713. }