eam7000.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "StdAfx.h"
  2. #include ".\eam7000.h"
  3. eam7000::eam7000(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 < MAX_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. eam7000::~eam7000()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection( &m_csReadMsg );
  20. #endif
  21. MTVERIFY( CloseHandle( m_hSemComm ) );
  22. CloseComm();
  23. }
  24. BOOL eam7000::eam7000_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. return bResult;
  29. }
  30. int eam7000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  31. {
  32. CHAR szFile[MAX_PATH + 1] = "";
  33. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  34. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  35. GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
  36. szDataType[strlen(szDataType)] = '\0';
  37. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  38. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  39. iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值
  40. iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile);
  41. return 0;
  42. }
  43. // 发送读取设备参数请求
  44. int eam7000::SendReadRequest(
  45. char szPath[MAX_PATH], // 程序所在路径
  46. char szIniName[MAX_PATH], // 配置文件名称
  47. int nCommPort, // 串行端口
  48. int nAddr, // 设备地址
  49. char szCmd[MAX_CMD], // 请求命令
  50. char szMsg[VAR_MSG], // 响应的值
  51. int nReversed1, // 预留整形参数1接口
  52. int nReversed2, // 预留整形参数2接口
  53. int nReversed3, // 预留整形参数3接口
  54. int nReversed4, // 预留整形参数4接口
  55. int nReversed5, // 预留整形参数5接口
  56. float fReversed1, // 预留float参数1接口
  57. float fReversed2, // 预留float参数2接口
  58. float fReversed3, // 预留float参数3接口
  59. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  60. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  61. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  62. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  63. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  64. )
  65. {
  66. CCommProcess *pComm = FindComm(nCommPort);
  67. if( pComm == NULL ) return -1;
  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. if( nAddr < 1 )
  73. return -1;
  74. int iCmd = atoi(szCmd + 4);
  75. if( (strlen(m_szMsg[nAddr -1]) == 0 && (iCmd >= 1 && iCmd <= 16) || iCmd == 1 ) ||
  76. (strlen(m_szMsg[nAddr -1]) == 0 && (iCmd >=110 && iCmd <=117) || iCmd == 110 || iCmd == 115) )
  77. {
  78. nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
  79. if ( nRet != 0)
  80. {
  81. LOG4C((LOG_NOTICE,"GetDeviceParam::error"));
  82. return nRet;
  83. }
  84. }
  85. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  86. m_devOnline[nAddr - 1] = FALSE;
  87. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  88. m_devOnline[nAddr - 1] = TRUE;
  89. if( m_devOnline[nAddr - 1] == FALSE )
  90. return -1;
  91. nRet = GetLDSVarMsg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit);
  92. //LOG4C((LOG_NOTICE," %s = %s",szCmd,szMsg));
  93. return nRet;
  94. }
  95. int eam7000::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
  96. {
  97. int nRet = -1;
  98. nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
  99. if( nRet != 0 )
  100. {
  101. LOG4C((LOG_NOTICE,"Send_ReadDeviceData::error"));
  102. return nRet;
  103. }
  104. nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
  105. return nRet;
  106. }
  107. int eam7000::SendSetReuest(
  108. char *szPath, // 程序所在路径
  109. char *szIniName, // 配置文件名称
  110. int nCommPort, // 串行端口
  111. int nAddr, // 设备地址
  112. char *szCmd, // 请求命令
  113. char *szMsg, // 响应的值
  114. int nReversed1, // 预留整形参数1接口
  115. int nReversed2, // 预留整形参数2接口
  116. int nReversed3, // 预留整形参数3接口
  117. int nReversed4, // 预留整形参数4接口
  118. int nReversed5, // 预留整形参数5接口
  119. float fReversed1, // 预留float参数1接口
  120. float fReversed2, // 预留float参数2接口
  121. float fReversed3, // 预留float参数3接口
  122. char *szReversed1, // 预留字符数组参数1接口
  123. char *szReversed2, // 预留字符数组参数2接口
  124. char *szReversed3, // 预留字符数组参数3接口
  125. char *szReversed4, // 预留字符数组参数4接口
  126. char *szReversed5 // 预留字符数组参数5接口
  127. )
  128. {
  129. return 0;
  130. }
  131. int eam7000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
  132. {
  133. #if DEBUG_EAM7000
  134. int iSendLen = 0;
  135. BYTE byArySend[5] = {0};
  136. // SOI
  137. byArySend[0] = 0x24;
  138. // 地址;
  139. byArySend[1] = ByteToAscii((nAddr>>4) & 0x0f);
  140. byArySend[2] = ByteToAscii(nAddr & 0x0f);
  141. // EOI;
  142. byArySend[3] = 0x36;
  143. byArySend[4] = 0x0D;
  144. iSendLen = 5;
  145. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  146. {
  147. ResetEvent( m_hSemComm );
  148. int nResult = pComm->Write(byArySend, iSendLen);
  149. if( nResult != iSendLen )
  150. {
  151. LOG4C((LOG_NOTICE,"Send_ReadDeviceData::发送命令失败"));
  152. SetEvent( m_hSemComm );
  153. return ERR_CODE_COM_FAULT;
  154. }
  155. }
  156. else
  157. {
  158. return ERR_CODE_COM_BUSY;
  159. }
  160. #endif
  161. return 0;
  162. }
  163. int eam7000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  164. {
  165. #if DEBUG_EAM7000
  166. BYTE *byAryRecv = new BYTE[20];
  167. ZeroMemory(byAryRecv, 20);
  168. int iRecvLen = pComm->Read(byAryRecv, 20);
  169. if( iRecvLen <= 0)
  170. {
  171. SetEvent( m_hSemComm );
  172. if( iRecvLen != NULL)
  173. {
  174. LOG4C((LOG_NOTICE,"Recv_ReadDeviceData::返回长度为0"));
  175. delete[] byAryRecv;
  176. byAryRecv = NULL;
  177. }
  178. return ERR_CODE_COM_READ_NO_DATA;
  179. }
  180. SetLDSVarMsg( nAddr,szCmd, (char*)byAryRecv);
  181. m_dwOnlineTick[nAddr -1] = GetTickCount();
  182. SetEvent( m_hSemComm );
  183. if( byAryRecv != NULL)
  184. {
  185. delete[] byAryRecv;
  186. byAryRecv = NULL;
  187. }
  188. #else
  189. SimulationCommData();
  190. #endif
  191. return 0;
  192. }
  193. void eam7000::SetLDSVarMsg( int nAddr,char *szCmd, char *pBuffer)
  194. {
  195. int iCmd = atoi(szCmd + 4);
  196. if ( iCmd >= 1 && iCmd <=16)
  197. {
  198. #if IS_USE_READMSG_CS
  199. EnterCriticalSection( &m_csReadMsg );
  200. #endif
  201. memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
  202. #if IS_USE_READMSG_CS
  203. LeaveCriticalSection(&m_csReadMsg);
  204. #endif
  205. }else if ( iCmd >= 110 && iCmd <=117)
  206. {
  207. #if IS_USE_READMSG_CS
  208. EnterCriticalSection( &m_csReadMsg );
  209. #endif
  210. memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
  211. #if IS_USE_READMSG_CS
  212. LeaveCriticalSection(&m_csReadMsg);
  213. #endif
  214. }
  215. }
  216. //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
  217. int eam7000::GetLDSVarMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  218. {
  219. int nRet = 0;
  220. int iCmd = atoi(szCmd + 4);
  221. if ( iCmd >= 1 && iCmd <=16)
  222. {
  223. #if IS_USE_READMSG_CS
  224. EnterCriticalSection( &m_csReadMsg );
  225. #endif
  226. char Cable[5] ={0};
  227. memcpy(Cable, m_szMsg[nAddr - 1] + 1,4);
  228. int iSour = HexStr2Dec(Cable);
  229. //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
  230. GetWORDBit(szRecvMsg,iSour,iSBit,iEBit);
  231. #if IS_USE_READMSG_CS
  232. LeaveCriticalSection(&m_csReadMsg);
  233. nRet = 0;
  234. #endif
  235. }else if( iCmd >= 110 && iCmd <= 117)
  236. {
  237. #if IS_USE_READMSG_CS
  238. EnterCriticalSection( &m_csReadMsg );
  239. #endif
  240. char Cable[3] ={0};
  241. memcpy(Cable, m_szMsg[nAddr - 1] + 1,2);
  242. int iSour = HexStr2Dec(Cable);
  243. //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
  244. GetBYTEBit(szRecvMsg,iSour,iSBit,iEBit);
  245. #if IS_USE_READMSG_CS
  246. LeaveCriticalSection(&m_csReadMsg);
  247. nRet = 0;
  248. #endif
  249. }
  250. return nRet;
  251. }
  252. void eam7000::SimulationCommData(void)
  253. {
  254. memcpy(m_szMsg,":0104061F0801030109EF..",sizeof(":010406120801030109EF.."));
  255. }