ST_C620KS_Q6.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "StdAfx.h"
  2. #include ".\ST_C620KS_Q6.h"
  3. ST_C620KS_Q6::ST_C620KS_Q6(char *szPath,char *szIniName,int nCommPort, int nAddr,int nRate, int nDataBit,int nStopBit, int nParity,int nInterval)
  4. {
  5. #if IS_USE_READMSG_CS
  6. InitializeCriticalSection(&m_csReadMsg);
  7. #endif
  8. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  9. memset(m_szQ6Msg, 0, sizeof(m_szQ6Msg));
  10. memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
  11. m_dwOnlineTick = 0;
  12. m_devOnline = TRUE;
  13. }
  14. ST_C620KS_Q6::~ST_C620KS_Q6(void)
  15. {
  16. #if IS_USE_READMSG_CS
  17. DeleteCriticalSection(& m_csReadMsg);
  18. #endif
  19. MTVERIFY(CloseHandle(m_hSemComm));
  20. CloseComm();
  21. }
  22. BOOL ST_C620KS_Q6::Santak_Q6OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  23. {
  24. BOOL bResult = FALSE;
  25. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  26. return bResult;
  27. }
  28. int ST_C620KS_Q6::SendReadRequest( char *szPath,char *szIniName,int nCommPort,int nAddr,char *szCmd,char *szMsg,
  29. int nReversed1,int nReversed2,int nReversed3,int nReversed4,int nReversed5,
  30. float fReversed1,float fReversed2,float fReversed3,char *szReversed1,
  31. char *szReversed2,char *szReversed3,char *szReversed4,char *szReversed5
  32. )
  33. {
  34. int nIndex = 0, nLen = 0,IniSendlen = 0;
  35. char IniSendCMD[MAX_CMD] = {0};
  36. char szType[CMD_TYPE] = {0};
  37. GetIniInfo(szPath, szIniName, szCmd, IniSendCMD, IniSendlen ,szType, nIndex, nLen);
  38. CCommProcess *pComm = FindComm(nCommPort);
  39. if( pComm == NULL ) return -1;
  40. int nRet = -1;
  41. int iCmd = atoi(szCmd + 4);
  42. if( (strlen(m_szQ6Msg) == 0 && (iCmd >= 1 && iCmd <= 18)) || iCmd == 1 ||
  43. (strlen(m_szWAMsg) == 0 && (iCmd >= 24 && iCmd <= 43)) || iCmd == 24 )
  44. {
  45. nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
  46. if( nRet != 0 )
  47. return nRet;
  48. }
  49. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  50. m_devOnline = FALSE;
  51. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  52. m_devOnline = TRUE;
  53. if( m_devOnline == FALSE )
  54. return -1;
  55. nRet = GetSantak_Q6Msg(szCmd, szMsg, nIndex, nLen, szType );
  56. nRet = GetSanTak_WAMsg(szCmd, szMsg, nIndex, nLen, szType );
  57. //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
  58. return nRet;
  59. }
  60. int ST_C620KS_Q6::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
  61. {
  62. int nRet = -1;
  63. nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
  64. if( nRet != 0 ) {
  65. return nRet;
  66. }
  67. nRet = Recv_ReadDeviceData(pComm, szCmd);
  68. return nRet;
  69. }
  70. int ST_C620KS_Q6::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD, int &IniSendlen, char *nType,int &nIndex,int &nLen)
  71. {
  72. CHAR szFile[MAX_PATH + 1] = "";
  73. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  74. TRACE("szFile =%s\n",szFile);
  75. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  76. IniSendCMD[IniSendlen] = 0x0D;
  77. ++IniSendlen;
  78. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  79. nType[strlen(nType)] = '\0';
  80. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  81. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  82. return 0;
  83. }
  84. int ST_C620KS_Q6::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
  85. {
  86. #if DEBUG_Santak_Q6
  87. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
  88. ResetEvent(m_hSemComm);
  89. int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen);
  90. if (nResult != IniSendlen){
  91. SetEvent(m_hSemComm);
  92. return EER_CODE_COM_REGNUM;
  93. }
  94. }
  95. else{
  96. return ERR_CODE_COM_BUSY;
  97. }
  98. #endif
  99. return 0;
  100. }
  101. int ST_C620KS_Q6::Recv_ReadDeviceData(CCommProcess *pComm, char *szCmd)
  102. {
  103. #if DEBUG_Santak_Q6
  104. int nProcessLen = 0;
  105. int nReadLen = 0;
  106. char *pBuff = new char[MAX_RECE_MSG];
  107. memset(pBuff, 0 , MAX_RECE_MSG);
  108. nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
  109. if (nReadLen <= 0){
  110. SetEvent(m_hSemComm);
  111. if (pBuff != NULL) {
  112. delete [] pBuff;
  113. pBuff = NULL;
  114. }
  115. return ERR_CODE_COM_READ_NO_DATA;
  116. } //长度校验出错 ;
  117. SetSanTakQ6Msg(szCmd, pBuff);
  118. SetSanTakWAMsg(szCmd, pBuff);
  119. m_dwOnlineTick = GetTickCount();
  120. SetEvent( m_hSemComm );
  121. if( pBuff != NULL) {
  122. delete[] pBuff;
  123. pBuff = NULL;
  124. }
  125. #else
  126. SimulationCommData();
  127. #endif
  128. return 0;
  129. }
  130. void ST_C620KS_Q6::SetSanTakQ6Msg(char *szCmd, char *pBuff)
  131. {
  132. int iCmd = atoi(szCmd + 4);
  133. if ( iCmd >= 1 && iCmd <= 18)
  134. {
  135. #if IS_USE_READMSG_CS
  136. EnterCriticalSection(&m_csReadMsg);
  137. #endif
  138. memcpy(m_szQ6Msg, pBuff, sizeof(m_szQ6Msg));
  139. #if IS_USE_READMSG_CS
  140. LeaveCriticalSection(&m_csReadMsg);
  141. #endif
  142. }
  143. }
  144. void ST_C620KS_Q6::SetSanTakWAMsg(char *szCmd, char *pBuff)
  145. {
  146. int iCmd = atoi(szCmd + 4);
  147. if ( iCmd >= 24 && iCmd <= 43)
  148. {
  149. #if IS_USE_READMSG_CS
  150. EnterCriticalSection(&m_csReadMsg);
  151. #endif
  152. memcpy(m_szWAMsg, pBuff, sizeof(m_szWAMsg));
  153. #if IS_USE_READMSG_CS
  154. LeaveCriticalSection(&m_csReadMsg);
  155. #endif
  156. }
  157. }
  158. int ST_C620KS_Q6::GetSantak_Q6Msg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  159. {
  160. int nRet = 0;
  161. int iCmd = atoi(szCmd + 4);
  162. if ( iCmd >= 1 && iCmd <= 18)
  163. {
  164. #if IS_USE_READMSG_CS
  165. EnterCriticalSection(&m_csReadMsg);
  166. #endif
  167. memcpy(szMsg, m_szQ6Msg + nIndex, nLen);
  168. #if IS_USE_READMSG_CS
  169. LeaveCriticalSection(&m_csReadMsg);
  170. #endif
  171. nRet = 0;
  172. }
  173. return nRet;
  174. }
  175. int ST_C620KS_Q6::GetSanTak_WAMsg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  176. {
  177. int nRet = 0;
  178. int iCmd = atoi(szCmd + 4);
  179. if ( iCmd >= 24 && iCmd <= 43)
  180. {
  181. #if IS_USE_READMSG_CS
  182. EnterCriticalSection(&m_csReadMsg);
  183. #endif
  184. memcpy(szMsg, m_szWAMsg + nIndex, nLen);
  185. #if IS_USE_READMSG_CS
  186. LeaveCriticalSection(&m_csReadMsg);
  187. #endif
  188. nRet = 0;
  189. }
  190. return nRet;
  191. }
  192. void ST_C620KS_Q6::SimulationCommData(void)
  193. {
  194. memcpy(m_szQ6Msg, "(MMM.M MMM.M MMM.M NN.N PPP.P PPP.P PPP.P RR.R QQQ QQQ QQQ SSS.S VVV.V TT.T ttttt CCC KB ffffffff wwwwwwww YO.",
  195. sizeof("(MMM.M MMM.M MMM.M NN.N PPP.P PPP.P PPP.P RR.R QQQ QQQ QQQ SSS.S VVV.V TT.T ttttt CCC KB ffffffff wwwwwwww YO."));
  196. memcpy(m_szWAMsg, "(111.4 111.8 111.0 222.4 222.8 222.0 444.8 444.0 001.4 001.8 001.5 019 00010000.",
  197. 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."));
  198. }