UpsDelta.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: UpsDelta.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: Delta协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-08-24 //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include "UpsDelta.h"
  15. CUpsDelta::CUpsDelta( 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. memset(m_szDelta_STBMsg, 0, sizeof(m_szDelta_STBMsg));
  27. memset(m_szDelta_STIMsg, 0, sizeof(m_szDelta_STIMsg));
  28. memset(m_szDelta_STOMsg, 0, sizeof(m_szDelta_STOMsg));
  29. memset(m_szDelta_STPMsg, 0, sizeof(m_szDelta_STPMsg));
  30. memset(m_szDelta_STAMsg, 0, sizeof(m_szDelta_STAMsg));
  31. m_dwOnlineTick = 0;
  32. m_devOnline = TRUE;
  33. }
  34. CUpsDelta::~CUpsDelta()
  35. {
  36. #if IS_USE_READMSG_CS
  37. DeleteCriticalSection(& m_csReadMsg);
  38. #endif
  39. MTVERIFY(CloseHandle(m_hSemComm));
  40. CloseComm();
  41. }
  42. BOOL CUpsDelta::UpsDeltaOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  43. {
  44. BOOL bResult = FALSE;
  45. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  46. return bResult;
  47. }
  48. int CUpsDelta::SendReadRequest(
  49. char szPath[MAX_PATH],
  50. char szIniName[MAX_PATH],
  51. int nCommPort,
  52. int nAddr,
  53. char szCmd[MAX_CMD],
  54. char szMsg[VAR_MSG],
  55. int nReversed1,
  56. int nReversed2,
  57. int nReversed3,
  58. int nReversed4,
  59. int nReversed5,
  60. float fReversed1,
  61. float fReversed2,
  62. float fReversed3,
  63. char szReversed1[MAX_RESERVED1],
  64. char szReversed2[MAX_RESERVED2],
  65. char szReversed3[MAX_RESERVED3],
  66. char szReversed4[MAX_RESERVED4],
  67. char szReversed5[MAX_RESERVED5]
  68. )
  69. {
  70. int nIndex = 0, nLen = 0;
  71. char szSendMsg[UPS_DELTA_SEND_MSG] = {0};
  72. char szType[UPS_TYPE_LENGTH] = {0};
  73. int startBit = 0;
  74. int endBit = 0;
  75. m_address = nAddr;
  76. GetDeltaFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  77. CCommProcess *pComm = FindComm(nCommPort);
  78. if( pComm == NULL ) return -1;
  79. int nRet = -1;
  80. if( ( strlen(m_szDelta_STBMsg) == 0 &&
  81. (
  82. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  83. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  84. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  85. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  86. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  87. strcmp(szCmd, "cmd-10") == 0
  88. ) )
  89. || //获取系统模拟量量化数据(浮点数)
  90. ( strlen(m_szDelta_STIMsg) == 0 &&
  91. (
  92. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  93. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  94. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  95. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  96. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  97. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  98. strcmp(szCmd, "cmd-23") == 0
  99. ) )
  100. ||//UPS工作状态及传送顺序
  101. ( strlen(m_szDelta_STOMsg) == 0 &&
  102. (
  103. strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  104. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0||
  105. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  106. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  107. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  108. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  109. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  110. strcmp(szCmd, "cmd-38") == 0
  111. ) )
  112. ||//UPS告警内容及传送
  113. ( strlen(m_szDelta_STPMsg) == 0 &&
  114. (
  115. strcmp(szCmd, "cmd-39") == 0 ||strcmp(szCmd, "cmd-40") == 0 ||
  116. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  117. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
  118. strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
  119. strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0
  120. ) )
  121. ||//获取系统参数(浮点数)
  122. ( strlen(m_szDelta_STAMsg) == 0 &&
  123. (
  124. strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
  125. strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
  126. strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
  127. strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
  128. strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
  129. strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
  130. strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
  131. strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
  132. strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
  133. strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0
  134. ) )
  135. ||
  136. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  137. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  138. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  139. strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0
  140. )
  141. {
  142. nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  143. if( nRet != 0 ) return nRet;
  144. }
  145. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  146. {
  147. m_devOnline = FALSE;
  148. }
  149. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  150. {
  151. m_devOnline = TRUE;
  152. }
  153. if( m_devOnline == FALSE )
  154. {
  155. return -1;
  156. }
  157. nRet = GetDeltaStbMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  158. nRet = GetDeltaStiMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  159. nRet = GetDeltaStoMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  160. nRet = GetDeltaStpMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  161. nRet = GetDeltaStaMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  162. LOG4C((LOG_NOTICE, "%s = %s", szCmd, szMsg));
  163. return nRet;
  164. }
  165. int CUpsDelta::GetDeltaStbMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
  166. {
  167. int nRet = 0;
  168. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  169. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  170. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  171. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  172. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  173. strcmp(szCmd, "cmd-10") == 0)
  174. {
  175. #if IS_USE_READMSG_CS
  176. EnterCriticalSection(&m_csReadMsg);
  177. #endif
  178. CString strSTB = CString(m_szDelta_STBMsg+7);
  179. CString strTemp = strSTB;
  180. int nTempIndex = -1;
  181. for( int i = 0; i < nIndex - 1; i++ )
  182. {
  183. nTempIndex = strTemp.Find(";");
  184. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  185. }
  186. strTemp = strTemp.Mid(0, nLen);
  187. strTemp.TrimLeft("0");
  188. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  189. #if IS_USE_READMSG_CS
  190. LeaveCriticalSection(&m_csReadMsg);
  191. #endif
  192. nRet = 0;
  193. }
  194. return nRet;
  195. }
  196. int CUpsDelta::GetDeltaStiMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  197. {
  198. int nRet = 0;
  199. if (
  200. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  201. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  202. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  203. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  204. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  205. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  206. strcmp(szCmd, "cmd-23") == 0
  207. )
  208. {
  209. #if IS_USE_READMSG_CS
  210. EnterCriticalSection(&m_csReadMsg);
  211. #endif
  212. CString strSTI = CString(m_szDelta_STIMsg+7);
  213. CString strTemp = strSTI;
  214. int nTempIndex = -1;
  215. for( int i = 0; i < nIndex - 1; i++ )
  216. {
  217. nTempIndex = strTemp.Find(";");
  218. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  219. }
  220. strTemp = strTemp.Mid(0, nLen);
  221. strTemp.TrimLeft("0");
  222. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  223. #if IS_USE_READMSG_CS
  224. LeaveCriticalSection(&m_csReadMsg);
  225. #endif
  226. nRet = 0;
  227. }
  228. return nRet;
  229. }
  230. int CUpsDelta::GetDeltaStoMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  231. {
  232. int nRet = 0;
  233. if (strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  234. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0||
  235. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  236. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  237. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  238. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  239. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  240. strcmp(szCmd, "cmd-38") == 0)
  241. {
  242. #if IS_USE_READMSG_CS
  243. EnterCriticalSection(&m_csReadMsg);
  244. #endif
  245. CString strSTO = CString(m_szDelta_STOMsg+7);
  246. CString strTemp = strSTO;
  247. int nTempIndex = -1;
  248. for( int i = 0; i < nIndex - 1; i++ )
  249. {
  250. nTempIndex = strTemp.Find(";");
  251. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  252. }
  253. strTemp = strTemp.Mid(0, nLen);
  254. strTemp.TrimLeft("0");
  255. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  256. #if IS_USE_READMSG_CS
  257. LeaveCriticalSection(&m_csReadMsg);
  258. #endif
  259. nRet = 0;
  260. }
  261. return nRet;
  262. }
  263. int CUpsDelta::GetDeltaStpMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  264. {
  265. int nRet = 0;
  266. if (strcmp(szCmd, "cmd-39") == 0 ||strcmp(szCmd, "cmd-40") == 0 ||
  267. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  268. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
  269. strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
  270. strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 )
  271. {
  272. #if IS_USE_READMSG_CS
  273. EnterCriticalSection(&m_csReadMsg);
  274. #endif
  275. CString strSTP = CString(m_szDelta_STPMsg+7);
  276. CString strTemp = strSTP;
  277. int nTempIndex = -1;
  278. for( int i = 0; i < nIndex - 1; i++ )
  279. {
  280. nTempIndex = strTemp.Find(";");
  281. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  282. }
  283. strTemp = strTemp.Mid(0, nLen);
  284. strTemp.TrimLeft("0");
  285. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  286. #if IS_USE_READMSG_CS
  287. LeaveCriticalSection(&m_csReadMsg);
  288. #endif
  289. nRet = 0;
  290. }
  291. return nRet;
  292. }
  293. int CUpsDelta::GetDeltaStaMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  294. {
  295. int nRet = 0;
  296. if ( strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
  297. strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
  298. strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
  299. strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
  300. strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
  301. strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
  302. strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
  303. strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
  304. strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
  305. strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0)
  306. {
  307. #if IS_USE_READMSG_CS
  308. EnterCriticalSection(&m_csReadMsg);
  309. #endif
  310. CString strSTA = CString(m_szDelta_STAMsg+7);
  311. CString strTemp = strSTA;
  312. int nTempIndex = -1;
  313. for( int i = 0; i < nIndex - 1; i++ )
  314. {
  315. nTempIndex = strTemp.Find(";");
  316. strTemp = strTemp.Mid(nTempIndex + 1, strTemp.GetLength() - nTempIndex);
  317. }
  318. strTemp = strTemp.Mid(0, nLen);
  319. strTemp.TrimLeft("0");
  320. memcpy( szMsg, (char *)(LPCTSTR)strTemp, strTemp.GetLength() );
  321. #if IS_USE_READMSG_CS
  322. LeaveCriticalSection(&m_csReadMsg);
  323. #endif
  324. nRet = 0;
  325. }
  326. return nRet;
  327. }
  328. int CUpsDelta::GetDeviceParam(
  329. CCommProcess *pComm, //串口对象指针
  330. char szSendMsg[UPS_DELTA_SEND_MSG], //发送Buffer
  331. char szCmd[MAX_CMD], // 命令
  332. char szMsg[VAR_MSG], // 接收Buffer
  333. int &nIndex, // 变量索引,针对接收Buffer而言
  334. int &nLen, // 变量长度
  335. char szType[TYPE_LENGTH]) // 变量数据类型
  336. {
  337. int nRet = -1;
  338. nRet = RequestStatus(pComm, szSendMsg);
  339. if( nRet != 0 )
  340. {
  341. return nRet; // 串口忙
  342. }
  343. nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType);
  344. return nRet;
  345. }
  346. int CUpsDelta::GetDeltaFromIni(char szPath[MAX_PATH], //服务器程序所在目录
  347. char szIniName[MAX_PATH], //配置文件名
  348. char szCmd[MAX_CMD], //命令
  349. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  350. char nType[MAX_CMD],
  351. int &nIndex,
  352. int &nLen,
  353. int &startBit,
  354. int &endBit)
  355. {
  356. CHAR szFile[MAX_PATH + 1] = "";
  357. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  358. szSanTakSendMsg[0] = '~';
  359. szSanTakSendMsg[1] = '0';
  360. szSanTakSendMsg[2] = '0';
  361. szSanTakSendMsg[3] = 'P';
  362. szSanTakSendMsg[4] = '0';
  363. szSanTakSendMsg[5] = '0';
  364. szSanTakSendMsg[6] = '3';
  365. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg + 7, sizeof(szSanTakSendMsg) - 7, szFile);
  366. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  367. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  368. return 0;
  369. }
  370. int CUpsDelta::RequestStatus(CCommProcess *pComm, char chSendMsg[UPS_DELTA_SEND_MSG] )
  371. {
  372. #if DEBUG_DELTA //开关量,用于测试模拟数据
  373. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  374. {
  375. int nDatalen = (int)strlen(chSendMsg);
  376. ResetEvent(m_hSemComm);
  377. int nResult = pComm->Write((BYTE *)chSendMsg, nDatalen);
  378. if (nResult == nDatalen)
  379. {
  380. }
  381. else
  382. {
  383. SetEvent(m_hSemComm);
  384. //LOG4C((LOG_NOTICE, "长度不一"));
  385. return EER_CODE_UPSDELTA_COM_REGNUM;
  386. }
  387. }
  388. else
  389. {
  390. return ERR_CODE_UPSDELTA_COM_BUSY; //串口忙
  391. }
  392. #endif
  393. return 0;
  394. }
  395. int CUpsDelta::ResponseStatus(
  396. CCommProcess *pComm,
  397. char szCmd[MAX_CMD],
  398. char szMsg[VAR_MSG],
  399. int &nIndex,
  400. int &nLen,
  401. char szType[TYPE_LENGTH]
  402. )
  403. {
  404. #if DEBUG_DELTA
  405. int nReceiveLen = 0;
  406. int nProcessLen = 0;
  407. int nReadLen = 0;
  408. RESPONSE_STRUCT strcutResponse;
  409. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  410. nReceiveLen = sizeof(RESPONSE_STRUCT);
  411. //LOG4C((LOG_NOTICE, "nReceiveLen = %d",nReceiveLen));
  412. char *pBuff = new char[nReceiveLen];
  413. memset(pBuff, 0 , nLen);
  414. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  415. if (nReadLen <= 0)
  416. {
  417. SetEvent(m_hSemComm);
  418. if (pBuff != NULL)
  419. {
  420. delete [] pBuff;
  421. pBuff = NULL;
  422. }
  423. return ERR_CODE_UPSDELTA_COM_READ_NO_DATA;
  424. }
  425. SetDeltaStbMsg(szCmd, pBuff);
  426. SetDeltaStiMsg(szCmd, pBuff);
  427. SetDeltaStoMsg(szCmd, pBuff);
  428. SetDeltaStpMsg(szCmd, pBuff);
  429. SetDeltaStaMsg(szCmd, pBuff);
  430. m_dwOnlineTick = GetTickCount();
  431. // 设置串口等待事件为有信号
  432. SetEvent( m_hSemComm );
  433. if( pBuff != NULL)
  434. {
  435. delete[] pBuff;
  436. pBuff = NULL;
  437. }
  438. #else
  439. SimulationCommData();
  440. #endif
  441. return 0;
  442. }
  443. void CUpsDelta::SetDeltaStbMsg(char szCmd[MAX_CMD], char *pBuff)
  444. {
  445. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  446. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  447. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  448. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  449. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  450. strcmp(szCmd, "cmd-10") == 0 )
  451. {
  452. #if IS_USE_READMSG_CS
  453. EnterCriticalSection(&m_csReadMsg);
  454. #endif
  455. memcpy(m_szDelta_STBMsg, pBuff, sizeof(m_szDelta_STBMsg));
  456. #if IS_USE_READMSG_CS
  457. LeaveCriticalSection(&m_csReadMsg);
  458. #endif
  459. }
  460. }
  461. void CUpsDelta::SetDeltaStiMsg(char szCmd[MAX_CMD], char *pBuff)
  462. {
  463. if ( strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  464. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  465. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  466. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  467. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  468. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  469. strcmp(szCmd, "cmd-23") == 0
  470. )
  471. {
  472. #if IS_USE_READMSG_CS
  473. EnterCriticalSection(&m_csReadMsg);
  474. #endif
  475. memcpy(m_szDelta_STIMsg, pBuff, sizeof(m_szDelta_STIMsg));
  476. #if IS_USE_READMSG_CS
  477. LeaveCriticalSection(&m_csReadMsg);
  478. #endif
  479. }
  480. }
  481. void CUpsDelta::SetDeltaStoMsg(char szCmd[MAX_CMD], char *pBuff)
  482. {
  483. if ( strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  484. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
  485. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
  486. strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
  487. strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  488. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
  489. strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  490. strcmp(szCmd, "cmd-38") == 0 )
  491. {
  492. #if IS_USE_READMSG_CS
  493. EnterCriticalSection(&m_csReadMsg);
  494. #endif
  495. memcpy(m_szDelta_STOMsg, pBuff, sizeof(m_szDelta_STOMsg));
  496. #if IS_USE_READMSG_CS
  497. LeaveCriticalSection(&m_csReadMsg);
  498. #endif
  499. }
  500. }
  501. void CUpsDelta::SetDeltaStpMsg(char szCmd[MAX_CMD], char *pBuff)
  502. {
  503. if ( strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 ||
  504. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  505. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
  506. strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
  507. strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0)
  508. {
  509. #if IS_USE_READMSG_CS
  510. EnterCriticalSection(&m_csReadMsg);
  511. #endif
  512. memcpy(m_szDelta_STPMsg,pBuff,sizeof(m_szDelta_STPMsg));
  513. #if IS_USE_READMSG_CS
  514. LeaveCriticalSection(&m_csReadMsg);
  515. #endif
  516. }
  517. }
  518. void CUpsDelta::SetDeltaStaMsg(char szCmd[MAX_CMD], char *pBuff)
  519. {
  520. if ( strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
  521. strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
  522. strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
  523. strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
  524. strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
  525. strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
  526. strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
  527. strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
  528. strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
  529. strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 )
  530. {
  531. #if IS_USE_READMSG_CS
  532. EnterCriticalSection(&m_csReadMsg);
  533. #endif
  534. memcpy(m_szDelta_STAMsg,pBuff,sizeof(m_szDelta_STAMsg));
  535. #if IS_USE_READMSG_CS
  536. LeaveCriticalSection(&m_csReadMsg);
  537. #endif
  538. }
  539. }
  540. void CUpsDelta::SimulationCommData(void)
  541. {
  542. CString strRAT, strSTB, strSTI, strSTO, strSTA;
  543. strRAT = "~00D031;220;500;220;;;;;;;;;;;;;;;;;;;";
  544. strSTB = "~00D016;0;0;;;;;;;;100;";
  545. strSTI = "~00D022;3;500;2212;;;;;;;;;;;";
  546. strSTO = "~00D036;500;3;2200;1000;136000;900;;;;;;;;;";
  547. //strSTA = "1;1;1;1;1;1;1;0;1;0;1;1;0;1;1;1;1;1;1;1;";
  548. strSTA = "~00D041;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0;0;0;";
  549. memcpy(m_szDelta_STAMsg, (char *)(LPCTSTR)strSTA, strSTA.GetLength());
  550. memcpy(m_szDelta_STBMsg, (char *)(LPCTSTR)strSTB, strSTB.GetLength());
  551. memcpy(m_szDelta_STIMsg, (char *)(LPCTSTR)strSTI, strSTI.GetLength());
  552. memcpy(m_szDelta_STOMsg, (char *)(LPCTSTR)strSTO, strSTO.GetLength());
  553. }
  554. int CUpsDelta::RequestWrStatus(char szSendMsg[])
  555. {
  556. return 0;
  557. }
  558. int CUpsDelta::ResponseWrStatus(char szSendMsg[])
  559. {
  560. return 0;
  561. }
  562. int CUpsDelta::SendSetReuest(
  563. char szPath[MAX_PATH], // 程序所在路径
  564. char szIniName[MAX_PATH], // 配置文件名称
  565. int nCommPort, // 串行端口
  566. int nAddr, // 设备地址
  567. char szCmd[MAX_CMD], // 请求命令
  568. char szMsg[VAR_MSG], // 响应的值
  569. int nReversed1, // 预留整形参数1接口
  570. int nReversed2, // 预留整形参数2接口
  571. int nReversed3, // 预留整形参数3接口
  572. int nReversed4, // 预留整形参数4接口
  573. int nReversed5, // 预留整形参数5接口
  574. float fReversed1, // 预留float参数1接口
  575. float fReversed2, // 预留float参数2接口
  576. float fReversed3, // 预留float参数3接口
  577. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  578. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  579. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  580. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  581. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  582. )
  583. {
  584. return 0;
  585. }
  586. void CUpsDelta::ReturnData(char *szSource, char *szDestion, int nIndex)
  587. {
  588. char nlen[3] = {0};
  589. nlen[0] = szSource[4];
  590. nlen[1] = szSource[5];
  591. nlen[2] = szSource[6];
  592. int len = atoi(nlen);
  593. BOOL bSi = FALSE;
  594. int i = 0;
  595. int index = 0;
  596. if (szSource[7] == 0x3b)
  597. {
  598. index = 1;
  599. }
  600. while(i < len)
  601. {
  602. if (index == nIndex)
  603. {
  604. int datalen = 0;
  605. for (int k = 0;i < len ; k++)
  606. {
  607. if (szSource[i+7] != 0x3b)
  608. szDestion[k] = szSource[i+7];
  609. else
  610. break;
  611. i++;
  612. }
  613. break;
  614. }
  615. if (szSource[i+7] == 0x3b)
  616. {
  617. int j = 0;
  618. for (;i<len;i++)
  619. {
  620. if (szSource[i+7] != 0x3b)
  621. break;
  622. j++;
  623. if (j > 1)
  624. index += 1 ;
  625. if (index == nIndex)
  626. break;
  627. }
  628. }
  629. if (index == nIndex)
  630. {
  631. int datalen = 0;
  632. for (int k = 0;i < len ; k++)
  633. {
  634. if (szSource[i+7] != 0x3b)
  635. szDestion[k] = szSource[i+7];
  636. else
  637. break;
  638. i++;
  639. }
  640. break;
  641. }
  642. if (szSource[i+7] != 0x3b)
  643. {
  644. index++;
  645. if (index == nIndex)
  646. continue;
  647. for (;i < len ;i++)
  648. if (szSource[i+7] == 0x3b)
  649. {
  650. --i;
  651. bSi = FALSE;
  652. break;
  653. }
  654. }
  655. i++;
  656. }
  657. }