ascprocess.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #include "stdafx.h"
  2. #include "ascprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include <stdlib.h>
  6. #include "BaseVar.h"
  7. #include "Device.h"
  8. #include <process.h>
  9. #include "NoticeQueue.h"
  10. //#include <Afxdll_.h>
  11. //#ifndef _AFXDLL
  12. //#error file must be compiled with _AFXDLL
  13. //#endif
  14. HANDLE g_ASCResponseThread = NULL;
  15. //HANDLE g_ASCRequestThread[2];
  16. HANDLE g_ASCRequestThread = NULL;
  17. CRITICAL_SECTION g_csAscReadOneData;
  18. BOOL LoadAscDLL(CString strpath, int iNumberdll);
  19. BOOL InitAscComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals);
  20. int AscCommandSend();
  21. int AscResponseValue();
  22. int AscSingleResponseSpecialVarData(char chDevUid[20], int iVarID, int iSpecialFlag);//用于只响应一次命令特殊变量的数据
  23. int AscSingleResponseData(char chDevUid[20], int iVarID);
  24. int AscRequestData(SETBASEPARAM SetBasePara,
  25. char chDataLen[4],
  26. char chPort[4],
  27. char chAddr[4],
  28. char chDevUid[20], int iVarid);//用于只发送一次读命令变量的请求
  29. int AscRequestSpecData(char chDevUid[20], int iVarID, int iSpecialFlag);//用于只发送一次读命令特殊变量的请求
  30. int AscRequestWrData(char chDevUid[20], int iVarID, double data);//用于请求写命令操作
  31. void InitAscDll(SETBASEPARAM SetBasePara, int iPort, int iBaudrate);//
  32. void UnInitAscDll();
  33. int GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState);
  34. CString SpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80]);
  35. CString SpecLineWeihuMakeWaterDianzhuFz(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80]);
  36. CString LDSSpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80]);
  37. //ASC 动态库输出函数
  38. HINSTANCE g_hASCLibModule;// = NULL;
  39. ASC_DLLInitCom pASCDLLInitCom;// = NULL;
  40. ASC_DLLInit pASCDLLInit = NULL;
  41. ASC_DLLUnInit pASCDLLUnInit;// = NULL;
  42. ASC_DLLRequestStatusData pASCDLLRequestStatusData = NULL;
  43. //ASC_DLLDetectOnlineCallBack pASCDLLDetectOnlineCallBack = NULL;
  44. ASC_DLLWrCom pASC_DLLWrCom = NULL;
  45. ASC_DLLRequestWrStatusData pASC_DLLRequestWrStatusData = NULL;
  46. BOOL LoadAscDLL(CString strpath)
  47. {
  48. char strFile[256] = {0};
  49. g_hASCLibModule = NULL;
  50. sprintf(strFile, "%s\\dll\\modbusasc.dll", strpath);
  51. InitializeCriticalSection( &g_csAscReadOneData );
  52. g_hASCLibModule = AfxLoadLibrary(strFile);//::LoadLibrary(strFile);
  53. //ASC 动态库初始化
  54. if (NULL != g_hASCLibModule)
  55. {
  56. pASC_DLLWrCom = (ASC_DLLWrCom)::GetProcAddress(g_hASCLibModule, "ASC_DLLWrCom");
  57. pASCDLLInitCom = (ASC_DLLInitCom)::GetProcAddress(g_hASCLibModule, "ASC_DLLInitCom");
  58. pASCDLLInit = (ASC_DLLInit)::GetProcAddress(g_hASCLibModule, "ASC_DLLInit");
  59. pASCDLLUnInit = (ASC_DLLUnInit)::GetProcAddress(g_hASCLibModule, "ASC_DLLUnInit");
  60. pASCDLLRequestStatusData = (ASC_DLLRequestStatusData)::GetProcAddress(g_hASCLibModule, "ASC_DLLRequestStatusData");
  61. pASC_DLLRequestWrStatusData = (ASC_DLLRequestWrStatusData)::GetProcAddress(g_hASCLibModule, "ASC_DLLRequestWrStatusData");
  62. return TRUE;
  63. }
  64. else
  65. return FALSE;
  66. }
  67. BOOL InitAscComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  68. {
  69. if (pASCDLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  70. return TRUE;
  71. else
  72. return FALSE;
  73. }
  74. int AscRequestData( SETBASEPARAM SetBasePara,
  75. int nDataLen,
  76. int nPort,
  77. int nDevAddr,
  78. char chDevUid[20],
  79. int iVarid)
  80. {
  81. char chMsg[80] = {0};
  82. int nRet = pASCDLLInit(nPort, nDevAddr, SetBasePara, nDataLen, chMsg);
  83. if (nRet==0)
  84. {
  85. nRet = AscSingleResponseData(chDevUid, iVarid, chMsg);
  86. }
  87. return nRet;
  88. }
  89. //用于只发送一次读命令特殊变量的请求
  90. int AscRequestSpecData(char chDevUid[20], int iVarID, int iSpecialFlag)
  91. {
  92. SETBASEPARAM SetBasePara;
  93. char chMsg[80] = {0};
  94. char chDataLen[4] = {0};
  95. int nDeviceIndex = -1;
  96. nDeviceIndex = FindDevice(chDevUid);
  97. if( nDeviceIndex == -1 ) return -1;
  98. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  99. SetBasePara.nAddr = pDev->m_iDevideaddr;
  100. if ((iSpecialFlag == 10000) || (iSpecialFlag == 10001))// 读取维护、漏水电阻阀值
  101. {
  102. SetBasePara.FuncCode = 0x04;
  103. SetBasePara.nStartAddr = 0x02;
  104. SetBasePara.nRegNum = 0x01;
  105. strcpy(chDataLen, "9");
  106. }
  107. else if ((iSpecialFlag == 10002) || (iSpecialFlag == 10003))
  108. {
  109. SetBasePara.FuncCode = 0x03;
  110. SetBasePara.nStartAddr = 0x2C;
  111. SetBasePara.nRegNum = 0x02;
  112. strcpy(chDataLen, "4");
  113. }
  114. else if( (iSpecialFlag >= 10007 && iSpecialFlag <= 10009) )
  115. {
  116. SetBasePara.FuncCode = 0x04;
  117. SetBasePara.nStartAddr = 0x02;
  118. SetBasePara.nRegNum = 0x03;
  119. strcpy(chDataLen, "6");
  120. }
  121. int nRet = pASCDLLInit(pDev->m_iPort,
  122. pDev->m_iDevideaddr,
  123. SetBasePara,
  124. atoi(chDataLen),
  125. chMsg);
  126. if ( nRet == 0)
  127. {
  128. //TRACE("Enter AscSingleResponseSpecialVarData\r\n");
  129. nRet = AscSingleResponseSpecialVarData( chDevUid,
  130. iVarID,
  131. iSpecialFlag,
  132. chMsg);
  133. //TRACE("Leave AscSingleResponseSpecialVarData\r\n");
  134. }
  135. return nRet;
  136. }
  137. int AscRequestWrData(char chDevUid[20], int iVarID, double data)//用于请求写命令操作
  138. {
  139. SETBASEPARAM SetBasePara;
  140. memset( &SetBasePara, 0, sizeof(SETBASEPARAM) );
  141. int nDeviceIndex = -1, nVarIndex = -1;
  142. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  143. if( bFind == FALSE ) return -1;
  144. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  145. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  146. SetBasePara.nAddr = pDev->m_iDevideaddr;
  147. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  148. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  149. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  150. if (pASC_DLLWrCom(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, data, pBaseVar->m_iDataLen) == 0)
  151. return 0;
  152. else
  153. return -1;
  154. }
  155. int AscSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
  156. {
  157. int nDeviceIndex = -1, nVarIndex = -1;
  158. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  159. if( bFind == FALSE ) return -1;
  160. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  161. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  162. EnterCriticalSection( &g_csAscReadOneData );
  163. pDev->m_dwOnlineTick = GetTickCount();
  164. LeaveCriticalSection( &g_csAscReadOneData );
  165. int nVarItemID = pBaseVar->m_nVarItemID;
  166. int nDataLen = pBaseVar->m_iDataLen;
  167. int nValue;
  168. double fCoef = pBaseVar->m_dbCoefficient;
  169. if( nVarItemID > 0 )//BIT位变量
  170. {
  171. int nStartBit = pBaseVar->m_nStartBit;
  172. int nEndBit = pBaseVar->m_nEndBit;
  173. int nStartReg, nEndReg;
  174. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Modbus Ascii码暂时没有碰到,没有经过调试,待测试
  175. {
  176. if( nDataLen == 1 )
  177. {
  178. char chBuffer[8] = {0};
  179. memset(chBuffer, 0, sizeof(chBuffer));
  180. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  181. itoa(nValue, chBuffer, 2);
  182. CString strTemp;
  183. switch( nEndBit - nStartBit )
  184. {
  185. case 0:
  186. strTemp.Format("%c", chBuffer[nStartBit]);
  187. break;
  188. case 1:
  189. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  190. break;
  191. case 2:
  192. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  193. break;
  194. case 3:
  195. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  196. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  197. break;
  198. case 4:
  199. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  200. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  201. break;
  202. case 5:
  203. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  204. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  205. break;
  206. case 6:
  207. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  208. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  209. break;
  210. }
  211. nValue = atoi(strTemp);
  212. //strTemp.Format("%d",
  213. }
  214. else if( nDataLen == 2 )
  215. {
  216. char chBuffer[16] = {0};
  217. memset(chBuffer, 0, sizeof(chBuffer));
  218. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  219. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  220. itoa(nValue, chBuffer, 2);
  221. CString strTemp;
  222. switch( nEndBit - nStartBit )
  223. {
  224. case 0:
  225. strTemp.Format("%c", chBuffer[nStartBit]);
  226. break;
  227. case 1:
  228. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  229. break;
  230. case 2:
  231. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  232. break;
  233. case 3:
  234. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  235. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  236. break;
  237. case 4:
  238. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  239. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  240. break;
  241. case 5:
  242. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  243. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  244. break;
  245. case 6:
  246. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  247. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  248. break;
  249. }
  250. nValue = atoi(strTemp);
  251. }
  252. //itoa(ntohs(g_vtMGEEquipData[nEquipIndex].MGEStatus.Status1), buffer, 16);
  253. }
  254. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  255. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  256. {
  257. nStartReg = (int)nStartBit / 8;
  258. nEndReg = (int)(nEndBit + 1) / 8;
  259. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  260. }
  261. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  262. {
  263. nStartReg = (int)nStartBit / 8;
  264. nEndReg = (int)(nEndBit + 1) / 8;
  265. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  266. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  267. }
  268. EnterCriticalSection( &g_csAscReadOneData );
  269. pBaseVar->m_dbData = (double)(nValue * fCoef);
  270. if( pBaseVar->m_nRearm != 0 )
  271. {
  272. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  273. }
  274. LeaveCriticalSection( &g_csAscReadOneData );
  275. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  276. }
  277. else
  278. {
  279. // 联合类型变量做特殊处理
  280. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  281. {
  282. if( nDataLen == 2 )
  283. {
  284. //int类型
  285. union __UNION_VAR_INT{
  286. char ch[2];
  287. int value;
  288. }unionVarInt;
  289. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  290. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  291. EnterCriticalSection( &g_csAscReadOneData );
  292. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  293. if( pBaseVar->m_nRearm != 0 )
  294. {
  295. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  296. }
  297. LeaveCriticalSection( &g_csAscReadOneData );
  298. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  299. }
  300. else if( nDataLen == 4 )
  301. {
  302. //float类型
  303. union __UNION_VAR_FLOAT{
  304. char ch[4];
  305. float value;
  306. }unionVarFloat;
  307. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  308. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  309. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  310. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  311. EnterCriticalSection( &g_csAscReadOneData );
  312. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  313. if( pBaseVar->m_nRearm != 0 )
  314. {
  315. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  316. }
  317. LeaveCriticalSection( &g_csAscReadOneData );
  318. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  319. }
  320. else if( nDataLen == 8 )
  321. {
  322. //double类型
  323. union __UNION_VAR_DOUBLE{
  324. char ch[8];
  325. double value;
  326. }unionVarDouble;
  327. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  328. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  329. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  330. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  331. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  332. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  333. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  334. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  335. EnterCriticalSection( &g_csAscReadOneData );
  336. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  337. if( pBaseVar->m_nRearm != 0 )
  338. {
  339. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  340. }
  341. LeaveCriticalSection( &g_csAscReadOneData );
  342. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  343. }
  344. }
  345. else
  346. {
  347. switch( nDataLen )
  348. {
  349. case 1:
  350. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  351. break;
  352. case 2:
  353. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  354. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  355. break;
  356. case 3: // 一般为字符串类型,不作处理
  357. break;
  358. case 4: // 保留,暂时不知道什么处理
  359. break;
  360. case 5: // 一般为字符串类型,不作处理
  361. break;
  362. case 6: // 一般为字符串类型,不作处理
  363. break;
  364. case 7: // 一般为字符串类型,不作处理
  365. break;
  366. case 8: // 保留,暂时没有用到
  367. break;
  368. }
  369. EnterCriticalSection( &g_csAscReadOneData );
  370. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  371. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  372. {
  373. double fScale = 1.0;
  374. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  375. {
  376. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  377. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  378. }
  379. pBaseVar->m_dbData = nValue *
  380. fCoef *
  381. fScale;
  382. }
  383. else
  384. {
  385. pBaseVar->m_dbData = (double)(nValue * fCoef);
  386. }
  387. if( pBaseVar->m_nRearm != 0 )
  388. {
  389. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  390. }
  391. LeaveCriticalSection( &g_csAscReadOneData );
  392. //TRACE2("普通变量ID%d, 值=%d\r\n", iVarID, nValue);
  393. }
  394. }
  395. EnterCriticalSection( &g_csAscReadOneData );
  396. pDev->m_dwOnlineTick = GetTickCount();
  397. LeaveCriticalSection( &g_csAscReadOneData );
  398. return 0;
  399. }
  400. void UnInitAscDll()
  401. {
  402. if (g_hASCLibModule != NULL && ( NULL != pASCDLLUnInit ))
  403. {
  404. pASCDLLUnInit();
  405. }
  406. DeleteCriticalSection( &g_csAscReadOneData );
  407. if (g_hASCLibModule!= NULL)
  408. {
  409. AfxFreeLibrary(g_hASCLibModule);
  410. g_hASCLibModule = NULL;
  411. }
  412. }
  413. double AscSingleResponseWriteData(char chDevUid[20], int iVarID)
  414. {
  415. int nDeviceIndex = -1, nVarIndex = -1;
  416. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  417. if( bFind == FALSE ) return -1;
  418. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  419. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  420. char tmpData[8] = {0};
  421. int iResValue = pASC_DLLRequestWrStatusData(pDev->m_iPort, pDev->m_iDevideaddr, tmpData);
  422. if (iResValue != 0 )
  423. return -1;
  424. int ii=0;
  425. int k=0;
  426. int iNum = pBaseVar->m_iDataLen;
  427. unsigned char *StrValue = new unsigned char[2*iNum];
  428. memset(StrValue, 0, 2*iNum);
  429. while ( ii<4*iNum)
  430. {
  431. WORD wHigh,wLow;
  432. wHigh = AsciiToBYTE(tmpData[ii]);
  433. wLow = AsciiToBYTE(tmpData[ii+1]);
  434. StrValue[k] = ( wHigh << 4 ) | ( wLow & 0x00FF );
  435. ii+=2;
  436. k++;
  437. }
  438. Sleep(1000);
  439. CString strResult;
  440. union unStuctResponse
  441. {
  442. char chValue[4];
  443. float dbValue;
  444. }unStuctResponse;
  445. memset(&unStuctResponse.chValue, 0, sizeof(unStuctResponse.chValue));
  446. memset(unStuctResponse.chValue, 0, sizeof(unStuctResponse.chValue));
  447. memcpy(unStuctResponse.chValue, (const char*)StrValue, 4);
  448. EnterCriticalSection( &g_csAscReadOneData );
  449. pBaseVar->m_dbData = unStuctResponse.dbValue;
  450. LeaveCriticalSection( &g_csAscReadOneData );
  451. CString strTime;
  452. CTime tm;
  453. tm = CTime::GetCurrentTime();
  454. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  455. delete []StrValue;
  456. return 0;
  457. }
  458. int AscSingleResponseSpecialVarData(char chDevUid[20], int iVarID, int iSpecialFlag, char chMsg[80])
  459. {
  460. int nDeviceIndex = -1, nVarIndex = -1;
  461. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  462. if( bFind == FALSE ) return -1;
  463. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  464. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  465. EnterCriticalSection( &g_csAscReadOneData );
  466. pDev->m_dwOnlineTick = GetTickCount();
  467. LeaveCriticalSection( &g_csAscReadOneData );
  468. CString strMemos, strWarnContent;
  469. if (iSpecialFlag == 10000 || iSpecialFlag == 10001)
  470. strWarnContent = SpecLineWarnAndWater(pBaseVar, iSpecialFlag, chMsg);
  471. if (iSpecialFlag == 10002 || iSpecialFlag == 10003)
  472. strWarnContent = SpecLineWeihuMakeWaterDianzhuFz(pBaseVar, iSpecialFlag, chMsg);
  473. if( (iSpecialFlag >= 10007 && iSpecialFlag <= 10009) )
  474. {
  475. strWarnContent = LDSSpecLineWarnAndWater(pBaseVar, iSpecialFlag, chMsg);
  476. }
  477. EnterCriticalSection( &g_csAscReadOneData );
  478. pDev->m_dwOnlineTick = GetTickCount();
  479. LeaveCriticalSection( &g_csAscReadOneData );
  480. return 0;
  481. }
  482. CString LDSSpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  483. {
  484. DWORD nDLWarn = 0;
  485. DWORD nLsPos = 0;
  486. float fLspos = 0;
  487. if (iSpecialFlag == 10007)//LDS线缆1当前电缆告警
  488. {
  489. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  490. EnterCriticalSection( &g_csAscReadOneData );
  491. pBaseVar->m_nStatus = (int)nDLWarn;
  492. LeaveCriticalSection( &g_csAscReadOneData );
  493. EnterCriticalSection( &g_csAscReadOneData );
  494. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  495. LeaveCriticalSection( &g_csAscReadOneData );
  496. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆告警=%d\r\n", pBaseVar->m_nVarID, (int)nDLWarn));
  497. }
  498. else if (iSpecialFlag == 10008)//LDS线缆1漏水位置
  499. {
  500. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  501. if( nDLWarn > 0 )
  502. {
  503. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  504. nLsPos = ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) << 8 ) |
  505. ( ( (AsciiToBYTE(chMsg[6]) << 4) | ( AsciiToBYTE(chMsg[7]) & 0x00FF ) ) & 0x0000FFFF );
  506. EnterCriticalSection( &g_csAscReadOneData );
  507. pBaseVar->m_dbData = (double)nLsPos/10;
  508. LeaveCriticalSection( &g_csAscReadOneData );
  509. }
  510. else
  511. {
  512. EnterCriticalSection( &g_csAscReadOneData );
  513. pBaseVar->m_dbData = 0;
  514. LeaveCriticalSection( &g_csAscReadOneData );
  515. }
  516. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆漏水=%d", pBaseVar->m_nVarID, (int)pBaseVar->m_dbData ));
  517. }
  518. else if (iSpecialFlag == 10009)//LDS线缆2当前电缆告警
  519. {
  520. nDLWarn = (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF );
  521. EnterCriticalSection( &g_csAscReadOneData );
  522. pBaseVar->m_nStatus = (int)nDLWarn;
  523. LeaveCriticalSection( &g_csAscReadOneData );
  524. EnterCriticalSection( &g_csAscReadOneData );
  525. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  526. LeaveCriticalSection( &g_csAscReadOneData );
  527. }
  528. else if (iSpecialFlag == 10010)//LDS线缆2漏水位置
  529. {
  530. nDLWarn = (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF );
  531. if( nDLWarn > 0 )
  532. {
  533. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  534. nLsPos = ( ( (AsciiToBYTE(chMsg[8]) << 4) | ( AsciiToBYTE(chMsg[9]) & 0x00FF ) ) << 8 ) |
  535. ( ( (AsciiToBYTE(chMsg[10]) << 4) | ( AsciiToBYTE(chMsg[11]) & 0x00FF ) ) & 0x0000FFFF );
  536. EnterCriticalSection( &g_csAscReadOneData );
  537. pBaseVar->m_dbData = (double)nLsPos/10;
  538. LeaveCriticalSection( &g_csAscReadOneData );
  539. }
  540. else
  541. {
  542. EnterCriticalSection( &g_csAscReadOneData );
  543. pBaseVar->m_dbData = 0;
  544. LeaveCriticalSection( &g_csAscReadOneData );
  545. }
  546. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆漏水=%d", pBaseVar->m_nVarID, (int)pBaseVar->m_dbData ));
  547. }
  548. CString strWarnContent = "";
  549. return strWarnContent;
  550. }
  551. CString SpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  552. {
  553. DWORD nDLWarn = 0;
  554. DWORD nLsPos = 0;
  555. float fLspos = 0;
  556. if (iSpecialFlag == 10000)//电缆告警
  557. {
  558. //memcpy(chReadWarnData, chMsg, sizeof(chReadWarnData));
  559. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  560. EnterCriticalSection( &g_csAscReadOneData );
  561. pBaseVar->m_nStatus = (int)nDLWarn;
  562. LeaveCriticalSection( &g_csAscReadOneData );
  563. nLsPos = ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) << 8 ) |
  564. ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) & 0x0000FFFF );
  565. fLspos = ((float)nLsPos)/10;
  566. EnterCriticalSection( &g_csAscReadOneData );
  567. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  568. LeaveCriticalSection( &g_csAscReadOneData );
  569. //TRACE("变量id=%d, 电缆告警=%d\r\n", pBaseVar->m_nVarID, (int)nDLWarn);
  570. }
  571. if (iSpecialFlag == 10001)//电缆漏水
  572. {
  573. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  574. if( nDLWarn > 0 )
  575. {
  576. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  577. nLsPos = ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) << 8 ) |
  578. ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) & 0x0000FFFF );
  579. EnterCriticalSection( &g_csAscReadOneData );
  580. pBaseVar->m_dbData = (double)nLsPos/10;
  581. LeaveCriticalSection( &g_csAscReadOneData );
  582. }
  583. else
  584. {
  585. EnterCriticalSection( &g_csAscReadOneData );
  586. pBaseVar->m_dbData = 0;
  587. LeaveCriticalSection( &g_csAscReadOneData );
  588. }
  589. //TRACE("变量id=%d, 电缆漏水=%.1f\r\n", pBaseVar->m_nVarID, pBaseVar->m_dbData );
  590. }
  591. CString strWarnContent = "";
  592. return strWarnContent;
  593. }
  594. //读取维护、漏水电阻阀值
  595. CString SpecLineWeihuMakeWaterDianzhuFz(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  596. {
  597. DWORD nWeihu = 0;
  598. DWORD nMakerwater = 0;
  599. float fValue = 0;
  600. CString strWarnContent = "";
  601. if (iSpecialFlag == 10002)
  602. {
  603. nWeihu = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  604. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  605. fValue = ((float)nWeihu);
  606. //strWarnContent.Format("维护电阻阀值: %f", fValue);
  607. }
  608. if (iSpecialFlag == 10003)
  609. {
  610. nMakerwater = ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) << 8 ) |
  611. ( ( (AsciiToBYTE(chMsg[6]) << 4) | ( AsciiToBYTE(chMsg[7]) & 0x00FF ) ) & 0x0000FFFF );
  612. fValue = ((float)nMakerwater);
  613. //strWarnContent.Format("漏水电阻阀值: %f", fValue);
  614. }
  615. EnterCriticalSection( &g_csAscReadOneData );
  616. pBaseVar->m_dbData = fValue;
  617. LeaveCriticalSection ( &g_csAscReadOneData );
  618. return strWarnContent;
  619. }