UpsMonitorLan.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: UpsMonitorLan.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: Monitor Lan协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-11-24 //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include "UpsMonitorLan.h"
  15. CUpsMonitorLan::CUpsMonitorLan( 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_szRD_Msg, 0, sizeof(m_szRD_Msg));
  27. memset(m_szRS_Msg, 0, sizeof(m_szRD_Msg));
  28. m_dwOnlineTick = 0;
  29. m_devOnline = TRUE;
  30. }
  31. CUpsMonitorLan::~CUpsMonitorLan()
  32. {
  33. #if IS_USE_READMSG_CS
  34. DeleteCriticalSection(& m_csReadMsg);
  35. #endif
  36. MTVERIFY(CloseHandle(m_hSemComm));
  37. CloseComm();
  38. }
  39. BOOL CUpsMonitorLan::UpsMonitorOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  40. {
  41. BOOL bResult = FALSE;
  42. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  43. return bResult;
  44. }
  45. int CUpsMonitorLan::SendReadRequest(
  46. char szPath[MAX_PATH],
  47. char szIniName[MAX_PATH],
  48. int nCommPort,
  49. int nAddr,
  50. char szCmd[MAX_CMD],
  51. char szMsg[VAR_MSG],
  52. int nReversed1,
  53. int nReversed2,
  54. int nReversed3,
  55. int nReversed4,
  56. int nReversed5,
  57. float fReversed1,
  58. float fReversed2,
  59. float fReversed3,
  60. char szReversed1[MAX_RESERVED1],
  61. char szReversed2[MAX_RESERVED2],
  62. char szReversed3[MAX_RESERVED3],
  63. char szReversed4[MAX_RESERVED4],
  64. char szReversed5[MAX_RESERVED5]
  65. )
  66. {
  67. int nIndex = 0, nLen = 0;
  68. char szSendMsg[UPS_MONITOR_SEND_MSG] = {0};
  69. char szType[UPS_TYPE_LENGTH] = {0};
  70. int startBit = 0;
  71. int endBit = 0;
  72. m_address = nAddr;
  73. GetMonitorFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  74. CCommProcess *pComm = FindComm(nCommPort);
  75. if( pComm == NULL ) return -1;
  76. int nRet = -1;
  77. if( ( strlen(m_szRD_Msg) == 0 &&
  78. (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  79. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  80. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  81. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  82. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  83. strcmp(szCmd, "cmd-11") == 0 ||strcmp(szCmd, "cmd-12") == 0 ||
  84. strcmp(szCmd, "cmd-13") == 0 ) ) || //获取系统模拟量量化数据(浮点数)
  85. ( strlen(m_szRS_Msg) == 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 ) ) ||
  89. strcmp(szCmd, "cmd-1") == 0 ||
  90. strcmp(szCmd, "cmd-7") == 0 ||
  91. strcmp(szCmd, "cmd-14") == 0)//UPS工作状态及传送顺序
  92. {
  93. nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  94. if( nRet != 0 )
  95. return nRet;
  96. }
  97. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  98. {
  99. m_devOnline = FALSE;
  100. }
  101. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  102. {
  103. m_devOnline = TRUE;
  104. }
  105. if( m_devOnline == FALSE )
  106. {
  107. return -1;
  108. }
  109. nRet = GetMonitorRDMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  110. nRet = GetMonitorRSMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  111. // if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0)
  112. // {
  113. // //LOG4C((LOG_NOTICE, "szCmd=%s, szMsg = %s,nIndex = %d, nLen = %d, szType =%s, nRet =%d",szCmd, szMsg,nIndex, nLen, szType, nRet));
  114. // LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg)));
  115. // }
  116. //LOG4C((LOG_NOTICE, "nRet=%d", nRet));
  117. return nRet;
  118. }
  119. int CUpsMonitorLan::GetMonitorRDMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
  120. {
  121. int nRet = 0;
  122. if (strcmp(szCmd, "cmd-1") == 0 ||
  123. strcmp(szCmd, "cmd-2") == 0 ||
  124. strcmp(szCmd, "cmd-3") == 0 ||
  125. strcmp(szCmd, "cmd-4") == 0 ||
  126. strcmp(szCmd, "cmd-6") == 0 ||
  127. strcmp(szCmd, "cmd-5") == 0 ||
  128. strcmp(szCmd, "cmd-7") == 0 ||
  129. strcmp(szCmd, "cmd-8") == 0 ||
  130. strcmp(szCmd, "cmd-9") == 0 ||
  131. strcmp(szCmd, "cmd-10") == 0 ||
  132. strcmp(szCmd, "cmd-11") == 0 ||
  133. strcmp(szCmd, "cmd-12") == 0 ||
  134. strcmp(szCmd, "cmd-13") == 0 )
  135. {
  136. #if IS_USE_READMSG_CS
  137. EnterCriticalSection(&m_csReadMsg);
  138. #endif
  139. DataConversion(szType, m_szRD_Msg + nIndex, szMsg, nLen, startBit, endBit);
  140. //LOG4C((LOG_NOTICE, "szcmd = %s, m_szRD_Msg=%s, szMsg = %s",szCmd, m_szRD_Msg, szMsg));
  141. #if IS_USE_READMSG_CS
  142. LeaveCriticalSection(&m_csReadMsg);
  143. #endif
  144. nRet = 0;
  145. }
  146. return nRet;
  147. }
  148. int CUpsMonitorLan::GetMonitorRSMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  149. {
  150. int nRet = 0;
  151. if (strcmp(szCmd, "cmd-14") == 0 ||
  152. strcmp(szCmd, "cmd-15") == 0 ||
  153. strcmp(szCmd, "cmd-16") == 0 ||
  154. strcmp(szCmd, "cmd-17") == 0 ||
  155. strcmp(szCmd, "cmd-18") == 0 )
  156. {
  157. #if IS_USE_READMSG_CS
  158. EnterCriticalSection(&m_csReadMsg);
  159. #endif
  160. DataConversion(szType, m_szRS_Msg + nIndex, szMsg, nLen, startBit, endBit);
  161. //LOG4C((LOG_NOTICE, "szcmd = %s, m_szRs_Msg=%s, szMsg = %s",szCmd, m_szRS_Msg, szMsg));
  162. #if IS_USE_READMSG_CS
  163. LeaveCriticalSection(&m_csReadMsg);
  164. #endif
  165. nRet = 0;
  166. }
  167. return nRet;
  168. }
  169. int CUpsMonitorLan::GetDeviceParam(
  170. CCommProcess *pComm, //串口对象指针
  171. char szSendMsg[UPS_MONITOR_SEND_MSG], //发送Buffer
  172. char szCmd[MAX_CMD], // 命令
  173. char szMsg[VAR_MSG], // 接收Buffer
  174. int &nIndex, // 变量索引,针对接收Buffer而言
  175. int &nLen, // 变量长度
  176. char szType[TYPE_LENGTH]) // 变量数据类型
  177. {
  178. int nRet = -1;
  179. nRet = RequestStatus(pComm, szSendMsg);
  180. if( nRet != 0 )
  181. {
  182. return nRet; // 串口忙
  183. }
  184. nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType);
  185. return nRet;
  186. }
  187. int CUpsMonitorLan::GetMonitorFromIni( char szPath[MAX_PATH], //服务器程序所在目录
  188. char szIniName[MAX_PATH], //配置文件名
  189. char szCmd[MAX_CMD], //命令
  190. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  191. char nType[MAX_CMD],
  192. int &nIndex,
  193. int &nLen,
  194. int &startBit,
  195. int &endBit)
  196. {
  197. CHAR szFile[MAX_PATH + 1] = "";
  198. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  199. TRACE("szFile =%s\n",szFile);
  200. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
  201. szSanTakSendMsg[strlen(szSanTakSendMsg)] = 0x0D;
  202. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  203. nType[strlen(nType)] = '\0';
  204. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  205. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  206. //LOG4C((LOG_NOTICE, "szCmd=%s, szSanTakSendMsg = %s", szCmd, szSanTakSendMsg));
  207. return 0;
  208. }
  209. int CUpsMonitorLan::RequestStatus(CCommProcess *pComm, char chSendMsg[UPS_MONITOR_SEND_MSG] )
  210. {
  211. #if DEBUG_MONITOR //开关量,用于测试模拟数据
  212. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  213. {
  214. int nDatalen = (int)strlen(chSendMsg);
  215. ResetEvent(m_hSemComm);
  216. int nResult = pComm->Write((BYTE *)chSendMsg, nDatalen);
  217. if (nResult == nDatalen)
  218. {
  219. }
  220. else
  221. {
  222. SetEvent(m_hSemComm);
  223. return EER_CODE_MONITOR_COM_WRITE_DATA;
  224. }
  225. }
  226. else
  227. {
  228. return ERR_CODE_MONITOR_COM_BUSY; //串口忙
  229. }
  230. #endif
  231. return 0;
  232. }
  233. int CUpsMonitorLan::ResponseStatus(
  234. CCommProcess *pComm,
  235. char szCmd[MAX_CMD],
  236. char szMsg[VAR_MSG],
  237. int &nIndex,
  238. int &nLen,
  239. char szType[TYPE_LENGTH]
  240. )
  241. {
  242. #if DEBUG_MONITOR
  243. int nReceiveLen = 0;
  244. int nProcessLen = 0;
  245. int nReadLen = 0;
  246. RESPONSE_STRUCT strcutResponse;
  247. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  248. nReceiveLen = sizeof(RESPONSE_STRUCT);
  249. char *pBuff = new char[nReceiveLen];
  250. memset(pBuff, 0 , nReceiveLen);
  251. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  252. if (nReadLen <= 0)
  253. {
  254. SetEvent(m_hSemComm);
  255. if (pBuff != NULL)
  256. {
  257. delete [] pBuff;
  258. pBuff = NULL;
  259. }
  260. //LOG4C((LOG_NOTICE, "CUpsMonitorLan::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen));
  261. return ERR_CODE_MONITOR_COM_READ_NO_DATA;
  262. }
  263. //长度校验出错
  264. SetMonitorRDMsg(szCmd, pBuff);
  265. SetMonitorRSMsg(szCmd, pBuff);
  266. //LOG4C((LOG_NOTICE, "CUpsMonitorLan::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen));
  267. m_dwOnlineTick = GetTickCount();
  268. // 设置串口等待事件为有信号
  269. SetEvent( m_hSemComm );
  270. if( pBuff != NULL)
  271. {
  272. delete[] pBuff;
  273. pBuff = NULL;
  274. }
  275. #else
  276. SimulationCommData();
  277. #endif
  278. return 0;
  279. }
  280. void CUpsMonitorLan::SetMonitorRDMsg(char szCmd[MAX_CMD], char *pBuff)
  281. {
  282. if (strcmp(szCmd, "cmd-1") == 0 ||
  283. strcmp(szCmd, "cmd-2") == 0 ||
  284. strcmp(szCmd, "cmd-3") == 0 ||
  285. strcmp(szCmd, "cmd-4") == 0 ||
  286. strcmp(szCmd, "cmd-6") == 0 ||
  287. strcmp(szCmd, "cmd-5") == 0 ||
  288. strcmp(szCmd, "cmd-7") == 0 ||
  289. strcmp(szCmd, "cmd-8") == 0 ||
  290. strcmp(szCmd, "cmd-9") == 0 ||
  291. strcmp(szCmd, "cmd-10") == 0 ||
  292. strcmp(szCmd, "cmd-11") == 0 ||
  293. strcmp(szCmd, "cmd-12") == 0 ||
  294. strcmp(szCmd, "cmd-13") == 0 )
  295. {
  296. #if IS_USE_READMSG_CS
  297. EnterCriticalSection(&m_csReadMsg);
  298. #endif
  299. memcpy(m_szRD_Msg, pBuff, sizeof(m_szRD_Msg));
  300. #if IS_USE_READMSG_CS
  301. LeaveCriticalSection(&m_csReadMsg);
  302. #endif
  303. }
  304. }
  305. void CUpsMonitorLan::SetMonitorRSMsg(char szCmd[MAX_CMD], char *pBuff)
  306. {
  307. if ( strcmp(szCmd, "cmd-14") == 0 ||
  308. strcmp(szCmd, "cmd-15") == 0 ||
  309. strcmp(szCmd, "cmd-16") == 0 ||
  310. strcmp(szCmd, "cmd-17") == 0 ||
  311. strcmp(szCmd, "cmd-18") == 0 )
  312. {
  313. #if IS_USE_READMSG_CS
  314. EnterCriticalSection(&m_csReadMsg);
  315. #endif
  316. memcpy(m_szRS_Msg, pBuff, sizeof(m_szRS_Msg));
  317. #if IS_USE_READMSG_CS
  318. LeaveCriticalSection(&m_csReadMsg);
  319. #endif
  320. }
  321. }
  322. void CUpsMonitorLan::SimulationCommData(void)
  323. {
  324. memcpy(m_szRD_Msg, "DVIA228..DVIB233..DVIC235..DVOA191..DVOB192..DVOC197..DLOA032..DLOB030..DLOC028..DTMP025..DHZF050..DVBT428..DTMB191..", sizeof("DVIA228..DVIB233..DVIC235..DVOA191..DVOB192..DVOC197..DLOA032..DLOB030..DLOC001..DTMP025..DHZF050..DVBT428..DTMB191.."));
  325. memcpy(m_szRS_Msg, "SOK1..SUF1..SBP1..SBM1..SBL1..", sizeof("SOK0..SUF0..SBP0..SBM0..SBL0.."));
  326. }
  327. int CUpsMonitorLan::SendSetReuest(
  328. char szPath[MAX_PATH], // 程序所在路径
  329. char szIniName[MAX_PATH], // 配置文件名称
  330. int nCommPort, // 串行端口
  331. int nAddr, // 设备地址
  332. char szCmd[MAX_CMD], // 请求命令
  333. char szMsg[VAR_MSG], // 响应的值
  334. int nReversed1, // 预留整形参数1接口
  335. int nReversed2, // 预留整形参数2接口
  336. int nReversed3, // 预留整形参数3接口
  337. int nReversed4, // 预留整形参数4接口
  338. int nReversed5, // 预留整形参数5接口
  339. float fReversed1, // 预留float参数1接口
  340. float fReversed2, // 预留float参数2接口
  341. float fReversed3, // 预留float参数3接口
  342. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  343. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  344. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  345. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  346. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  347. )
  348. {
  349. return 0;
  350. }