RS485Model.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "StdAfx.h"
  2. #include ".\RS485Model.h"
  3. RS485Model::RS485Model(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) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  9. for( int i = 0; i < CPDU_ADDR; i++ )
  10. {
  11. memset(m_szMsg[i], 0, sizeof(m_szMsg[i]));
  12. m_devOnline[i] = TRUE;
  13. m_dwOnlineTick[i] = 0;
  14. }
  15. }
  16. RS485Model::~RS485Model()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection( &m_csReadMsg );
  20. #endif
  21. MTVERIFY( CloseHandle( m_hSemComm ) );
  22. CloseComm();
  23. }
  24. BOOL RS485Model::RS485ModelOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  25. {
  26. BOOL bResult = FALSE;
  27. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  28. return bResult;
  29. }
  30. BYTE RS485Model::GetCheckCode(const BYTE *pSendBuf, const int &ilen)
  31. {
  32. BYTE byLrc = 0;
  33. for(int i=2; i<ilen; i++)
  34. byLrc ^= pSendBuf[i];
  35. return byLrc;
  36. }
  37. // 发送读取设备参数请求
  38. int RS485Model::SendReadRequest(
  39. char *szPath, // 程序所在路径
  40. char *szIniName, // 配置文件名称
  41. int nCommPort, // 串行端口
  42. int nAddr, // 设备地址
  43. char *szCmd, // 请求命令
  44. char *szMsg, // 响应的值
  45. int nReversed1, // 预留整形参数1接口
  46. int nReversed2, // 预留整形参数2接口
  47. int nReversed3, // 预留整形参数3接口
  48. int nReversed4, // 预留整形参数4接口
  49. int nReversed5, // 预留整形参数5接口
  50. float fReversed1, // 预留float参数1接口
  51. float fReversed2, // 预留float参数2接口
  52. float fReversed3, // 预留float参数3接口
  53. char *szReversed1, // 预留字符数组参数1接口
  54. char *szReversed2, // 预留字符数组参数2接口
  55. char *szReversed3, // 预留字符数组参数3接口
  56. char *szReversed4, // 预留字符数组参数4接口
  57. char *szReversed5 // 预留字符数组参数5接口
  58. )
  59. {
  60. //LOG4C((LOG_NOTICE,"PDU::SendReadRequest"));
  61. CCommProcess *pComm = FindComm(nCommPort);
  62. if( pComm == NULL )
  63. {
  64. LOG4C((LOG_NOTICE,"PDU pComm=NULL"));
  65. return -1;
  66. }
  67. int nRet = -1;
  68. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  69. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  70. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  71. if( (strlen((char*)m_szMsg[nAddr -1]) == 0 && strcmp(IniSendCMD,"WW") == 0) || strcmp(szCmd,"cmd-1") == 0)
  72. {
  73. nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
  74. if ( nRet != 0) return nRet;
  75. }
  76. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  77. m_devOnline[nAddr - 1] = FALSE;
  78. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  79. m_devOnline[nAddr - 1] = TRUE;
  80. if( m_devOnline[nAddr - 1] == FALSE )
  81. {
  82. LOG4C((LOG_NOTICE,"CPDU::m_devOnline=FALSE"));
  83. return -1;
  84. }
  85. nRet = GetCPDUVarMsg( nAddr, IniSendCMD, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit);
  86. //LOG4C((LOG_NOTICE,"CPDU->cmd = %s,值 = %s",szCmd,szMsg));
  87. return nRet;
  88. }
  89. int RS485Model::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
  90. {
  91. int nRet = -1;
  92. nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
  93. if( nRet != 0 )
  94. return nRet;
  95. nRet = Recv_ReadDeviceData( nAddr, pComm, IniSendCMD, szCmd);
  96. return nRet;
  97. }
  98. int RS485Model::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
  99. {
  100. #if DEBUG_RS485MODEL
  101. //LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData,nAddr = %d",nAddr));
  102. int iSendLen = 0;
  103. BYTE byArySend[16] = {0};
  104. byArySend[0] = 0x69;
  105. // 地址;
  106. byArySend[1] = nAddr;
  107. // 12个XX;
  108. byArySend[2] = 0x00;
  109. byArySend[3] = 0x00;
  110. byArySend[4] = 0x00;
  111. byArySend[5] = 0x00;
  112. byArySend[6] = 0x00;
  113. byArySend[7] = 0x00;
  114. byArySend[8] = 0x00;
  115. byArySend[9] = 0x00;
  116. byArySend[10] = 0x00;
  117. byArySend[11] = 0x00;
  118. byArySend[12] = 0x00;
  119. byArySend[13] = 0x00;
  120. // 结束;
  121. byArySend[14] = byArySend[0] ^ byArySend[1];
  122. iSendLen = 15;
  123. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  124. {
  125. ResetEvent( m_hSemComm );
  126. int nResult = pComm->Write(byArySend, iSendLen);
  127. if( nResult != iSendLen )
  128. {
  129. SetEvent( m_hSemComm );
  130. LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData=ERR_CODE_COM_FAULT"));
  131. return ERR_CODE_COM_FAULT;
  132. }
  133. }
  134. else
  135. {
  136. LOG4C((LOG_NOTICE,"PDU::Send_ReadDeviceData=ERR_CODE_COM_FAULT"));
  137. return ERR_CODE_COM_BUSY;
  138. }
  139. #endif
  140. return 0;
  141. }
  142. int RS485Model::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *szIniSendCMD, char *szCmd)
  143. {
  144. #if DEBUG_RS485MODEL
  145. BYTE *byAryRecv = new BYTE[16];
  146. ZeroMemory(byAryRecv, 16);
  147. int iRecvLen = pComm->Read(byAryRecv, 15);
  148. if( iRecvLen != 15)
  149. {
  150. SetEvent( m_hSemComm );
  151. if( iRecvLen != NULL)
  152. {
  153. delete[] byAryRecv;
  154. byAryRecv = NULL;
  155. }
  156. LOG4C((LOG_NOTICE,"RS485Model 设备无数据返回"));
  157. return ERR_CODE_COM_READ_NO_DATA;
  158. }
  159. SetCPDUVarMsg( nAddr,szIniSendCMD, szCmd, byAryRecv);
  160. m_dwOnlineTick[nAddr -1] = GetTickCount();
  161. SetEvent( m_hSemComm );
  162. if( byAryRecv != NULL)
  163. {
  164. delete[] byAryRecv;
  165. byAryRecv = NULL;
  166. }
  167. #else
  168. SimulationCommData();
  169. #endif
  170. return 0;
  171. }
  172. void RS485Model::SetCPDUVarMsg( int nAddr,const char *szIniSendCMD, char *szCmd, BYTE *pBuffer)
  173. {
  174. if( strcmp(szIniSendCMD,"WW") == 0)
  175. {
  176. #if IS_USE_READMSG_CS
  177. EnterCriticalSection( &m_csReadMsg );
  178. #endif
  179. memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
  180. #if IS_USE_READMSG_CS
  181. LeaveCriticalSection(&m_csReadMsg);
  182. #endif
  183. }
  184. }
  185. int RS485Model::GetCPDUVarMsg(int nAddr,const char *szIniSendCMD, char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  186. {
  187. int nRet = 0;
  188. if( strcmp(szIniSendCMD,"WW") == 0)
  189. {
  190. #if IS_USE_READMSG_CS
  191. EnterCriticalSection( &m_csReadMsg );
  192. #endif
  193. // ... anything
  194. #if IS_USE_READMSG_CS
  195. LeaveCriticalSection(&m_csReadMsg);
  196. nRet = 0;
  197. #endif
  198. }
  199. return nRet;
  200. }
  201. void RS485Model::SimulationCommData(void)
  202. {
  203. }