RS232Model.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "StdAfx.h"
  2. #include ".\RS232Model.h"
  3. RS232Model::RS232Model(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. RS232Model::~RS232Model(void)
  15. {
  16. #if IS_USE_READMSG_CS
  17. DeleteCriticalSection(& m_csReadMsg);
  18. #endif
  19. MTVERIFY(CloseHandle(m_hSemComm));
  20. CloseComm();
  21. }
  22. BOOL RS232Model::RS232ModelOpenComm(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 RS232Model::SendReadRequest(char *szPath,
  29. char *szIniName,
  30. int nCommPort,
  31. int nAddr,
  32. char *szCmd,
  33. char *szMsg,
  34. int nReversed1,
  35. int nReversed2,
  36. int nReversed3,
  37. int nReversed4,
  38. int nReversed5,
  39. float fReversed1,
  40. float fReversed2,
  41. float fReversed3,
  42. char *szReversed1,
  43. char *szReversed2,
  44. char *szReversed3,
  45. char *szReversed4,
  46. char *szReversed5 )
  47. {
  48. CCommProcess *pComm = FindComm(nCommPort);
  49. if( pComm == NULL ) return -1;
  50. int nRet = -1;
  51. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  52. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  53. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  54. if( (strlen(m_szQ2Msg) == 0 && strcmp(IniSendCMD,"Q2") ==0 ) || strcmp(szCmd,"cmd-1") ==0 ||
  55. (strlen(m_szWAMsg) == 0 && strcmp(IniSendCMD,"WA") ==0 ) || strcmp(szCmd,"cmd-2") ==0 )
  56. {
  57. nRet = GetDeviceParam(pComm, IniSendCMD,IniSendlen, szCmd );
  58. if( nRet != 0 )
  59. return nRet;
  60. }
  61. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  62. m_devOnline = FALSE;
  63. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  64. m_devOnline = TRUE;
  65. if( m_devOnline == FALSE )
  66. return -1;
  67. nRet = GetSanTak_Q2Msg(IniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
  68. //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
  69. return nRet;
  70. }
  71. int RS232Model::GetDeviceParam(CCommProcess *pComm,char *IniSendCMD,const int &IniSendlen ,char *szCmd)
  72. {
  73. int nRet = -1;
  74. nRet = Send_ReadDeviceData(pComm, IniSendCMD, IniSendlen);
  75. if( nRet != 0 )
  76. return nRet;
  77. nRet = Recv_ReadDeviceData(pComm, IniSendCMD, szCmd);
  78. return nRet;
  79. }
  80. //int RS232Model::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD, int &IniSendlen, char *nType,int &nIndex,int &nLen)
  81. //{
  82. // CHAR szFile[MAX_PATH + 1] = "";
  83. // wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  84. //
  85. // TRACE("szFile =%s\n",szFile);
  86. // IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  87. // IniSendCMD[IniSendlen] = 0x0D;
  88. // ++IniSendlen;
  89. // GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  90. // nType[strlen(nType)] = '\0';
  91. // nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  92. // nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  93. //
  94. // return 0;
  95. //}
  96. int RS232Model::Send_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen )
  97. {
  98. #if DEBGU_RS232MODEL
  99. /************************************************************************/
  100. /* */
  101. /************************************************************************/
  102. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  103. {
  104. ResetEvent(m_hSemComm);
  105. int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen);
  106. if (nResult != IniSendlen)
  107. {
  108. SetEvent(m_hSemComm);
  109. LOG4C((LOG_NOTICE,"RS232MODEL 串口返回数据长度不对"));
  110. return EER_CODE_COM_REGNUM;
  111. }
  112. }
  113. else
  114. {
  115. LOG4C((LOG_NOTICE,"RS232MODEL 串口线程处于无信号当中,无法处理"));
  116. return ERR_CODE_COM_BUSY;
  117. }
  118. #endif
  119. return 0;
  120. }
  121. int RS232Model::Recv_ReadDeviceData(CCommProcess *pComm,const char *IniSendCMD, char *szCmd)
  122. {
  123. #if DEBGU_RS232MODEL
  124. BYTE *byAryRecv = new BYTE[90];
  125. ZeroMemory(byAryRecv, 90);
  126. int nReadLen = pComm->Read(byAryRecv,MAX_RECE_MSG);
  127. if (nReadLen <= 0)
  128. {
  129. SetEvent(m_hSemComm);
  130. if (byAryRecv != NULL)
  131. {
  132. delete[] byAryRecv;
  133. byAryRecv = NULL;
  134. }
  135. LOG4C((LOG_NOTICE,"RS232MODEL 设备无返回数据"));
  136. return ERR_CODE_COM_READ_NO_DATA;
  137. } //长度校验出错 ;
  138. SetSanTakQ2Msg(IniSendCMD, szCmd, (char*)byAryRecv);
  139. m_dwOnlineTick = GetTickCount();
  140. SetEvent( m_hSemComm );
  141. if( byAryRecv != NULL)
  142. {
  143. delete[] byAryRecv;
  144. byAryRecv = NULL;
  145. }
  146. #else
  147. SimulationCommData();
  148. #endif
  149. return 0;
  150. }
  151. void RS232Model::SetSanTakQ2Msg(const char *IniSendCMD, char *szCmd, char *pBuff)
  152. {
  153. if ( strcmp(IniSendCMD,"Q2") == 0)
  154. {
  155. #if IS_USE_READMSG_CS
  156. EnterCriticalSection(&m_csReadMsg);
  157. #endif
  158. memcpy(m_szQ2Msg, pBuff, sizeof(m_szQ2Msg));
  159. #if IS_USE_READMSG_CS
  160. LeaveCriticalSection(&m_csReadMsg);
  161. #endif
  162. }
  163. }
  164. int RS232Model::GetSanTak_Q2Msg(const char *IniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  165. {
  166. int nRet = 0;
  167. if ( strcmp(IniSendCMD,"WA") == 0)
  168. {
  169. #if IS_USE_READMSG_CS
  170. EnterCriticalSection(&m_csReadMsg);
  171. #endif
  172. DataConversion(szType, m_szQ2Msg + nIndex, szMsg, nLen);
  173. #if IS_USE_READMSG_CS
  174. LeaveCriticalSection(&m_csReadMsg);
  175. #endif
  176. nRet = 0;
  177. }
  178. return nRet;
  179. }
  180. void RS232Model::SimulationCommData(void)
  181. {
  182. 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."));
  183. 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."));
  184. }