BatteryAdu2000.cpp 18 KB

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