UPSLan.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "StdAfx.h"
  2. #include ".\UPSLan.h"
  3. RS232Model_UPSLan::RS232Model_UPSLan(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_szRS232_RSMsg, 0, sizeof(m_szRS232_RSMsg));
  10. memset(m_szRS232_RDMsg, 0, sizeof(m_szRS232_RDMsg));
  11. m_dwOnlineTick = 0;
  12. m_devOnline = TRUE;
  13. }
  14. RS232Model_UPSLan::~RS232Model_UPSLan(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_UPSLan::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_UPSLan::SendReadRequest(
  29. char *szPath,
  30. char *szIniName,
  31. int nCommPort,
  32. int nAddr,
  33. char *szCmd,
  34. char *szMsg,
  35. int nReversed1,
  36. int nReversed2,
  37. int nReversed3,
  38. int nReversed4,
  39. int nReversed5,
  40. float fReversed1,
  41. float fReversed2,
  42. float fReversed3,
  43. char *szReversed1,
  44. char *szReversed2,
  45. char *szReversed3,
  46. char *szReversed4,
  47. char *szReversed5 )
  48. {
  49. CCommProcess *pComm = FindComm(nCommPort);
  50. if( pComm == NULL ) return -1;
  51. int nRet = -1;
  52. int nIndex(0), nLen(0), nIniSendlen(0),iSBit(0), iEBit(0);
  53. char szIniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  54. GetIniInfo(szPath,szIniName,szCmd,szIniSendCMD,nIniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  55. if( (strlen(m_szRS232_RSMsg) == 0 && strcmp(szIniSendCMD,"RS") ==0 ) || strcmp(szCmd,"cmd-1") ==0 ||
  56. (strlen(m_szRS232_RDMsg) == 0 && strcmp(szIniSendCMD,"RD") ==0 ) || strcmp(szCmd,"cmd-6") ==0 )
  57. {
  58. nRet = GetDeviceParam(pComm, szIniSendCMD,nIniSendlen, szCmd );
  59. if( nRet != 0 )
  60. return nRet;
  61. }
  62. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  63. m_devOnline = FALSE;
  64. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  65. m_devOnline = TRUE;
  66. if( m_devOnline == FALSE )
  67. return -1;
  68. nRet = GetRS232Model_RSMsg(szIniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
  69. nRet = GetRS232Model_RDMsg(szIniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType );
  70. //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
  71. return nRet;
  72. }
  73. int RS232Model_UPSLan::GetDeviceParam(CCommProcess *pComm,char *szIniSendCMD,const int &nIniSendlen ,char *szCmd)
  74. {
  75. int nRet = -1;
  76. nRet = Send_ReadDeviceData(pComm, szIniSendCMD, nIniSendlen);
  77. if( nRet != 0 )
  78. return nRet;
  79. nRet = Recv_ReadDeviceData(pComm, szIniSendCMD, szCmd);
  80. return nRet;
  81. }
  82. int RS232Model_UPSLan::Send_ReadDeviceData(CCommProcess *pComm,const char *szIniSendCMD,const int &nIniSendlen )
  83. {
  84. #if DEBGU_RS232MODEL_UPSLAN
  85. /************************************************************************/
  86. /* do something you want */
  87. /************************************************************************/
  88. int iSendLen = nIniSendlen +1;
  89. BYTE byArySend[5] = {0};
  90. memcpy(byArySend,szIniSendCMD,nIniSendlen);
  91. byArySend[nIniSendlen] = 0x0D;
  92. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  93. {
  94. ResetEvent(m_hSemComm);
  95. int nResult = pComm->Write(byArySend, iSendLen);
  96. if (nResult != iSendLen)
  97. {
  98. SetEvent(m_hSemComm);
  99. LOG4C((LOG_NOTICE,"RS232MODEL 串口返回数据长度不对"));
  100. return EER_CODE_COM_REGNUM;
  101. }
  102. }
  103. else
  104. {
  105. LOG4C((LOG_NOTICE,"RS232MODEL 串口线程处于无信号当中,无法处理"));
  106. return ERR_CODE_COM_BUSY;
  107. }
  108. #endif
  109. return 0;
  110. }
  111. int RS232Model_UPSLan::Recv_ReadDeviceData(CCommProcess *pComm,const char *szIniSendCMD, char *szCmd)
  112. {
  113. #if DEBGU_RS232MODEL_UPSLAN
  114. BYTE *byAryRecv = new BYTE[1024];
  115. ZeroMemory(byAryRecv, 1024);
  116. int nReadLen = pComm->Read(byAryRecv,MAX_RECE_MSG);
  117. if (nReadLen <= 0)
  118. {
  119. SetEvent(m_hSemComm);
  120. if (byAryRecv != NULL)
  121. {
  122. delete[] byAryRecv;
  123. byAryRecv = NULL;
  124. }
  125. LOG4C((LOG_NOTICE,"RS232MODEL 设备无返回数据"));
  126. return ERR_CODE_COM_READ_NO_DATA;
  127. } //长度校验出错 ;
  128. SetRS232Model_RSMsg(szIniSendCMD, szCmd, (char*)byAryRecv);
  129. SetRS232Model_RDMsg(szIniSendCMD, szCmd, (char*)byAryRecv);
  130. m_dwOnlineTick = GetTickCount();
  131. SetEvent( m_hSemComm );
  132. if( byAryRecv != NULL)
  133. {
  134. delete[] byAryRecv;
  135. byAryRecv = NULL;
  136. }
  137. #else
  138. SimulationCommData();
  139. #endif
  140. return 0;
  141. }
  142. void RS232Model_UPSLan::SetRS232Model_RSMsg(const char *szIniSendCMD, char *szCmd, char *pBuff)
  143. {
  144. if ( strcmp(szIniSendCMD,"RS") == 0)
  145. {
  146. #if IS_USE_READMSG_CS
  147. EnterCriticalSection(&m_csReadMsg);
  148. #endif
  149. memcpy(m_szRS232_RSMsg, pBuff, sizeof(m_szRS232_RSMsg));
  150. #if IS_USE_READMSG_CS
  151. LeaveCriticalSection(&m_csReadMsg);
  152. #endif
  153. }
  154. }
  155. void RS232Model_UPSLan::SetRS232Model_RDMsg(const char *szIniSendCMD, char *szCmd, char *pBuff)
  156. {
  157. if ( strcmp(szIniSendCMD,"RD") == 0)
  158. {
  159. #if IS_USE_READMSG_CS
  160. EnterCriticalSection(&m_csReadMsg);
  161. #endif
  162. memcpy(m_szRS232_RDMsg, pBuff, sizeof(m_szRS232_RDMsg));
  163. #if IS_USE_READMSG_CS
  164. LeaveCriticalSection(&m_csReadMsg);
  165. #endif
  166. }
  167. }
  168. int RS232Model_UPSLan::GetRS232Model_RSMsg(const char *szIniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  169. {
  170. int nRet = 0;
  171. if ( strcmp(szIniSendCMD,"RS") == 0)
  172. {
  173. #if IS_USE_READMSG_CS
  174. EnterCriticalSection(&m_csReadMsg);
  175. #endif
  176. DataConversion(szType, m_szRS232_RSMsg + nIndex, szMsg, nLen);
  177. #if IS_USE_READMSG_CS
  178. LeaveCriticalSection(&m_csReadMsg);
  179. #endif
  180. nRet = 0;
  181. }
  182. return nRet;
  183. }
  184. int RS232Model_UPSLan::GetRS232Model_RDMsg(const char *szIniSendCMD, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  185. {
  186. int nRet = 0;
  187. if ( strcmp(szIniSendCMD,"RD") == 0)
  188. {
  189. #if IS_USE_READMSG_CS
  190. EnterCriticalSection(&m_csReadMsg);
  191. #endif
  192. DataConversion(szType, m_szRS232_RDMsg + nIndex, szMsg, nLen);
  193. #if IS_USE_READMSG_CS
  194. LeaveCriticalSection(&m_csReadMsg);
  195. #endif
  196. nRet = 0;
  197. }
  198. return nRet;
  199. }
  200. void RS232Model_UPSLan::SimulationCommData(void)
  201. {
  202. //DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1
  203. memcpy(m_szRS232_RDMsg,"DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1",
  204. sizeof("DVIA0240.80..DVIB0245.90..DVIC0238.70..DVOA0219.40..DVOB0000.00..DVOC0000.00..DLOA0000.60..DLOB0000.00..DLOC0000.00..DVBT0434.60..DVBN0434.90..DVBL0100.00..DHZF0050.00..DTMB0120.00..DTMP0025.1"));
  205. //SOK0..SUF0..SBP0..SBM0..SBL0..
  206. memcpy(m_szRS232_RSMsg,"SOK0..SUF0..SBP0..SBM0..SBL0..",sizeof("SOK0..SUF0..SBP0..SBM0..SBL0.."));
  207. }