BatteryAdu.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: BatteryAdu.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: ADU1000协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-11-23 //////
  9. ////// 修改说明:数据转换 //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include "BatteryAdu.h"
  15. CBatteryAdu::CBatteryAdu( char szPath[MAX_PATH],
  16. char szIniName[MAX_PATH],
  17. int nCommPort, int nAddr,
  18. int nRate, int nDataBit,
  19. int nStopBit, int nParity,
  20. int nInterval )
  21. {
  22. #if IS_USE_READMSG_CS
  23. InitializeCriticalSection(&m_csReadMsg);
  24. #endif
  25. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  26. for( int i = 0; i < MAX_ADDR; i++ )
  27. {
  28. memset(m_Adu_41Msg[i], 0, sizeof(m_Adu_41Msg[i]));
  29. m_devOnline[i] = TRUE;
  30. m_dwOnlineTick[i] = 0;
  31. }
  32. }
  33. CBatteryAdu::~CBatteryAdu()
  34. {
  35. #if IS_USE_READMSG_CS
  36. DeleteCriticalSection(& m_csReadMsg);
  37. #endif
  38. MTVERIFY(CloseHandle(m_hSemComm));
  39. CloseComm();
  40. }
  41. BOOL CBatteryAdu::AduOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  42. {
  43. BOOL bResult = FALSE;
  44. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  45. return bResult;
  46. }
  47. int CBatteryAdu::SendReadRequest(
  48. char szPath[MAX_PATH],
  49. char szIniName[MAX_PATH],
  50. int nCommPort,
  51. int nAddr,
  52. char szCmd[MAX_CMD],
  53. char szMsg[VAR_MSG],
  54. int nReversed1,
  55. int nReversed2,
  56. int nReversed3,
  57. int nReversed4,
  58. int nReversed5,
  59. float fReversed1,
  60. float fReversed2,
  61. float fReversed3,
  62. char szReversed1[MAX_RESERVED1],
  63. char szReversed2[MAX_RESERVED2],
  64. char szReversed3[MAX_RESERVED3],
  65. char szReversed4[MAX_RESERVED4],
  66. char szReversed5[MAX_RESERVED5]
  67. )
  68. {
  69. int nIndex = 0, nLen = 0;
  70. char szSendMsg[ADU_SEND_MSG] = {0};
  71. char szType[ADU_TYPE_LENGTH] = {0};
  72. int startBit = 0;
  73. int endBit = 0;
  74. GetAduFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  75. CCommProcess *pComm = FindComm(nCommPort);
  76. if( pComm == NULL ) return -1;
  77. int nRet = -1;
  78. if( ( strlen(m_Adu_41Msg[nAddr-1]) == 0 &&
  79. (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  80. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  81. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  82. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  83. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  84. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  85. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  86. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  87. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  88. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  89. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  90. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  91. strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  92. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
  93. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  94. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  95. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  96. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  97. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  98. strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  99. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
  100. strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
  101. strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  102. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
  103. strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
  104. strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  105. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
  106. strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
  107. strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
  108. strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
  109. strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0) ) ||//获取监测模块
  110. strcmp(szCmd, "cmd-3") == 0 ||
  111. strcmp(szCmd, "cmd-59") == 0 ||
  112. strcmp(szCmd, "cmd-61") == 0 )
  113. {
  114. nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  115. if( nRet != 0 ) return nRet;
  116. }
  117. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  118. {
  119. m_devOnline[nAddr - 1] = FALSE;
  120. }
  121. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  122. {
  123. m_devOnline[nAddr - 1] = TRUE;
  124. }
  125. if( m_devOnline[nAddr - 1] == FALSE )
  126. {
  127. return -1;
  128. }
  129. nRet = GetAdu_Q41VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  130. //LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d ,return nRet = %d", szCmd, szMsg,nIndex, nRet));
  131. return nRet;
  132. }
  133. int CBatteryAdu::GetAdu_Q41VarMsg( int nAddr, //设备地址
  134. char szCmd[MAX_CMD], // 命令
  135. char szMsg[VAR_MSG], // 接收Buffer
  136. int &nIndex, // 变量索引,针对接收Buffer而言
  137. int &nLen, // 变量长度
  138. char szType[TYPE_LENGTH], // 变量数据类型
  139. int startBit,
  140. int endBit )
  141. {
  142. int nRet = 0;
  143. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  144. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  145. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  146. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  147. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  148. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  149. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  150. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  151. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  152. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  153. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  154. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  155. strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  156. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
  157. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  158. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  159. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  160. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  161. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  162. strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  163. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
  164. strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
  165. strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  166. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
  167. strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
  168. strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  169. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
  170. strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
  171. strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
  172. strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
  173. strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0)
  174. {
  175. #if IS_USE_READMSG_CS
  176. EnterCriticalSection(&m_csReadMsg);
  177. #endif
  178. DataConversion(szType, m_Adu_41Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
  179. //LOG4C((LOG_NOTICE," m_Adu_41Msg = %s ,szMsg = %s",m_Adu_41Msg[nAddr - 1], szMsg));
  180. #if IS_USE_READMSG_CS
  181. LeaveCriticalSection(&m_csReadMsg);
  182. #endif
  183. nRet = 0;
  184. }
  185. return nRet;
  186. }
  187. int CBatteryAdu::GetDeviceParam(int nAddr,
  188. CCommProcess *pComm, //串口对象指针
  189. char szSendMsg[ADU_SEND_MSG], //发送Buffer
  190. char szCmd[MAX_CMD], // 命令
  191. char szMsg[VAR_MSG], // 接收Buffer
  192. int &nIndex, // 变量索引,针对接收Buffer而言
  193. int &nLen, // 变量长度
  194. char szType[TYPE_LENGTH] // 变量数据类型
  195. )
  196. {
  197. int nRet = -1;
  198. nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg);
  199. if( nRet != 0 )
  200. {
  201. return nRet; // 串口忙
  202. }
  203. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  204. return nRet;
  205. }
  206. int CBatteryAdu::GetAduFromIni(char szPath[MAX_PATH], //服务器程序所在目录
  207. char szIniName[MAX_PATH], //配置文件名
  208. char szCmd[MAX_CMD], //命令
  209. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  210. char nType[MAX_CMD],
  211. int &nIndex,
  212. int &nLen,
  213. int &startBit,
  214. int &endBit)
  215. {
  216. CHAR szFile[MAX_PATH + 1] = "";
  217. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  218. TRACE("szFile =%s\n",szFile);
  219. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
  220. szSanTakSendMsg[strlen(szSanTakSendMsg)] = '\0';
  221. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  222. nType[strlen(nType)] = '\0';
  223. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  224. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  225. TRACE("GetUhkFromIniszPath=%s\nszIniName=%s\nszCmd=%s\nzSanTakSendMsg=%s\nsType=%s\nnIndex=%d\nnLen = %d\n",
  226. szPath,szIniName,szCmd,szSanTakSendMsg,nType,nIndex,nLen);
  227. return 0;
  228. }
  229. int CBatteryAdu::RequestStatus( int nAddr,
  230. char szCmd[MAX_CMD], // 命令
  231. CCommProcess *pComm, //串口对象指针
  232. char chSendMsg[ADU_SEND_MSG] //发送Buffer
  233. )
  234. {
  235. int nRet = -1;
  236. #if DEBUG_ADU
  237. int iLen = sizeof(REQUEST_STRUCT);
  238. char chLength[4] = {0};
  239. char chChkSum[5] = {0};
  240. REQUEST_STRUCT RequestPara;
  241. memset( &RequestPara, 0, iLen );
  242. //起始位
  243. RequestPara.headMes.Start = 0x7E;
  244. //通讯协议版本
  245. RequestPara.headMes.Version[0] = 0x32;
  246. RequestPara.headMes.Version[1] = 0x30;
  247. //设备地址描述
  248. RequestPara.headMes.Address[0] = ByteToAscii((nAddr>>4) & 0x0f);
  249. RequestPara.headMes.Address[1] = ByteToAscii(nAddr & 0x0f);
  250. //Cid1
  251. RequestPara.headMes.Cid1[0] = 0x34;
  252. RequestPara.headMes.Cid1[1] = 0x36;
  253. //Cid2
  254. memcpy(RequestPara.headMes.Cid2, chSendMsg, 2);
  255. RequestPara.headMes.Lenth[1] = 0x30;
  256. RequestPara.headMes.Lenth[2] = 0x30;
  257. RequestPara.headMes.Lenth[3] = 0x32;
  258. RequestPara.headMes.Lenth[0] = ByteToAscii(GetLengthSum(RequestPara.headMes.Lenth));
  259. RequestPara.headMes.GroupInfo[0] = 0x30;
  260. RequestPara.headMes.GroupInfo[1] = 0x31;
  261. //校验码
  262. BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ];
  263. memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 );
  264. memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 );
  265. GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 );
  266. RequestPara.chkSum[0] = chChkSum[0];
  267. RequestPara.chkSum[1] = chChkSum[1];
  268. RequestPara.chkSum[2] = chChkSum[2];
  269. RequestPara.chkSum[3] = chChkSum[3];
  270. //结束符
  271. RequestPara.End = 0x0D;
  272. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  273. {
  274. int nDataLen = (int)sizeof(RequestPara);
  275. ResetEvent( m_hSemComm );
  276. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  277. if( nResult == nDataLen )
  278. {
  279. nRet = 0;
  280. }
  281. else
  282. {
  283. delete []pDataBuf;
  284. SetEvent( m_hSemComm );
  285. return EER_CODE_ADU_COM_WRITE_DATA;
  286. }
  287. }
  288. else
  289. {
  290. //LOG4C((LOG_NOTICE, "m_hSemComm no single"));
  291. delete []pDataBuf;
  292. return ERR_CODE_ADU_COM_BUSY;
  293. }
  294. delete[] pDataBuf;
  295. #endif
  296. return 0;
  297. }
  298. int CBatteryAdu::ResponseStatus(
  299. int nAddr, // 设备地址
  300. CCommProcess *pComm, //串口对象指针
  301. char szCmd[MAX_CMD], // 命令
  302. char szMsg[VAR_MSG], // 接收Buffer
  303. int &nIndex, // 变量索引,针对接收Buffer而言
  304. int &nLen, // 变量长度
  305. char szType[TYPE_LENGTH] // 变量数据类型
  306. )
  307. {
  308. #if DEBUG_ADU
  309. int nReceiveLen = 0;
  310. int nProcessLen = 0;
  311. int nReadLen = 0;
  312. RESPONSE_STRUCT structResponse;
  313. memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) );
  314. nReceiveLen = sizeof(RESPONSE_STRUCT);
  315. char *pBuffer = new char[ nReceiveLen ];
  316. memset(pBuffer, 0, nReceiveLen);
  317. nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen);
  318. ///LOG4C((LOG_NOTICE," nReadLen = %d ,pBuffer = %s",nReadLen,pBuffer));
  319. if( nReadLen <= 0)
  320. {
  321. // 串口没有读到数据
  322. //TRACE("串口没有读到数据!\r\n");
  323. SetEvent( m_hSemComm );
  324. if( pBuffer != NULL)
  325. {
  326. delete[] pBuffer;
  327. pBuffer = NULL;
  328. }
  329. return ERR_CODE_ADU_COM_READ_NO_DATA;
  330. }
  331. if (LengthCheck(pBuffer) != 0)
  332. {
  333. SetEvent(m_hSemComm);
  334. TRACE("长度校验出错");
  335. if (pBuffer != NULL)
  336. {
  337. delete [] pBuffer;
  338. pBuffer = NULL;
  339. }
  340. return ERR_CODE_RTN_LCHKSUM_ERROR;
  341. }
  342. if (!ChkSumCheck(pBuffer, nReadLen))
  343. {
  344. TRACE("校验码校验出错");
  345. SetEvent(m_hSemComm);
  346. if (pBuffer != NULL)
  347. {
  348. delete [] pBuffer;
  349. pBuffer = NULL;
  350. }
  351. return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
  352. }
  353. if (RtnCheck(pBuffer) != 0)
  354. {
  355. TRACE("RTN校验出错");
  356. SetEvent(m_hSemComm);
  357. if (pBuffer != NULL)
  358. {
  359. delete [] pBuffer;
  360. pBuffer = NULL;
  361. }
  362. return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
  363. }
  364. SetAduQ41VarMsg(nAddr, szCmd, pBuffer);
  365. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  366. // 设置串口等待事件为有信号
  367. SetEvent(m_hSemComm);
  368. if( pBuffer != NULL)
  369. {
  370. delete[] pBuffer;
  371. pBuffer = NULL;
  372. }
  373. #else
  374. SimulationCommData(nAddr);
  375. #endif
  376. return 0;
  377. }
  378. void CBatteryAdu::SetAduQ41VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
  379. {
  380. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  381. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  382. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  383. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  384. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  385. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  386. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  387. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  388. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  389. strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
  390. strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  391. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
  392. strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  393. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
  394. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  395. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  396. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  397. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  398. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  399. strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  400. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
  401. strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
  402. strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  403. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
  404. strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
  405. strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  406. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
  407. strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
  408. strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
  409. strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
  410. strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0)
  411. {
  412. #if IS_USE_READMSG_CS
  413. EnterCriticalSection(&m_csReadMsg);
  414. #endif
  415. memcpy(m_Adu_41Msg[nAddr - 1], pBuffer, sizeof(m_Adu_41Msg[nAddr - 1]));
  416. #if IS_USE_READMSG_CS
  417. LeaveCriticalSection(&m_csReadMsg);
  418. #endif
  419. }
  420. }
  421. void CBatteryAdu::SimulationCommData(int nAddr)
  422. {
  423. /*获取模拟量量化数据浮点数*/
  424. // 7E 32 31 30 30 32 41 30 30 37
  425. // 30 39 30 30 30 36 36 36 36 36
  426. // 44 34 33 36 36 36 36 36 44 34
  427. // 33 36 36 36 36 37 30 34 33 36
  428. // 36 36 36 35 43 34 33 20 20 20
  429. // 20 20 20 20 20 20 20 20 20 20
  430. // 20 20 20 43 44 43 43 34 43 34 ``
  431. // 30 20 20 20 20 20 20 20 20 20
  432. // 20 20 20 20 20 20 20 39 41 31
  433. // 39 38 36 34 33 37 42 31 34 34
  434. // 38 34 32 30 30 30 30 30 36 37
  435. // 42 31 34 34 38 34 32 30 30 30
  436. // 30 30 30 34 31 30 30 30 30 43
  437. // 38 34 32 34 38 45 31 44 41 34
  438. // 31 36 36 45 36 36 41 34 33 30
  439. // 30 38 30 38 36 34 33 45 31 46
  440. // 30 0D
  441. m_Adu_41Msg[nAddr - 1][0] = 0x7E;
  442. m_Adu_41Msg[nAddr - 1][1] = 0x32;
  443. m_Adu_41Msg[nAddr - 1][2] = 0x30;
  444. m_Adu_41Msg[nAddr - 1][3] = 0x30;
  445. m_Adu_41Msg[nAddr - 1][4] = 0x31;
  446. m_Adu_41Msg[nAddr - 1][5] = 0x34;
  447. m_Adu_41Msg[nAddr - 1][6] = 0x30;
  448. m_Adu_41Msg[nAddr - 1][7] = 0x30;
  449. m_Adu_41Msg[nAddr - 1][8] = 0x30;
  450. m_Adu_41Msg[nAddr - 1][9] = 0x38;
  451. m_Adu_41Msg[nAddr - 1][10] = 0x30;
  452. m_Adu_41Msg[nAddr - 1][11] = 0x36;
  453. m_Adu_41Msg[nAddr - 1][12] = 0x32;
  454. m_Adu_41Msg[nAddr - 1][13] = 0x30;
  455. m_Adu_41Msg[nAddr - 1][14] = 0x30;
  456. m_Adu_41Msg[nAddr - 1][15] = 0x30;
  457. m_Adu_41Msg[nAddr - 1][16] = 0x31;
  458. m_Adu_41Msg[nAddr - 1][17] = 0x30;
  459. m_Adu_41Msg[nAddr - 1][18] = 0x32;
  460. m_Adu_41Msg[nAddr - 1][19] = 0x43;
  461. m_Adu_41Msg[nAddr - 1][20] = 0x34;
  462. m_Adu_41Msg[nAddr - 1][21] = 0x43;
  463. m_Adu_41Msg[nAddr - 1][22] = 0x45;
  464. m_Adu_41Msg[nAddr - 1][23] = 0x37;
  465. m_Adu_41Msg[nAddr - 1][24] = 0x31;
  466. m_Adu_41Msg[nAddr - 1][25] = 0x34;
  467. m_Adu_41Msg[nAddr - 1][26] = 0x33;
  468. m_Adu_41Msg[nAddr - 1][27] = 0x42;
  469. m_Adu_41Msg[nAddr - 1][28] = 0x46;
  470. m_Adu_41Msg[nAddr - 1][29] = 0x37;
  471. m_Adu_41Msg[nAddr - 1][30] = 0x36;
  472. m_Adu_41Msg[nAddr - 1][31] = 0x37;
  473. m_Adu_41Msg[nAddr - 1][32] = 0x32;
  474. m_Adu_41Msg[nAddr - 1][33] = 0x34;
  475. m_Adu_41Msg[nAddr - 1][34] = 0x33;
  476. m_Adu_41Msg[nAddr - 1][35] = 0x43;
  477. m_Adu_41Msg[nAddr - 1][36] = 0x38;
  478. m_Adu_41Msg[nAddr - 1][37] = 0x32;
  479. m_Adu_41Msg[nAddr - 1][38] = 0x36;
  480. m_Adu_41Msg[nAddr - 1][39] = 0x37;
  481. m_Adu_41Msg[nAddr - 1][40] = 0x31;
  482. m_Adu_41Msg[nAddr - 1][41] = 0x34;
  483. m_Adu_41Msg[nAddr - 1][42] = 0x33;
  484. m_Adu_41Msg[nAddr - 1][43] = 0x20;
  485. m_Adu_41Msg[nAddr - 1][44] = 0x20;
  486. m_Adu_41Msg[nAddr - 1][45] = 0x20;
  487. m_Adu_41Msg[nAddr - 1][46] = 0x20;
  488. m_Adu_41Msg[nAddr - 1][47] = 0x20;
  489. m_Adu_41Msg[nAddr - 1][48] = 0x20;
  490. m_Adu_41Msg[nAddr - 1][49] = 0x20;
  491. m_Adu_41Msg[nAddr - 1][50] = 0x20;
  492. m_Adu_41Msg[nAddr - 1][51] = 0x30;
  493. m_Adu_41Msg[nAddr - 1][52] = 0x30;
  494. m_Adu_41Msg[nAddr - 1][53] = 0x43;
  495. m_Adu_41Msg[nAddr - 1][54] = 0x31;
  496. m_Adu_41Msg[nAddr - 1][55] = 0x41;
  497. m_Adu_41Msg[nAddr - 1][56] = 0x30;
  498. m_Adu_41Msg[nAddr - 1][57] = 0x37;
  499. m_Adu_41Msg[nAddr - 1][58] = 0x32;
  500. m_Adu_41Msg[nAddr - 1][59] = 0x34;
  501. m_Adu_41Msg[nAddr - 1][60] = 0x33;
  502. m_Adu_41Msg[nAddr - 1][61] = 0x43;
  503. m_Adu_41Msg[nAddr - 1][62] = 0x34;
  504. m_Adu_41Msg[nAddr - 1][63] = 0x43;
  505. m_Adu_41Msg[nAddr - 1][64] = 0x45;
  506. m_Adu_41Msg[nAddr - 1][65] = 0x37;
  507. m_Adu_41Msg[nAddr - 1][66] = 0x31;
  508. m_Adu_41Msg[nAddr - 1][67] = 0x34;
  509. m_Adu_41Msg[nAddr - 1][68] = 0x33;
  510. m_Adu_41Msg[nAddr - 1][69] = 0x46;
  511. m_Adu_41Msg[nAddr - 1][70] = 0x37;
  512. m_Adu_41Msg[nAddr - 1][71] = 0x46;
  513. m_Adu_41Msg[nAddr - 1][72] = 0x41;
  514. m_Adu_41Msg[nAddr - 1][73] = 0x36;
  515. m_Adu_41Msg[nAddr - 1][74] = 0x46;
  516. m_Adu_41Msg[nAddr - 1][75] = 0x34;
  517. m_Adu_41Msg[nAddr - 1][76] = 0x33;
  518. }
  519. WORD CBatteryAdu::RtnCheck(char Msg[VAR_MSG])
  520. {
  521. char buffer[3] = {0};
  522. int j = 0;
  523. for (int i = 7; i < 9; i++)
  524. {
  525. buffer[j++] = Msg[i];
  526. }
  527. buffer[j] = '\0';
  528. if (strcmp(buffer,"00") == 0)
  529. {
  530. return 0;
  531. }
  532. else if (strcmp(buffer,"01") == 0)
  533. {
  534. return ERR_CODE_RTN_VER_ERROR;
  535. }
  536. else if (strcmp(buffer,"02") == 0)
  537. {
  538. return ERR_CODE_RTN_CHKSUM_ERROR;
  539. }
  540. else if (strcmp(buffer,"03") == 0)
  541. {
  542. return ERR_CODE_RTN_LCHKSUM_ERROR;
  543. }
  544. else if (strcmp(buffer,"04") == 0)
  545. {
  546. return ERR_CODE_RTN_CID_ERROR;
  547. }
  548. else if (strcmp(buffer,"05") == 0)
  549. {
  550. return ERR_CODE_RTN_COMMAND_FORMAT;
  551. }
  552. else if (strcmp(buffer,"06") == 0)
  553. {
  554. return ERR_CODE_RTN_INVALID_LIMIT;
  555. }
  556. else if (strcmp(buffer,"E2") == 0)
  557. {
  558. return ERR_CODE_RTN_OTHER_ERROR;
  559. }
  560. return 0;
  561. }
  562. UINT CBatteryAdu::GetCheckSum(char *pBuf, char chDest[5], int len)
  563. {
  564. WORD iSum = 0;
  565. for(int i=0; i<len; i++)//求和
  566. {
  567. iSum += pBuf[i];
  568. }
  569. WORD iCompliment = iSum;
  570. iCompliment = ~iCompliment;//取反
  571. iCompliment++;
  572. itoa(iCompliment, chDest, 16);
  573. chDest[0] = lowercase2uppercase(chDest[0]);
  574. chDest[1] = lowercase2uppercase(chDest[1]);
  575. chDest[2] = lowercase2uppercase(chDest[2]);
  576. chDest[3] = lowercase2uppercase(chDest[3]);
  577. return atoi(chDest);
  578. }
  579. BYTE CBatteryAdu::GetLengthSum(BYTE szChar[])
  580. {
  581. unsigned int nData = 0;
  582. unsigned int nData1 =0;
  583. unsigned short nDataLength = 0;
  584. unsigned short nChar1;
  585. nChar1 =AsciiToBYTE(szChar[1]) +
  586. AsciiToBYTE(szChar[2]) +
  587. AsciiToBYTE(szChar[3]);
  588. nChar1 = (~nChar1) + 1;
  589. nChar1 %=16;
  590. return (BYTE)nChar1;
  591. }
  592. int CBatteryAdu::LengthCheck(char szSendMsg[])
  593. {
  594. int len = (int)strlen(szSendMsg);
  595. char buffer[5] = {0};
  596. int j = 0;
  597. for (int i = 9; i< 13; i++)
  598. {
  599. buffer[j++] = szSendMsg[i]; //取出检验位
  600. }
  601. buffer[j] ='0';
  602. if(!ValiateLength(buffer)) // 相加模十六取反加一
  603. {
  604. return ERR_CODE_RTN_LCHKSUM_ERROR;
  605. }
  606. /*转换为十六进制*/
  607. return 0;
  608. }
  609. BOOL CBatteryAdu::ValiateLength(char szChar[])
  610. {
  611. unsigned int nData = 0;
  612. unsigned int nData1 =0;
  613. unsigned short nDataLength = 0;
  614. unsigned short nChar1;
  615. nChar1 =AsciiToBYTE(szChar[1]) +
  616. AsciiToBYTE(szChar[2]) +
  617. AsciiToBYTE(szChar[3]);
  618. nChar1 = (~nChar1) + 1;
  619. nChar1 %=16;
  620. nDataLength = AsciiToBYTE(szChar[0]);
  621. if (nDataLength != nChar1)
  622. {
  623. return FALSE;
  624. }
  625. return TRUE;
  626. }