EA800.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "StdAfx.h"
  2. #include ".\ea800.h"
  3. CEA800::CEA800(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. CEA800::~CEA800(void)
  19. {
  20. #if IS_USE_READMSG_CS
  21. DeleteCriticalSection(& m_csReadMsg);
  22. #endif
  23. MTVERIFY(CloseHandle(m_hSemComm));
  24. CloseComm();
  25. }
  26. BOOL CEA800::EA800OpenComm(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. int CEA800::SendReadRequest(
  33. char *szPath, // 程序所在路径
  34. char *szIniName, // 配置文件名称
  35. int nCommPort, // 串行端口
  36. int nAddr, // 设备地址
  37. char *szCmd, // 请求命令
  38. char *szMsg, // 响应的值
  39. int nReversed1, // 预留整形参数1接口
  40. int nReversed2, // 预留整形参数2接口
  41. int nReversed3, // 预留整形参数3接口
  42. int nReversed4, // 预留整形参数4接口
  43. int nReversed5, // 预留整形参数5接口
  44. float fReversed1, // 预留float参数1接口
  45. float fReversed2, // 预留float参数2接口
  46. float fReversed3, // 预留float参数3接口
  47. char *szReversed1, // 预留字符数组参数1接口
  48. char *szReversed2, // 预留字符数组参数2接口
  49. char *szReversed3, // 预留字符数组参数3接口
  50. char *szReversed4, // 预留字符数组参数4接口
  51. char *szReversed5 // 预留字符数组参数5接口
  52. )
  53. {
  54. CCommProcess *pComm = FindComm(nCommPort);
  55. if( pComm == NULL ) return -1;
  56. int nRet = -1;
  57. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  58. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  59. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  60. if(
  61. ( strlen(m_szG1_Msg) == 0 && strcmp(IniSendCMD,"G1") == 0 ) || strcmp(szCmd, "cmd-1") == 0 ||
  62. ( strlen(m_szG2_Msg) == 0 && strcmp(IniSendCMD,"G2") == 0 ) || strcmp(szCmd, "cmd-9") == 0 ||
  63. ( strlen(m_szG3_Msg) == 0 && strcmp(IniSendCMD,"G3") == 0 ) || strcmp(szCmd, "cmd-28") == 0 ||
  64. ( strlen(m_szQ1_Msg) == 0 && strcmp(IniSendCMD,"Q1") == 0 ) || strcmp(szCmd,"cmd-40") == 0 ||
  65. ( strlen(m_szF_Msg) == 0 && strcmp(IniSendCMD,"F") == 0 ) || strcmp(szCmd, "cmd-55") == 0
  66. )
  67. {
  68. nRet = GetDeviceParam(nAddr,pComm,IniSendCMD,IniSendlen);
  69. if( nRet != 0 )
  70. {
  71. LOG4C((LOG_NOTICE,"收发数据失败"));
  72. return nRet;
  73. }
  74. }
  75. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  76. m_devOnline = FALSE;
  77. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  78. m_devOnline = TRUE;
  79. if( m_devOnline == FALSE )
  80. return -1;
  81. nRet = GetEA800_G1Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  82. nRet = GetEA800_G2Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  83. nRet = GetEA800_G3Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  84. nRet = GetEA800_Q1Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  85. nRet = GetEA800_FMsg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  86. nRet = GetEA800_GFMsg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit );
  87. //LOG4C((LOG_NOTICE, "%s = %s", szCmd, szMsg));
  88. return nRet;
  89. }
  90. int CEA800::GetDeviceParam( int nAddr,CCommProcess *pComm, char *IniSendCMD,const int &IniSendlen)
  91. {
  92. int nRet = -1;
  93. nRet = Send_ReadDeviceData(nAddr,pComm,IniSendCMD,IniSendlen);
  94. if( nRet != 0 )
  95. return nRet;
  96. nRet = Recv_ReadDeviceData(nAddr,pComm,IniSendCMD);
  97. return nRet;
  98. }
  99. // IniSendCMD 2个byte;
  100. int CEA800::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen)
  101. {
  102. #if DEBUG_EA800
  103. BYTE bySend[4] = {0};
  104. memcpy(bySend,IniSendCMD,IniSendlen);
  105. bySend[IniSendlen] = 0x0D;
  106. int nDatalen = IniSendlen+1;
  107. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  108. {
  109. ResetEvent(m_hSemComm);
  110. int nResult = pComm->Write(bySend, nDatalen);
  111. if (nResult != nDatalen)
  112. {
  113. SetEvent(m_hSemComm);
  114. LOG4C((LOG_NOTICE,"实际写长度不一致"));
  115. return EER_CODE_EA800_COM_REGNUM;
  116. }
  117. }
  118. else
  119. {
  120. LOG4C((LOG_NOTICE,"线程信号外"));
  121. return ERR_CODE_EA800_COM_BUSY;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. int CEA800::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szSendCMD)
  127. {
  128. #if DEBUG_EA800
  129. int nReceiveLen = 260;
  130. int nReadLen = 0;
  131. char *pBuff = new char[nReceiveLen];
  132. memset(pBuff, 0 , nReceiveLen);
  133. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  134. if (nReadLen <= 0)
  135. {
  136. SetEvent(m_hSemComm);
  137. if (pBuff != NULL)
  138. {
  139. delete [] pBuff;
  140. pBuff = NULL;
  141. }
  142. LOG4C((LOG_NOTICE,"接收数据长度 = 0"));
  143. return ERR_CODE_EA800_COM_READ_NO_DATA;
  144. } //长度校验出错 ;
  145. SetEA800_G1Msg(nAddr, szSendCMD, pBuff);
  146. SetEA800_G2Msg(nAddr, szSendCMD, pBuff);
  147. SetEA800_G3Msg(nAddr, szSendCMD, pBuff);
  148. SetEA800_Q1Msg(nAddr, szSendCMD, pBuff);
  149. SetEA800_FMsg( nAddr, szSendCMD, pBuff);
  150. SetEA800_GFMsg(nAddr, szSendCMD, pBuff);
  151. m_dwOnlineTick = GetTickCount();
  152. SetEvent( m_hSemComm );
  153. if( pBuff != NULL)
  154. {
  155. delete[] pBuff;
  156. pBuff = NULL;
  157. }
  158. #else
  159. SimulationCommData();
  160. #endif
  161. return 0;
  162. }
  163. void CEA800::SetEA800_G1Msg( int nAddr, char *szSendCMD,char *pBuffer)
  164. {
  165. if ( strcmp(szSendCMD,"G1") == 0 )
  166. {
  167. #if IS_USE_READMSG_CS
  168. EnterCriticalSection(&m_csReadMsg);
  169. #endif
  170. memcpy(m_szG1_Msg, pBuffer, sizeof(m_szG1_Msg));
  171. #if IS_USE_READMSG_CS
  172. LeaveCriticalSection(&m_csReadMsg);
  173. #endif
  174. }
  175. }
  176. void CEA800::SetEA800_G2Msg( int nAddr, char *szSendCMD, char *pBuffer)
  177. {
  178. if ( strcmp(szSendCMD,"G2") == 0 )
  179. {
  180. #if IS_USE_READMSG_CS
  181. EnterCriticalSection(&m_csReadMsg);
  182. #endif
  183. memcpy(m_szG2_Msg, pBuffer, sizeof(m_szG2_Msg));
  184. #if IS_USE_READMSG_CS
  185. LeaveCriticalSection(&m_csReadMsg);
  186. #endif
  187. }
  188. }
  189. void CEA800::SetEA800_G3Msg( int nAddr, char *szSendCMD, char *pBuffer)
  190. {
  191. if ( strcmp(szSendCMD,"G3") == 0 )
  192. {
  193. #if IS_USE_READMSG_CS
  194. EnterCriticalSection(&m_csReadMsg);
  195. #endif
  196. memcpy(m_szG3_Msg, pBuffer, sizeof(m_szG3_Msg));
  197. #if IS_USE_READMSG_CS
  198. LeaveCriticalSection(&m_csReadMsg);
  199. #endif
  200. }
  201. }
  202. void CEA800::SetEA800_Q1Msg( int nAddr, char *szSendCMD,char *pBuffer)
  203. {
  204. if ( strcmp(szSendCMD,"Q1") == 0 )
  205. {
  206. #if IS_USE_READMSG_CS
  207. EnterCriticalSection(&m_csReadMsg);
  208. #endif
  209. memcpy(m_szQ1_Msg, pBuffer, sizeof(m_szQ1_Msg));
  210. #if IS_USE_READMSG_CS
  211. LeaveCriticalSection(&m_csReadMsg);
  212. #endif
  213. }
  214. }
  215. void CEA800::SetEA800_FMsg( int nAddr, char *szSendCMD,char *pBuffer)
  216. {
  217. if ( strcmp(szSendCMD,"F") == 0 )
  218. {
  219. #if IS_USE_READMSG_CS
  220. EnterCriticalSection(&m_csReadMsg);
  221. #endif
  222. memcpy(m_szF_Msg, pBuffer, sizeof(m_szF_Msg));
  223. #if IS_USE_READMSG_CS
  224. LeaveCriticalSection(&m_csReadMsg);
  225. #endif
  226. }
  227. }
  228. void CEA800::SetEA800_GFMsg( int nAddr, char *szSendCMD, char *pBuffer)
  229. {
  230. if ( strcmp(szSendCMD,"GF") == 0 )
  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 CEA800::GetEA800_G1Msg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  242. {
  243. int nRet = 0;
  244. if ( strcmp(szSendCMD,"G1") == 0 )
  245. {
  246. #if IS_USE_READMSG_CS
  247. EnterCriticalSection(&m_csReadMsg);
  248. #endif
  249. DataConversion(szType, m_szG1_Msg + nIndex, szMsg, nLen, 0, 0);
  250. #if IS_USE_READMSG_CS
  251. LeaveCriticalSection(&m_csReadMsg);
  252. #endif
  253. nRet = 0;
  254. }
  255. return nRet;
  256. }
  257. int CEA800::GetEA800_G2Msg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  258. {
  259. int nRet = 0;
  260. if ( strcmp(szSendCMD,"G2") == 0 )
  261. {
  262. #if IS_USE_READMSG_CS
  263. EnterCriticalSection(&m_csReadMsg);
  264. #endif
  265. DataConversion(szType, m_szG2_Msg + nIndex, szMsg, nLen, 0, 0);
  266. #if IS_USE_READMSG_CS
  267. LeaveCriticalSection(&m_csReadMsg);
  268. #endif
  269. nRet = 0;
  270. }
  271. return nRet;
  272. }
  273. int CEA800::GetEA800_G3Msg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  274. {
  275. int nRet = 0;
  276. if ( strcmp(szSendCMD,"G3") == 0 )
  277. {
  278. #if IS_USE_READMSG_CS
  279. EnterCriticalSection(&m_csReadMsg);
  280. #endif
  281. DataConversion(szType, m_szG3_Msg + nIndex, szMsg, nLen, 0, 0);
  282. #if IS_USE_READMSG_CS
  283. LeaveCriticalSection(&m_csReadMsg);
  284. #endif
  285. nRet = 0;
  286. }
  287. return nRet;
  288. }
  289. int CEA800::GetEA800_Q1Msg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  290. {
  291. int nRet = 0;
  292. if ( strcmp(szSendCMD,"Q1") == 0 )
  293. {
  294. #if IS_USE_READMSG_CS
  295. EnterCriticalSection(&m_csReadMsg);
  296. #endif
  297. DataConversion(szType, m_szQ1_Msg + nIndex, szMsg, nLen, 0, 0);
  298. #if IS_USE_READMSG_CS
  299. LeaveCriticalSection(&m_csReadMsg);
  300. #endif
  301. nRet = 0;
  302. }
  303. return nRet;
  304. }
  305. int CEA800::GetEA800_FMsg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  306. {
  307. int nRet = 0;
  308. if ( strcmp(szSendCMD,"F") == 0 )
  309. {
  310. #if IS_USE_READMSG_CS
  311. EnterCriticalSection(&m_csReadMsg);
  312. #endif
  313. DataConversion(szType, m_szF_Msg + nIndex, szMsg, nLen, 0, 0);
  314. #if IS_USE_READMSG_CS
  315. LeaveCriticalSection(&m_csReadMsg);
  316. #endif
  317. nRet = 0;
  318. }
  319. return nRet;
  320. }
  321. int CEA800::GetEA800_GFMsg( int nAddr,char *szSendCMD,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  322. {
  323. int nRet = 0;
  324. if ( strcmp(szSendCMD,"GF") == 0 )
  325. {
  326. #if IS_USE_READMSG_CS
  327. EnterCriticalSection(&m_csReadMsg);
  328. #endif
  329. DataConversion(szType, m_szGF_Msg + nIndex, szMsg, nLen, 0, 0);
  330. #if IS_USE_READMSG_CS
  331. LeaveCriticalSection(&m_csReadMsg);
  332. #endif
  333. nRet = 0;
  334. }
  335. return nRet;
  336. }
  337. void CEA800::SimulationCommData(void)
  338. {
  339. //LOG4C((LOG_NOTICE,"进行数据模拟操作"));
  340. 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."));
  341. //memcpy(m_szF_Msg, "#235.1 012 231.1 50.2 000.", sizeof("#235.1 012 231.1 50.2 000."));
  342. memcpy(m_szG1_Msg, "!372 067 0752 006.5 000.0 00.0 00.0 49.9.", sizeof("!372 067 0752 006.5 000.0 00.0 00.0 49.9."));
  343. memcpy(m_szG2_Msg, "!01000100 00011011 00000000.", sizeof("!01000100 00011011 00000000."));
  344. 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."));
  345. }
  346. //F: #235.1 012 231.1 50.2 000.
  347. //Q1:(235.2 000.0 245.3 015 49.9 5.66 26.3 10001001 000.