UpsEast.cpp 15 KB

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