UpsEaton.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: UpsEaton.cpp //////
  4. ////// 作 者: xt //////
  5. ////// 创建时间: 2012-05-08 10:45 //////
  6. ////// 说 明: Eaton UPS协议 //////
  7. ////// //////
  8. ////// 修改时间: //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include"stdafx.h"
  13. #include "UpsEaton.h"
  14. #include "CommProcess.h"
  15. CUpsEaton::CUpsEaton(
  16. char szPath[MAX_PATH],
  17. char szIniName[MAX_PATH],
  18. int nCommPort, int nAddr,
  19. int nRate, int nDataBit,
  20. int nStopBit, int nParity,
  21. int nInterval
  22. )
  23. {
  24. #if IS_USE_READMSG_CS
  25. InitializeCriticalSection(&m_csReadMsg);
  26. #endif
  27. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  28. memset(m_szQ2Msg, 0, sizeof(m_szQ2Msg));
  29. memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
  30. m_dwOnlineTick = 0;
  31. m_devOnline = TRUE;
  32. }
  33. CUpsEaton::~CUpsEaton()
  34. {
  35. #if IS_USE_READMSG_CS
  36. DeleteCriticalSection(& m_csReadMsg);
  37. #endif
  38. MTVERIFY(CloseHandle(m_hSemComm));
  39. CloseComm();
  40. }
  41. BOOL CUpsEaton::UpsEatonOpenComm(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 CUpsEaton::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[UPS_EATON_SEND_MSG] = {0};
  71. char szType[UPS_TYPE_LENGTH] = {0};
  72. int startBit = 0;
  73. int endBit = 0;
  74. m_address = nAddr;
  75. GetParFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  76. LOG4C((LOG_NOTICE, " szSendMsg = %s", szSendMsg));
  77. CCommProcess *pComm = FindComm(nCommPort);
  78. if( pComm == NULL ) return -1;
  79. int nRet = -1;
  80. if(( strlen(m_szQ2Msg) == 0 && (
  81. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  82. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  83. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  84. strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  85. strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  86. strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  87. strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0
  88. ) )
  89. ||
  90. (strlen(m_szWAMsg) == 0 && (
  91. strcmp(szCmd,"cmd-21") == 0 ||
  92. strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  93. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  94. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 ||
  95. strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0
  96. ))
  97. ||
  98. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-21") == 0
  99. )
  100. {
  101. nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  102. if( nRet != 0 )
  103. return nRet;
  104. }
  105. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  106. {
  107. m_devOnline = FALSE;
  108. }
  109. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  110. {
  111. m_devOnline = TRUE;
  112. }
  113. if( m_devOnline == FALSE )
  114. {
  115. return -1;
  116. }
  117. nRet = GetUpsEaton_Q2Msg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  118. nRet = GetUpsEaton_WAMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  119. //LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg)));
  120. //LOG4C((LOG_NOTICE, "cmd=%s, sizeof = %d, result %s", szCmd, sizeof(szMsg), szMsg));
  121. return nRet;
  122. }
  123. int CUpsEaton::GetUpsEaton_Q2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  124. {
  125. int nRet = 0;
  126. if (strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  127. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  128. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  129. strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  130. strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  131. strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  132. strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0
  133. )
  134. {
  135. #if IS_USE_READMSG_CS
  136. EnterCriticalSection(&m_csReadMsg);
  137. #endif
  138. DataConversion(szType, m_szQ2Msg + nIndex, szMsg, nLen, startBit, endBit);
  139. //LOG4C((LOG_NOTICE, "GetSanTak_3C_Q4Msg"));
  140. //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_3C_Q4Msg,sizeof(m_szSanTak_3C_Q4Msg)));
  141. //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5 ));
  142. #if IS_USE_READMSG_CS
  143. LeaveCriticalSection(&m_csReadMsg);
  144. #endif
  145. nRet = 0;
  146. }
  147. return nRet;
  148. }
  149. int CUpsEaton::GetUpsEaton_WAMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  150. {
  151. int nRet = 0;
  152. if (
  153. strcmp(szCmd,"cmd-21") == 0 ||
  154. strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  155. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  156. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 ||
  157. strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0
  158. )
  159. {
  160. #if IS_USE_READMSG_CS
  161. EnterCriticalSection(&m_csReadMsg);
  162. #endif
  163. LOG4C((LOG_NOTICE, "szType = %s, m_szWAMsg = %s, nIndex = %d ,nLen = %d, startBit = %d, endBit = %d", szType,m_szWAMsg, nIndex,nLen,startBit,endBit));
  164. DataConversion(szType, m_szWAMsg + nIndex, szMsg, nLen, startBit, endBit);
  165. LOG4C((LOG_NOTICE, "szMsg = %s",szMsg));
  166. //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_3C_Q4Msg,sizeof(m_szSanTak_3C_Q4Msg)));
  167. //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5 ));
  168. #if IS_USE_READMSG_CS
  169. LeaveCriticalSection(&m_csReadMsg);
  170. #endif
  171. nRet = 0;
  172. }
  173. return nRet;
  174. }
  175. int CUpsEaton::GetDeviceParam(
  176. int nAddr,
  177. CCommProcess *pComm, // 串口对象指针
  178. char szSendMsg[UPS_EATON_SEND_MSG], // 发送Buffer
  179. char szCmd[MAX_CMD], // 命令
  180. char szMsg[VAR_MSG], // 接收Buffer
  181. int &nIndex, // 变量索引,针对接收Buffer而言
  182. int &nLen, // 变量长度
  183. char szType[TYPE_LENGTH]) // 变量数据类型
  184. {
  185. int nRet = -1;
  186. nRet = RequestStatus(nAddr, pComm, szSendMsg);
  187. if( nRet != 0 )
  188. {
  189. return nRet; // 串口忙
  190. }
  191. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  192. return nRet;
  193. }
  194. int CUpsEaton::GetParFromIni(
  195. char szPath[MAX_PATH], //服务器程序所在目录
  196. char szIniName[MAX_PATH], //配置文件名
  197. char szCmd[MAX_CMD], //命令
  198. char *szUpsEatonSendMsg, //发送Buffer
  199. char nType[MAX_CMD],
  200. int &nIndex,
  201. int &nLen,
  202. int &startBit,
  203. int &endBit)
  204. {
  205. CHAR szFile[MAX_PATH + 1] = "";
  206. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  207. //TRACE("szFile =%s\n",szFile);
  208. GetPrivateProfileString(szCmd, "SendCmd", "", szUpsEatonSendMsg, 10, szFile);
  209. szUpsEatonSendMsg[strlen(szUpsEatonSendMsg)] = 0x0D;
  210. GetPrivateProfileString(szCmd, "Type", "", nType, 10, szFile);
  211. nType[strlen(nType)] = '\0';
  212. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  213. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  214. startBit = GetPrivateProfileInt(szCmd,"staBit",0,szFile);
  215. endBit = GetPrivateProfileInt(szCmd,"EndBit",0,szFile);
  216. return 0;
  217. }
  218. int CUpsEaton::RequestStatus(int nAddr, CCommProcess *pComm,const char chSendMsg[UPS_EATON_SEND_MSG] )
  219. {
  220. #if DEBUG_UPS_EATON_120 //开关量,用于测试模拟数据
  221. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  222. {
  223. int nDatalen = (int)sizeof(chSendMsg)-1;
  224. ResetEvent(m_hSemComm);
  225. int nResult = pComm->Write((BYTE *)chSendMsg, nDatalen);
  226. //LOG4C_HEX_DUMP((LOG_NOTICE, chSendMsg, 3));
  227. //LOG4C((LOG_NOTICE, "CSanTak::RequestStatus chSendMsg = %s, nResult = %d, nDatalen = %d", chSendMsg,nResult, nDatalen));
  228. if (nResult == nDatalen)
  229. {
  230. }
  231. else
  232. {
  233. SetEvent(m_hSemComm);
  234. return EER_CODE_UPS_EATON_COM_REGNUM;
  235. }
  236. }
  237. else
  238. {
  239. return ERR_CODE_UPS_EATON_COM_BUSY; //串口忙
  240. }
  241. #endif
  242. return 0;
  243. }
  244. int CUpsEaton::ResponseStatus(
  245. int nAddr,
  246. CCommProcess *pComm,
  247. char szCmd[MAX_CMD],
  248. char szMsg[VAR_MSG],
  249. int &nIndex,
  250. int &nLen,
  251. char szType[TYPE_LENGTH]
  252. )
  253. {
  254. #if DEBUG_UPS_EATON_120
  255. int nReceiveLen = MAX_PATH;
  256. int nProcessLen = 0;
  257. int nReadLen = 0;
  258. char *pBuff = new char[nReceiveLen];
  259. memset(pBuff, 0 , nReceiveLen);
  260. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  261. //LOG4C((LOG_NOTICE, "CSanTak::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen));
  262. if (nReadLen <= 0)
  263. {
  264. SetEvent(m_hSemComm);
  265. if (pBuff != NULL)
  266. {
  267. delete [] pBuff;
  268. pBuff = NULL;
  269. }
  270. return ERR_CODE_UPS_EATON_COM_READ_NO_DATA;
  271. }
  272. //长度校验出错
  273. SetUpsEaton_Q2Msg(szCmd, pBuff);
  274. SetUpsEaton_WAMsg(szCmd, pBuff);
  275. m_dwOnlineTick = GetTickCount();
  276. // 设置串口等待事件为有信号
  277. SetEvent( m_hSemComm );
  278. if( pBuff != NULL)
  279. {
  280. delete[] pBuff;
  281. pBuff = NULL;
  282. }
  283. #else
  284. SimulationCommData();
  285. #endif
  286. //LOG4C((LOG_NOTICE, "SimulationCommData"));
  287. return 0;
  288. }
  289. void CUpsEaton::SetUpsEaton_Q2Msg(char szCmd[MAX_CMD], char *pBuff)
  290. {
  291. if (
  292. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  293. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  294. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  295. strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  296. strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  297. strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  298. strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0
  299. )
  300. {
  301. #if IS_USE_READMSG_CS
  302. EnterCriticalSection(&m_csReadMsg);
  303. #endif
  304. memcpy(m_szQ2Msg, pBuff, sizeof(m_szQ2Msg));
  305. #if IS_USE_READMSG_CS
  306. LeaveCriticalSection(&m_csReadMsg);
  307. #endif
  308. }
  309. }
  310. void CUpsEaton::SetUpsEaton_WAMsg(char szCmd[MAX_CMD], char *pBuff)
  311. {
  312. if (
  313. strcmp(szCmd,"cmd-21") == 0 ||
  314. strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  315. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  316. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 ||
  317. strcmp(szCmd,"cmd-30") == 0 || strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0
  318. )
  319. {
  320. #if IS_USE_READMSG_CS
  321. EnterCriticalSection(&m_csReadMsg);
  322. #endif
  323. memcpy(m_szWAMsg, pBuff, sizeof(m_szWAMsg));
  324. #if IS_USE_READMSG_CS
  325. LeaveCriticalSection(&m_csReadMsg);
  326. #endif
  327. }
  328. }
  329. void CUpsEaton::SimulationCommData(void)
  330. {
  331. }