cpdu.cpp 7.0 KB

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