ST_6C20KS_Q1.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*************************************************
  2. /* Copyright (C), 2010-2011,StoneU. Co., Ltd.
  3. /* File name: ST_3C20KS.h
  4. /* Author: Zero.t
  5. /* Version: Ver1.0.0.1
  6. /* Date: 2012-01-05
  7. /* Description: 山特 UPS,具体型号6C20KS;本类首次使用在项目<福建/第二医院>;
  8. /* Others: UPS协议类,不做控制设备操作;
  9. /* Function List: ###
  10. /* History:
  11. 1.Date: ###
  12. Author: ###
  13. Modification: ###
  14. *************************************************/
  15. #include "StdAfx.h"
  16. #include ".\st_6c20ks_q1.h"
  17. #include "CommProcess.h"
  18. CST_6C20KS::CST_6C20KS(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval)
  19. {
  20. #if IS_USE_READMSG_CS
  21. InitializeCriticalSection(&m_csReadMsg);
  22. #endif
  23. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  24. memset(m_szQ1Msg, 0, sizeof(m_szQ1Msg));
  25. memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
  26. m_dwOnlineTick = 0;
  27. m_devOnline = TRUE;
  28. }
  29. CST_6C20KS::~CST_6C20KS()
  30. {
  31. #if IS_USE_READMSG_CS
  32. DeleteCriticalSection(& m_csReadMsg);
  33. #endif
  34. MTVERIFY(CloseHandle(m_hSemComm));
  35. CloseComm();
  36. }
  37. BOOL CST_6C20KS::SanTakOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  38. {
  39. BOOL bResult = FALSE;
  40. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  41. return bResult;
  42. }
  43. int CST_6C20KS::SendReadRequest(
  44. char *szPath, // 程序所在路径
  45. char *szIniName, // 配置文件名称
  46. int nCommPort, // 串行端口
  47. int nAddr, // 设备地址
  48. char *szCmd, // 请求命令
  49. char *szMsg, // 响应的值
  50. int nReversed1, // 预留整形参数1接口
  51. int nReversed2, // 预留整形参数2接口
  52. int nReversed3, // 预留整形参数3接口
  53. int nReversed4, // 预留整形参数4接口
  54. int nReversed5, // 预留整形参数5接口
  55. float fReversed1, // 预留float参数1接口
  56. float fReversed2, // 预留float参数2接口
  57. float fReversed3, // 预留float参数3接口
  58. char *szReversed1, // 预留字符数组参数1接口
  59. char *szReversed2, // 预留字符数组参数2接口
  60. char *szReversed3, // 预留字符数组参数3接口
  61. char *szReversed4, // 预留字符数组参数4接口
  62. char *szReversed5 // 预留字符数组参数5接口
  63. )
  64. {
  65. CCommProcess *pComm = FindComm(nCommPort);
  66. if( pComm == NULL )
  67. {
  68. LOG4C((LOG_NOTICE,"6C20KS pComm=NULL"));
  69. return -1;
  70. }
  71. int nRet = -1;
  72. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  73. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  74. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  75. int iCmd = atoi(szCmd + 4);
  76. if( (strlen(m_szQ1Msg) == 0 && (iCmd >= 1 && iCmd <= 14)) || iCmd == 1 ||
  77. (strlen(m_szWAMsg) == 0 && (iCmd >= 15 && iCmd <= 26)) || iCmd == 15 )
  78. {
  79. nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
  80. if ( nRet != 0) return nRet;
  81. }
  82. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ) {
  83. m_devOnline = FALSE;
  84. }
  85. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ) {
  86. m_devOnline = TRUE;
  87. }
  88. if( m_devOnline == FALSE ) {
  89. return -1;
  90. }
  91. nRet = GetSanTak_Q1Msg(nAddr,szCmd, szMsg, nIndex, nLen, szDataType, iSBit,iEBit );
  92. nRet = GetSanTak_WAMsg(nAddr,szCmd, szMsg, nIndex, nLen, szDataType, iSBit,iEBit );
  93. //LOG4C((LOG_NOTICE, "%s=%s, return %d", szCmd, szMsg, nRet));
  94. return nRet;
  95. }
  96. int CST_6C20KS::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
  97. {
  98. int nRet = -1;
  99. nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
  100. if( nRet != 0 )
  101. {
  102. LOG4C((LOG_NOTICE,"山特CST_6C20KS发送命令失败"));
  103. return nRet;
  104. }
  105. nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
  106. return nRet;
  107. }
  108. int CST_6C20KS::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, char *IniSendCMD,const int &IniSendlen)
  109. {
  110. #if DEBUG_ST6C20KS
  111. IniSendCMD[IniSendlen] = 0x0D;
  112. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
  113. ResetEvent(m_hSemComm);
  114. int nResult = pComm->Write((BYTE *)IniSendCMD, 3);
  115. if (nResult != 3){
  116. SetEvent(m_hSemComm);
  117. return EER_CODE_SANTAK_COM_REGNUM;
  118. }
  119. }
  120. else{
  121. return ERR_CODE_SANTAK_COM_BUSY;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. int CST_6C20KS::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  127. {
  128. #if DEBUG_ST6C20KS
  129. int nReceiveLen = 0;
  130. int nProcessLen = 0;
  131. int nReadLen = 0;
  132. RESPONSE_STRUCT strcutResponse;
  133. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  134. nReceiveLen = sizeof(RESPONSE_STRUCT);
  135. char *pBuff = new char[nReceiveLen];
  136. memset(pBuff, 0 , nReceiveLen);
  137. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  138. if (nReadLen <= 0){
  139. SetEvent(m_hSemComm);
  140. if (pBuff != NULL) {
  141. delete [] pBuff;
  142. pBuff = NULL;
  143. }
  144. return ERR_CODE_SANTAK_COM_READ_NO_DATA;
  145. } //长度校验出错 ;
  146. SetSanTakQ1Msg(nAddr, szCmd, pBuff);
  147. SetSanTakWAMsg(nAddr, szCmd, pBuff);
  148. m_dwOnlineTick = GetTickCount();
  149. SetEvent( m_hSemComm );
  150. if( pBuff != NULL) {
  151. delete[] pBuff;
  152. pBuff = NULL;
  153. }
  154. #else
  155. SimulationCommData();
  156. #endif
  157. return 0;
  158. }
  159. void CST_6C20KS::SetSanTakQ1Msg( int nAddr,char *szCmd, char *pBuffer)
  160. {
  161. int iCmd = atoi(szCmd+4);
  162. if ( iCmd >=1 && iCmd <= 14 )
  163. {
  164. #if IS_USE_READMSG_CS
  165. EnterCriticalSection(&m_csReadMsg);
  166. #endif
  167. memcpy(m_szQ1Msg, pBuffer, sizeof(m_szQ1Msg));
  168. #if IS_USE_READMSG_CS
  169. LeaveCriticalSection(&m_csReadMsg);
  170. #endif
  171. }
  172. }
  173. void CST_6C20KS::SetSanTakWAMsg( int nAddr,char *szCmd, char *pBuffer)
  174. {
  175. int iCmd = atoi(szCmd+4);
  176. if ( iCmd >=15 && iCmd <= 26 )
  177. {
  178. #if IS_USE_READMSG_CS
  179. EnterCriticalSection(&m_csReadMsg);
  180. #endif
  181. memcpy(m_szWAMsg, pBuffer, sizeof(m_szWAMsg));
  182. #if IS_USE_READMSG_CS
  183. LeaveCriticalSection(&m_csReadMsg);
  184. #endif
  185. }
  186. }
  187. int CST_6C20KS::GetSanTak_Q1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  188. {
  189. int nRet = 0;
  190. int iCmd = atoi(szCmd+4);
  191. if ( iCmd >=1 && iCmd <= 14 ){
  192. #if IS_USE_READMSG_CS
  193. EnterCriticalSection(&m_csReadMsg);
  194. #endif
  195. DataConversion(szDataType, m_szQ1Msg + nIndex, szRecvMsg, nLen);
  196. #if IS_USE_READMSG_CS
  197. LeaveCriticalSection(&m_csReadMsg);
  198. #endif
  199. nRet = 0;
  200. }
  201. return nRet;
  202. }
  203. int CST_6C20KS::GetSanTak_WAMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  204. {
  205. int nRet = 0;
  206. int iCmd = atoi(szCmd+4);
  207. if ( iCmd >=15 && iCmd <= 26 )
  208. {
  209. #if IS_USE_READMSG_CS
  210. EnterCriticalSection(&m_csReadMsg);
  211. #endif
  212. DataConversion(szDataType, m_szWAMsg + nIndex, szRecvMsg, nLen);
  213. #if IS_USE_READMSG_CS
  214. LeaveCriticalSection(&m_csReadMsg);
  215. #endif
  216. nRet = 0;
  217. }
  218. return nRet;
  219. }
  220. void CST_6C20KS::SimulationCommData(void)
  221. {
  222. memcpy(m_szQ1Msg, "(208.4 140.0 208.4 034 59.9 2.05 35.0 00110000.", sizeof("(208.4 140.0 208.4 034 59.9 2.05 35.0 00110000."));
  223. memcpy(m_szWAMsg, "(215.4 216.8 221.0 235.4 226.8 421.0 616.8 261.0 295.4 276.8 621.0 019 01111100.", sizeof("(215.4 216.8 221.0 215.4 216.8 221.0 216.8 221.0 215.4 216.8 221.0 019 01111100."));
  224. }