ST_3C3Ex40ks.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "StdAfx.h"
  2. #include ".\st_3c3ex40ks.h"
  3. CST_3C3Ex40ks::CST_3C3Ex40ks(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_szQ2Msg, 0, sizeof(m_szQ2Msg));
  10. memset(m_szWAMsg, 0, sizeof(m_szWAMsg));
  11. m_dwOnlineTick = 0;
  12. m_devOnline = TRUE;
  13. }
  14. CST_3C3Ex40ks::~CST_3C3Ex40ks(void)
  15. {
  16. #if IS_USE_READMSG_CS
  17. DeleteCriticalSection(& m_csReadMsg);
  18. #endif
  19. MTVERIFY(CloseHandle(m_hSemComm));
  20. CloseComm();
  21. }
  22. BOOL CST_3C3Ex40ks::SanTakOpenComm(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 CST_3C3Ex40ks::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_szQ2Msg) == 0 && (iCmd >= 1 && iCmd <= 23)) || iCmd == 1 ||
  43. (strlen(m_szWAMsg) == 0 && (iCmd >= 24 && iCmd <= 35)) || iCmd == 24 )
  44. {
  45. nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
  46. if( nRet != 0 )
  47. return nRet;
  48. }
  49. /*if(
  50. ( strlen(m_szQ2Msg) == 0 && (
  51. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  52. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  53. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  54. strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  55. strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  56. strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  57. strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 || strcmp(szCmd,"cmd-21") == 0 ||
  58. strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0
  59. ) ) ||
  60. (strlen(m_szWAMsg) == 0 &&
  61. (strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  62. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  63. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 ||
  64. strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 || strcmp(szCmd,"cmd-33") == 0
  65. )) ||
  66. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd, "cmd-22") == 0
  67. ){
  68. nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
  69. if( nRet != 0 )
  70. return nRet;
  71. }*/
  72. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  73. m_devOnline = FALSE;
  74. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  75. m_devOnline = TRUE;
  76. if( m_devOnline == FALSE )
  77. return -1;
  78. nRet = GetSanTak_Q2Msg(szCmd, szMsg, nIndex, nLen, szType );
  79. nRet = GetSanTak_WAMsg(szCmd, szMsg, nIndex, nLen, szType );
  80. LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
  81. return nRet;
  82. }
  83. int CST_3C3Ex40ks::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
  84. {
  85. int nRet = -1;
  86. nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
  87. if( nRet != 0 ) {
  88. return nRet;
  89. }
  90. nRet = Recv_ReadDeviceData(pComm, szCmd);
  91. return nRet;
  92. }
  93. int CST_3C3Ex40ks::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD, int &IniSendlen, char *nType,int &nIndex,int &nLen)
  94. {
  95. CHAR szFile[MAX_PATH + 1] = "";
  96. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  97. TRACE("szFile =%s\n",szFile);
  98. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  99. IniSendCMD[IniSendlen] = 0x0D;
  100. ++IniSendlen;
  101. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  102. nType[strlen(nType)] = '\0';
  103. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  104. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  105. return 0;
  106. }
  107. int CST_3C3Ex40ks::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
  108. {
  109. #if DEBUG_ST_3C3EX40KS
  110. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
  111. ResetEvent(m_hSemComm);
  112. int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen);
  113. if (nResult != IniSendlen){
  114. SetEvent(m_hSemComm);
  115. return EER_CODE_COM_REGNUM;
  116. }
  117. }
  118. else{
  119. return ERR_CODE_COM_BUSY;
  120. }
  121. #endif
  122. return 0;
  123. }
  124. int CST_3C3Ex40ks::Recv_ReadDeviceData(CCommProcess *pComm, char *szCmd)
  125. {
  126. #if DEBUG_ST_3C3EX40KS
  127. int nProcessLen = 0;
  128. int nReadLen = 0;
  129. char *pBuff = new char[MAX_RECE_MSG];
  130. memset(pBuff, 0 , MAX_RECE_MSG);
  131. nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
  132. if (nReadLen <= 0){
  133. SetEvent(m_hSemComm);
  134. if (pBuff != NULL) {
  135. delete [] pBuff;
  136. pBuff = NULL;
  137. }
  138. return ERR_CODE_COM_READ_NO_DATA;
  139. } //长度校验出错 ;
  140. SetSanTakQ2Msg(szCmd, pBuff);
  141. SetSanTakWAMsg(szCmd, pBuff);
  142. m_dwOnlineTick = GetTickCount();
  143. SetEvent( m_hSemComm );
  144. if( pBuff != NULL) {
  145. delete[] pBuff;
  146. pBuff = NULL;
  147. }
  148. #else
  149. SimulationCommData();
  150. #endif
  151. return 0;
  152. }
  153. void CST_3C3Ex40ks::SetSanTakQ2Msg(char *szCmd, char *pBuff)
  154. {
  155. int iCmd = atoi(szCmd + 4);
  156. if ( iCmd >= 1 && iCmd <= 23)
  157. /*if (
  158. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  159. strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  160. strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  161. strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  162. strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  163. strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  164. strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 || strcmp(szCmd,"cmd-21") == 0 ||
  165. strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0
  166. )*/
  167. {
  168. #if IS_USE_READMSG_CS
  169. EnterCriticalSection(&m_csReadMsg);
  170. #endif
  171. memcpy(m_szQ2Msg, pBuff, sizeof(m_szQ2Msg));
  172. #if IS_USE_READMSG_CS
  173. LeaveCriticalSection(&m_csReadMsg);
  174. #endif
  175. }
  176. }
  177. void CST_3C3Ex40ks::SetSanTakWAMsg(char *szCmd, char *pBuff)
  178. {
  179. int iCmd = atoi(szCmd + 4);
  180. if ( iCmd >= 24 && iCmd <= 35)
  181. /*if (
  182. strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  183. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  184. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 ||
  185. strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 || strcmp(szCmd,"cmd-33") == 0
  186. )*/
  187. {
  188. #if IS_USE_READMSG_CS
  189. EnterCriticalSection(&m_csReadMsg);
  190. #endif
  191. memcpy(m_szWAMsg, pBuff, sizeof(m_szWAMsg));
  192. #if IS_USE_READMSG_CS
  193. LeaveCriticalSection(&m_csReadMsg);
  194. #endif
  195. }
  196. }
  197. int CST_3C3Ex40ks::GetSanTak_Q2Msg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  198. {
  199. int nRet = 0;
  200. int iCmd = atoi(szCmd + 4);
  201. if ( iCmd >= 1 && iCmd <= 23)
  202. //if ( strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  203. // strcmp(szCmd,"cmd-4") == 0 || strcmp(szCmd,"cmd-6") == 0 || strcmp(szCmd,"cmd-5") == 0 ||
  204. // strcmp(szCmd,"cmd-7") == 0 || strcmp(szCmd,"cmd-8") == 0 || strcmp(szCmd,"cmd-9") == 0 ||
  205. // strcmp(szCmd,"cmd-10") == 0 || strcmp(szCmd,"cmd-11") == 0 || strcmp(szCmd,"cmd-12") == 0 ||
  206. // strcmp(szCmd,"cmd-13") == 0 || strcmp(szCmd,"cmd-14") == 0 || strcmp(szCmd,"cmd-15") == 0 ||
  207. // strcmp(szCmd,"cmd-16") == 0 || strcmp(szCmd,"cmd-17") == 0 || strcmp(szCmd,"cmd-18") == 0 ||
  208. // strcmp(szCmd,"cmd-19") == 0 || strcmp(szCmd,"cmd-20") == 0 || strcmp(szCmd,"cmd-21") == 0 ||
  209. // strcmp(szCmd,"cmd-22") == 0 || strcmp(szCmd,"cmd-23") == 0
  210. // )
  211. {
  212. #if IS_USE_READMSG_CS
  213. EnterCriticalSection(&m_csReadMsg);
  214. #endif
  215. DataConversion(szType, m_szQ2Msg + nIndex, szMsg, nLen);
  216. #if IS_USE_READMSG_CS
  217. LeaveCriticalSection(&m_csReadMsg);
  218. #endif
  219. nRet = 0;
  220. }
  221. return nRet;
  222. }
  223. int CST_3C3Ex40ks::GetSanTak_WAMsg(char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  224. {
  225. int nRet = 0;
  226. int iCmd = atoi(szCmd + 4);
  227. if ( iCmd >= 24 && iCmd <= 35)
  228. /*if (strcmp(szCmd,"cmd-34") == 0 || strcmp(szCmd,"cmd-35") == 0 || strcmp(szCmd,"cmd-24") == 0 ||
  229. strcmp(szCmd,"cmd-25") == 0 || strcmp(szCmd,"cmd-26") == 0 || strcmp(szCmd,"cmd-27") == 0 ||
  230. strcmp(szCmd,"cmd-28") == 0 || strcmp(szCmd,"cmd-29") == 0 || strcmp(szCmd,"cmd-30") == 0 ||
  231. strcmp(szCmd,"cmd-31") == 0 || strcmp(szCmd,"cmd-32") == 0 || strcmp(szCmd,"cmd-33") == 0
  232. )*/
  233. {
  234. #if IS_USE_READMSG_CS
  235. EnterCriticalSection(&m_csReadMsg);
  236. #endif
  237. DataConversion(szType, m_szWAMsg + nIndex, szMsg, nLen);
  238. #if IS_USE_READMSG_CS
  239. LeaveCriticalSection(&m_csReadMsg);
  240. #endif
  241. nRet = 0;
  242. }
  243. return nRet;
  244. }
  245. void CST_3C3Ex40ks::SimulationCommData(void)
  246. {
  247. memcpy(m_szQ2Msg, "(333.6 333.2 333.9 140.0 555.1 555.2 555.3 001 002 003 50.0 419.2 20.0 00010000 999.00 100 00 00 00 00 91 00004000 11.", sizeof("(223.6 224.2 223.9 140.0 000.0 000.0 000.0 000 000 000 50.0 419.2 20.0 00010000 000.00 100 00 00 00 00 91 00004000 11."));
  248. 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.", 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."));
  249. }