EA810H.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*************************************************
  2. /* Copyright (C), 2010-2011,StoneU. Co., Ltd.
  3. /* File name: EA810H.cpp
  4. /* Author: Zero.t
  5. /* Version: Ver1.0.0.1
  6. /* Date: 2011-12-08
  7. /* Description: 易事特EA系列 UPS,EA810H;
  8. /* Others: RS232,2400/8/1,无校验;
  9. /* Function List: ###
  10. /* History:
  11. *************************************************/
  12. #include "StdAfx.h"
  13. #include ".\ea810h.h"
  14. CEA810H::CEA810H(
  15. char szPath[MAX_PATH],
  16. char szIniName[MAX_PATH],
  17. int nCommPort,
  18. int nAddr,
  19. int nRate,
  20. int nDataBit,
  21. int nStopBit,
  22. int nParity,
  23. int nInterval)
  24. {
  25. #if IS_USE_READMSG_CS
  26. InitializeCriticalSection(&m_csReadMsg);
  27. #endif
  28. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  29. memset(m_szG1_Msg, 0, sizeof(m_szG1_Msg));
  30. memset(m_szG2_Msg, 0, sizeof(m_szG2_Msg));
  31. memset(m_szG3_Msg, 0, sizeof(m_szG3_Msg));
  32. memset(m_szQ1_Msg, 0, sizeof(m_szQ1_Msg));
  33. m_dwOnlineTick = 0;
  34. m_devOnline = TRUE;
  35. }
  36. CEA810H::~CEA810H(void)
  37. {
  38. #if IS_USE_READMSG_CS
  39. DeleteCriticalSection(& m_csReadMsg);
  40. #endif
  41. MTVERIFY(CloseHandle(m_hSemComm));
  42. CloseComm();
  43. }
  44. int CEA810H::GetIniInfo(
  45. char szPath[MAX_PATH],
  46. char szIniName[MAX_PATH],
  47. char szCmd[MAX_CMD],
  48. char IniSendCMD[MAX_SENDCMD],
  49. char nType[MAX_CMD],
  50. int &nIndex,
  51. int &nLen)
  52. {
  53. CHAR szFile[MAX_PATH + 1] = "";
  54. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  55. TRACE("szFile =%s\n",szFile);
  56. GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile);
  57. IniSendCMD[strlen(IniSendCMD)] = 0x0D;
  58. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  59. nType[strlen(nType)] = '\0';
  60. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  61. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  62. return 0;
  63. }
  64. BOOL CEA810H::EA810HOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  65. {
  66. BOOL bResult = FALSE;
  67. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  68. return bResult;
  69. }
  70. int CEA810H::SendReadRequest(
  71. char szPath[MAX_PATH],
  72. char szIniName[MAX_PATH],
  73. int nCommPort,
  74. int nAddr,
  75. char szCmd[MAX_CMD],
  76. char szMsg[VAR_MSG],
  77. int nReversed1,
  78. int nReversed2,
  79. int nReversed3,
  80. int nReversed4,
  81. int nReversed5,
  82. float fReversed1,
  83. float fReversed2,
  84. float fReversed3,
  85. char szReversed1[MAX_RESERVED1],
  86. char szReversed2[MAX_RESERVED2],
  87. char szReversed3[MAX_RESERVED3],
  88. char szReversed4[MAX_RESERVED4],
  89. char szReversed5[MAX_RESERVED5]
  90. )
  91. {
  92. int nIndex = 0, nLen = 0;
  93. char IniSendCMD[MAX_SENDCMD] = {0};
  94. char szType[TYPE_LENGTH] = {0};
  95. GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, szType, nIndex, nLen);
  96. CCommProcess *pComm = FindComm(nCommPort);
  97. if( pComm == NULL ) return -1;
  98. int nRet = -1;
  99. if(
  100. ( strlen(m_szG1_Msg) == 0 && (
  101. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  102. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  103. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0
  104. ))||
  105. (strlen(m_szG2_Msg) == 0 && (
  106. strcmp(szCmd, "cmd-9") == 0 ||
  107. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  108. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  109. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  110. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  111. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  112. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0
  113. )) ||
  114. (strlen(m_szG3_Msg) == 0 && (
  115. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  116. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  117. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 ||
  118. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0
  119. )) ||
  120. (strlen(m_szQ1_Msg) == 0 && (
  121. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  122. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  123. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
  124. strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  125. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0
  126. )) ||
  127. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-9") == 0 || strcmp(szCmd,"cmd-28") || strcmp(szCmd,"cmd-40")
  128. )
  129. {
  130. nRet = GetDeviceParam( pComm, IniSendCMD, szCmd, szMsg, nIndex, nLen, szType );
  131. if( nRet != 0 )
  132. return nRet;
  133. }
  134. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ) {
  135. m_devOnline = FALSE;
  136. }
  137. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ){
  138. m_devOnline = TRUE;
  139. }
  140. if( m_devOnline == FALSE ) {
  141. return -1;
  142. }
  143. nRet = GetEA800_G1Msg(szCmd, szMsg, nIndex, nLen, szType );
  144. nRet = GetEA800_G2Msg(szCmd, szMsg, nIndex, nLen, szType );
  145. nRet = GetEA800_G3Msg(szCmd, szMsg, nIndex, nLen, szType );
  146. nRet = GetEA800_Q1Msg(szCmd, szMsg, nIndex, nLen, szType );
  147. return nRet;
  148. }
  149. int CEA810H::GetDeviceParam( CCommProcess *pComm,char IniSendCMD[MAX_SENDCMD],char szCmd[MAX_CMD],char szMsg[VAR_MSG],int &nIndex,int &nLen, char szType[TYPE_LENGTH])
  150. {
  151. int nRet = -1;
  152. nRet = RequestStatus(pComm, IniSendCMD);
  153. if( nRet != 0 ) {
  154. return nRet;
  155. }
  156. nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType);
  157. return nRet;
  158. }
  159. // IniSendCMD 2个byte;
  160. int CEA810H::RequestStatus( CCommProcess *pComm,const char IniSendCMD[MAX_SENDCMD] )
  161. {
  162. #if DEBUG_EA810H
  163. BYTE bySend[4] = {0};
  164. memcpy(bySend,IniSendCMD,2);
  165. bySend[2] = 0x0D;
  166. int nDatalen = 3;//(int)sizeof(bySend);
  167. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
  168. ResetEvent(m_hSemComm);
  169. int nResult = pComm->Write(bySend, nDatalen);
  170. if (nResult != nDatalen){
  171. SetEvent(m_hSemComm);
  172. return EER_CODE_EA800_COM_REGNUM;
  173. }
  174. }
  175. else{
  176. return ERR_CODE_EA800_COM_BUSY;
  177. }
  178. #endif
  179. return 0;
  180. }
  181. int CEA810H::ResponseStatus(CCommProcess *pComm, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] )
  182. {
  183. #if DEBUG_EA810H
  184. int nReceiveLen = 260;
  185. int nReadLen = 0;
  186. char *pBuff = new char[nReceiveLen];
  187. memset(pBuff, 0 , nReceiveLen);
  188. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  189. if (nReadLen <= 0){
  190. SetEvent(m_hSemComm);
  191. if (pBuff != NULL) {
  192. delete [] pBuff;
  193. pBuff = NULL;
  194. }
  195. return ERR_CODE_EA800_COM_READ_NO_DATA;
  196. } //长度校验出错 ;
  197. SetEA800_G1Msg(szCmd, pBuff);
  198. SetEA800_G2Msg(szCmd, pBuff);
  199. SetEA800_G3Msg(szCmd, pBuff);
  200. SetEA800_Q1Msg(szCmd, pBuff);
  201. m_dwOnlineTick = GetTickCount();
  202. SetEvent( m_hSemComm );
  203. if( pBuff != NULL) {
  204. delete[] pBuff;
  205. pBuff = NULL;
  206. }
  207. #else
  208. SimulationCommData();
  209. #endif
  210. return 0;
  211. }
  212. void CEA810H::SetEA800_G1Msg(char szCmd[MAX_CMD], char *pBuff)
  213. {
  214. if (
  215. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  216. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  217. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0
  218. ){
  219. #if IS_USE_READMSG_CS
  220. EnterCriticalSection(&m_csReadMsg);
  221. #endif
  222. memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg));
  223. #if IS_USE_READMSG_CS
  224. LeaveCriticalSection(&m_csReadMsg);
  225. #endif
  226. }
  227. }
  228. void CEA810H::SetEA800_G2Msg(char szCmd[MAX_CMD], char *pBuff)
  229. {
  230. if (
  231. strcmp(szCmd, "cmd-9") == 0 ||
  232. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  233. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  234. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  235. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  236. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  237. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0
  238. ){
  239. #if IS_USE_READMSG_CS
  240. EnterCriticalSection(&m_csReadMsg);
  241. #endif
  242. memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg));
  243. #if IS_USE_READMSG_CS
  244. LeaveCriticalSection(&m_csReadMsg);
  245. #endif
  246. }
  247. }
  248. void CEA810H::SetEA800_G3Msg(char szCmd[MAX_CMD], char *pBuff)
  249. {
  250. if (
  251. strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  252. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  253. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 ||
  254. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0
  255. ){
  256. #if IS_USE_READMSG_CS
  257. EnterCriticalSection(&m_csReadMsg);
  258. #endif
  259. memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg));
  260. #if IS_USE_READMSG_CS
  261. LeaveCriticalSection(&m_csReadMsg);
  262. #endif
  263. }
  264. }
  265. void CEA810H::SetEA800_Q1Msg(char szCmd[MAX_CMD], char *pBuff)
  266. {
  267. if (
  268. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  269. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  270. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
  271. strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  272. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0
  273. ){
  274. #if IS_USE_READMSG_CS
  275. EnterCriticalSection(&m_csReadMsg);
  276. #endif
  277. memcpy(m_szG1_Msg, pBuff, sizeof(m_szG1_Msg));
  278. #if IS_USE_READMSG_CS
  279. LeaveCriticalSection(&m_csReadMsg);
  280. #endif
  281. }
  282. }
  283. int CEA810H::GetEA800_G1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] )
  284. {
  285. int nRet = 0;
  286. if (strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  287. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  288. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0
  289. ){
  290. #if IS_USE_READMSG_CS
  291. EnterCriticalSection(&m_csReadMsg);
  292. #endif
  293. DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0);
  294. #if IS_USE_READMSG_CS
  295. LeaveCriticalSection(&m_csReadMsg);
  296. #endif
  297. nRet = 0;
  298. }
  299. return nRet;
  300. }
  301. int CEA810H::GetEA800_G2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] )
  302. {
  303. int nRet = 0;
  304. if (strcmp(szCmd, "cmd-9") == 0 ||
  305. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  306. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  307. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  308. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
  309. strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  310. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0
  311. ){
  312. #if IS_USE_READMSG_CS
  313. EnterCriticalSection(&m_csReadMsg);
  314. #endif
  315. DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0);
  316. #if IS_USE_READMSG_CS
  317. LeaveCriticalSection(&m_csReadMsg);
  318. #endif
  319. nRet = 0;
  320. }
  321. return nRet;
  322. }
  323. int CEA810H::GetEA800_G3Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] )
  324. {
  325. int nRet = 0;
  326. if (strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  327. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
  328. strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 ||
  329. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0
  330. ){
  331. #if IS_USE_READMSG_CS
  332. EnterCriticalSection(&m_csReadMsg);
  333. #endif
  334. DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0);
  335. #if IS_USE_READMSG_CS
  336. LeaveCriticalSection(&m_csReadMsg);
  337. #endif
  338. nRet = 0;
  339. }
  340. return nRet;
  341. }
  342. int CEA810H::GetEA800_Q1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] )
  343. {
  344. int nRet = 0;
  345. if (strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ||
  346. strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
  347. strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
  348. strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
  349. strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0
  350. ){
  351. #if IS_USE_READMSG_CS
  352. EnterCriticalSection(&m_csReadMsg);
  353. #endif
  354. DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0);
  355. #if IS_USE_READMSG_CS
  356. LeaveCriticalSection(&m_csReadMsg);
  357. #endif
  358. nRet = 0;
  359. }
  360. return nRet;
  361. }
  362. void CEA810H::SimulationCommData(void)
  363. {
  364. memcpy(m_szG1_Msg, "(215.4 216.8 221.0 056 50.0 2.29 44.5 01111100.", sizeof("(215.4 216.8 221.0 056 50.0 2.29 44.5 00000000."));
  365. }