EA99.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "StdAfx.h"
  2. #include ".\ea99.h"
  3. EA99::EA99(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_szG1_Msg, 0, sizeof(m_szG1_Msg));
  10. memset(m_szG2_Msg, 0, sizeof(m_szG2_Msg));
  11. memset(m_szG3_Msg, 0, sizeof(m_szG3_Msg));
  12. memset(m_szQ1_Msg, 0, sizeof(m_szQ1_Msg));
  13. memset(m_szF_Msg, 0, sizeof(m_szF_Msg));
  14. memset(m_szGF_Msg, 0, sizeof(m_szGF_Msg));
  15. m_dwOnlineTick = 0;
  16. m_devOnline = TRUE;
  17. }
  18. EA99::~EA99()
  19. {
  20. #if IS_USE_READMSG_CS
  21. DeleteCriticalSection( &m_csReadMsg );
  22. #endif
  23. MTVERIFY( CloseHandle( m_hSemComm ) );
  24. CloseComm();
  25. }
  26. BOOL EA99::EA99_OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  27. {
  28. BOOL bResult = FALSE;
  29. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  30. return bResult;
  31. }
  32. // 发送读取设备参数请求
  33. int EA99::SendReadRequest(
  34. char szPath[MAX_PATH], // 程序所在路径
  35. char szIniName[MAX_PATH], // 配置文件名称
  36. int nCommPort, // 串行端口
  37. int nAddr, // 设备地址
  38. char szCmd[MAX_CMD], // 请求命令
  39. char szMsg[VAR_MSG], // 响应的值
  40. int nReversed1, // 预留整形参数1接口
  41. int nReversed2, // 预留整形参数2接口
  42. int nReversed3, // 预留整形参数3接口
  43. int nReversed4, // 预留整形参数4接口
  44. int nReversed5, // 预留整形参数5接口
  45. float fReversed1, // 预留float参数1接口
  46. float fReversed2, // 预留float参数2接口
  47. float fReversed3, // 预留float参数3接口
  48. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  49. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  50. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  51. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  52. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  53. )
  54. {
  55. CCommProcess *pComm = FindComm(nCommPort);
  56. if( pComm == NULL ) return -1;
  57. int nRet = -1;
  58. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  59. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  60. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  61. if( nAddr < 1 )
  62. return -1;
  63. int iCmd = atoi(szCmd + 4);
  64. if(
  65. ( strlen(m_szG1_Msg) == 0 && (iCmd >= 1 && iCmd <= 8) ) || iCmd == 1 ||
  66. ( strlen(m_szG2_Msg) == 0 && (iCmd >= 9 && iCmd <= 27) ) || iCmd == 9 ||
  67. ( strlen(m_szG3_Msg) == 0 && (iCmd >= 28 && iCmd <= 39) ) || iCmd == 28 ||
  68. ( strlen(m_szQ1_Msg) == 0 && (iCmd >= 40 && iCmd <= 54) ) || iCmd == 40 ||
  69. ( strlen(m_szF_Msg) == 0 && (iCmd >= 55 && iCmd <= 58) ) || iCmd == 55 ||
  70. ( strlen(m_szGF_Msg) == 0 && (iCmd >= 59 && iCmd <= 99) ) || iCmd == 59
  71. )
  72. {
  73. nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
  74. if ( nRet != 0) return nRet;
  75. }
  76. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  77. m_devOnline = FALSE;
  78. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  79. m_devOnline = TRUE;
  80. if( m_devOnline == FALSE )
  81. return -1;
  82. nRet = GetEA800_G1Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  83. nRet = GetEA800_G2Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  84. nRet = GetEA800_G3Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  85. nRet = GetEA800_Q1Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  86. nRet = GetEA800_FMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  87. nRet = GetEA800_GFMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType );
  88. //LOG4C((LOG_NOTICE,"%s = %s",szCmd,szMsg));
  89. return nRet;
  90. }
  91. int EA99::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
  92. {
  93. int nRet = -1;
  94. nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
  95. if( nRet != 0 )
  96. return nRet;
  97. nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
  98. return nRet;
  99. }
  100. int EA99::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, char *IniSendCMD,const int &IniSendlen)
  101. {
  102. #if DEBUG_EA99
  103. BYTE byArySend[5] = {0};
  104. memcpy(byArySend,IniSendCMD,IniSendlen);
  105. byArySend[IniSendlen] = 0x0D;
  106. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  107. {
  108. ResetEvent( m_hSemComm );
  109. int nResult = pComm->Write(byArySend, IniSendlen +1 );
  110. if( nResult != IniSendlen+1 )
  111. {
  112. SetEvent( m_hSemComm );
  113. return ERR_CODE_COM_FAULT;
  114. }
  115. }
  116. else
  117. {
  118. return ERR_CODE_COM_BUSY;
  119. }
  120. #endif
  121. return 0;
  122. }
  123. int EA99::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  124. {
  125. #if DEBUG_EA99
  126. BYTE *byAryRecv = new BYTE[255];
  127. memset(byAryRecv,0, 255);
  128. int iRecvLen = pComm->Read(byAryRecv, 255);
  129. if( iRecvLen <= 0)
  130. {
  131. SetEvent( m_hSemComm );
  132. if( iRecvLen != NULL)
  133. {
  134. delete[] byAryRecv;
  135. byAryRecv = NULL;
  136. }
  137. return ERR_CODE_COM_READ_NO_DATA;
  138. }
  139. SetEA800_G1Msg(nAddr, szCmd, byAryRecv);
  140. SetEA800_G2Msg(nAddr, szCmd, byAryRecv);
  141. SetEA800_G3Msg(nAddr, szCmd, byAryRecv);
  142. SetEA800_Q1Msg(nAddr, szCmd, byAryRecv);
  143. SetEA800_FMsg(nAddr, szCmd, byAryRecv);
  144. SetEA800_GFMsg(nAddr, szCmd, byAryRecv);
  145. m_dwOnlineTick = GetTickCount();
  146. SetEvent( m_hSemComm );
  147. if( byAryRecv != NULL)
  148. {
  149. delete[] byAryRecv;
  150. byAryRecv = NULL;
  151. }
  152. #else
  153. SimulationCommData();
  154. #endif
  155. return 0;
  156. }
  157. void EA99::SetEA800_G1Msg( int nAddr,char *szCmd, BYTE *pBuffer)
  158. {
  159. int iCmd = atoi(szCmd + 4);
  160. if ( iCmd >= 1 && iCmd <=8)
  161. {
  162. #if IS_USE_READMSG_CS
  163. EnterCriticalSection( &m_csReadMsg );
  164. #endif
  165. memcpy(m_szG1_Msg, pBuffer, sizeof(m_szG1_Msg));
  166. #if IS_USE_READMSG_CS
  167. LeaveCriticalSection(&m_csReadMsg);
  168. #endif
  169. }
  170. }
  171. void EA99::SetEA800_G2Msg( int nAddr,char *szCmd, BYTE *pBuffer)
  172. {
  173. int iCmd = atoi(szCmd + 4);
  174. if ( iCmd >= 9 && iCmd <= 27)
  175. {
  176. #if IS_USE_READMSG_CS
  177. EnterCriticalSection( &m_csReadMsg );
  178. #endif
  179. memcpy(m_szG2_Msg, pBuffer, sizeof(m_szG2_Msg));
  180. #if IS_USE_READMSG_CS
  181. LeaveCriticalSection(&m_csReadMsg);
  182. #endif
  183. }
  184. }
  185. void EA99::SetEA800_G3Msg( int nAddr,char *szCmd, BYTE *pBuffer)
  186. {
  187. int iCmd = atoi(szCmd + 4);
  188. if ( iCmd >= 28 && iCmd <= 39)
  189. {
  190. #if IS_USE_READMSG_CS
  191. EnterCriticalSection( &m_csReadMsg );
  192. #endif
  193. memcpy(m_szG3_Msg, pBuffer, sizeof(m_szG3_Msg));
  194. #if IS_USE_READMSG_CS
  195. LeaveCriticalSection(&m_csReadMsg);
  196. #endif
  197. }
  198. }
  199. void EA99::SetEA800_Q1Msg( int nAddr,char *szCmd, BYTE *pBuffer)
  200. {
  201. int iCmd = atoi(szCmd + 4);
  202. if ( iCmd >= 40 && iCmd <= 54)
  203. {
  204. #if IS_USE_READMSG_CS
  205. EnterCriticalSection( &m_csReadMsg );
  206. #endif
  207. memcpy(m_szQ1_Msg, pBuffer, sizeof(m_szQ1_Msg));
  208. #if IS_USE_READMSG_CS
  209. LeaveCriticalSection(&m_csReadMsg);
  210. #endif
  211. }
  212. }
  213. void EA99::SetEA800_FMsg( int nAddr,char *szCmd, BYTE *pBuffer)
  214. {
  215. int iCmd = atoi(szCmd + 4);
  216. if ( iCmd >= 55 && iCmd <= 58)
  217. {
  218. #if IS_USE_READMSG_CS
  219. EnterCriticalSection( &m_csReadMsg );
  220. #endif
  221. memcpy(m_szF_Msg, pBuffer, sizeof(m_szF_Msg));
  222. #if IS_USE_READMSG_CS
  223. LeaveCriticalSection(&m_csReadMsg);
  224. #endif
  225. }
  226. }
  227. void EA99::SetEA800_GFMsg( int nAddr,char *szCmd, BYTE *pBuffer)
  228. {
  229. int iCmd = atoi(szCmd + 4);
  230. if ( iCmd >= 59 && iCmd <= 99)
  231. {
  232. #if IS_USE_READMSG_CS
  233. EnterCriticalSection( &m_csReadMsg );
  234. #endif
  235. memcpy(m_szGF_Msg, pBuffer, sizeof(m_szGF_Msg));
  236. #if IS_USE_READMSG_CS
  237. LeaveCriticalSection(&m_csReadMsg);
  238. #endif
  239. }
  240. }
  241. int EA99::GetEA800_G1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  242. {
  243. int nRet = 0;
  244. int iCmd = atoi(szCmd + 4);
  245. if ( iCmd >= 1 && iCmd <= 8)
  246. {
  247. #if IS_USE_READMSG_CS
  248. EnterCriticalSection( &m_csReadMsg );
  249. #endif
  250. DataConversion(szDataType, m_szG1_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  251. #if IS_USE_READMSG_CS
  252. LeaveCriticalSection(&m_csReadMsg);
  253. nRet = 0;
  254. #endif
  255. }
  256. return nRet;
  257. }
  258. int EA99::GetEA800_G2Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  259. {
  260. int nRet = 0;
  261. int iCmd = atoi(szCmd + 4);
  262. if ( iCmd >= 9 && iCmd <= 27)
  263. {
  264. #if IS_USE_READMSG_CS
  265. EnterCriticalSection( &m_csReadMsg );
  266. #endif
  267. DataConversion(szDataType, m_szG2_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  268. #if IS_USE_READMSG_CS
  269. LeaveCriticalSection(&m_csReadMsg);
  270. nRet = 0;
  271. #endif
  272. }
  273. return nRet;
  274. }
  275. int EA99::GetEA800_G3Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  276. {
  277. int nRet = 0;
  278. int iCmd = atoi(szCmd + 4);
  279. if ( iCmd >= 28 && iCmd <= 39)
  280. {
  281. #if IS_USE_READMSG_CS
  282. EnterCriticalSection( &m_csReadMsg );
  283. #endif
  284. DataConversion(szDataType, m_szG3_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  285. #if IS_USE_READMSG_CS
  286. LeaveCriticalSection(&m_csReadMsg);
  287. nRet = 0;
  288. #endif
  289. }
  290. return nRet;
  291. }
  292. int EA99::GetEA800_Q1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  293. {
  294. int nRet = 0;
  295. int iCmd = atoi(szCmd + 4);
  296. if ( iCmd >= 40 && iCmd <= 54)
  297. {
  298. #if IS_USE_READMSG_CS
  299. EnterCriticalSection( &m_csReadMsg );
  300. #endif
  301. DataConversion(szDataType, m_szQ1_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  302. #if IS_USE_READMSG_CS
  303. LeaveCriticalSection(&m_csReadMsg);
  304. nRet = 0;
  305. #endif
  306. }
  307. return nRet;
  308. }
  309. int EA99::GetEA800_FMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  310. {
  311. int nRet = 0;
  312. int iCmd = atoi(szCmd + 4);
  313. if ( iCmd >= 55 && iCmd <= 58)
  314. {
  315. #if IS_USE_READMSG_CS
  316. EnterCriticalSection( &m_csReadMsg );
  317. #endif
  318. DataConversion(szDataType, m_szF_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  319. #if IS_USE_READMSG_CS
  320. LeaveCriticalSection(&m_csReadMsg);
  321. nRet = 0;
  322. #endif
  323. }
  324. return nRet;
  325. }
  326. int EA99::GetEA800_GFMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  327. {
  328. int nRet = 0;
  329. int iCmd = atoi(szCmd + 4);
  330. if ( iCmd >= 59 && iCmd <= 99)
  331. {
  332. #if IS_USE_READMSG_CS
  333. EnterCriticalSection( &m_csReadMsg );
  334. #endif
  335. DataConversion(szDataType, m_szGF_Msg + nIndex, szRecvMsg, nLen, 0, 0);
  336. #if IS_USE_READMSG_CS
  337. LeaveCriticalSection(&m_csReadMsg);
  338. nRet = 0;
  339. #endif
  340. }
  341. return nRet;
  342. }
  343. void EA99::SimulationCommData(void)
  344. {
  345. //GF:"!220V/381V 3P4W 050 220V/381V 3P4W 050 220V/381V 3P4W 050 480 60kVA ."
  346. //F:"#220.0 091 480.0 50.0."
  347. //Q1:"(228.4 228.4 219.3 000 50.0 D.65 00.0 00000000."
  348. //G1:"!546 099 0000 000.3 +00.0 49.9 49.9 49.9."
  349. //G2:"!00000001 00001111 00000000."
  350. //G3:"!228.2/227.1/228.6 226.6/225.2/227.6 219.1/219.1/218.7 000.0/000.0/000.0."
  351. //LOG4C((LOG_NOTICE,"进行数据模拟操作"));
  352. memcpy(m_szQ1_Msg, "(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000.", sizeof("(000.0 000.0 219.0 000 00.0 2.14 16.0 10000000."));
  353. //memcpy(m_szF_Msg, "#235.1 012 231.1 50.2 000.", sizeof("#235.1 012 231.1 50.2 000."));
  354. memcpy(m_szG1_Msg, "!546 099 0000 000.3 +00.0 49.9 49.9 49.9.", sizeof("!546 099 0000 000.3 +00.0 49.9 49.9 49.9."));
  355. memcpy(m_szG2_Msg, "!01000100 00011011 00000000.", sizeof("!01000100 00011011 00000000."));
  356. memcpy(m_szG3_Msg, "!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0.", sizeof("!000.0/000.0/000.0 000.0/000.0/000.0 219.0/219.0/219.0 000.0/000.0/009.0."));
  357. }