ascprocess.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 <= 10010) )
  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. else
  136. {
  137. if( nRet==20005 )
  138. {
  139. LOG4C((LOG_NOTICE, "校验出错:设备:%s, iSpecialFlag = %d,iVarID = %d",pDev->m_strDeviceName,iSpecialFlag,iVarID));
  140. }
  141. else
  142. {
  143. LOG4C((LOG_NOTICE, "出错:设备:%s,nRet = %d,iSpecialFlag = %d,iVarID = %d (%s)",pDev->m_strDeviceName,nRet,iSpecialFlag,iVarID,chMsg));
  144. }
  145. }
  146. return nRet;
  147. }
  148. int AscRequestWrData(char chDevUid[20], int iVarID, double data)//用于请求写命令操作
  149. {
  150. SETBASEPARAM SetBasePara;
  151. memset( &SetBasePara, 0, sizeof(SETBASEPARAM) );
  152. int nDeviceIndex = -1, nVarIndex = -1;
  153. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  154. if( bFind == FALSE ) return -1;
  155. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  156. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  157. SetBasePara.nAddr = pDev->m_iDevideaddr;
  158. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  159. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  160. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  161. if (pASC_DLLWrCom(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, data, pBaseVar->m_iDataLen) == 0)
  162. return 0;
  163. else
  164. return -1;
  165. }
  166. int AscSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
  167. {
  168. int nDeviceIndex = -1, nVarIndex = -1;
  169. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  170. if( bFind == FALSE ) return -1;
  171. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  172. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  173. EnterCriticalSection( &g_csAscReadOneData );
  174. pDev->m_dwOnlineTick = GetTickCount();
  175. LeaveCriticalSection( &g_csAscReadOneData );
  176. int nVarItemID = pBaseVar->m_nVarItemID;
  177. int nDataLen = pBaseVar->m_iDataLen;
  178. int nValue;
  179. double fCoef = pBaseVar->m_dbCoefficient;
  180. //LOG4C((LOG_NOTICE, "%s nVarItemID=%d, nStartBit=%d, nEndBit=%d",chMsg,nVarItemID,pBaseVar->m_nStartBit,pBaseVar->m_nEndBit));
  181. if( nVarItemID > 0 )//BIT位变量
  182. {
  183. int nStartBit = pBaseVar->m_nStartBit;
  184. int nEndBit = pBaseVar->m_nEndBit;
  185. int nStartReg, nEndReg;
  186. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Modbus Ascii码暂时没有碰到,没有经过调试,待测试
  187. {
  188. if( nDataLen == 1 )
  189. {
  190. char chBuffer[8] = {0};
  191. memset(chBuffer, 0, sizeof(chBuffer));
  192. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  193. itoa(nValue, chBuffer, 2);
  194. CString strTemp;
  195. switch( nEndBit - nStartBit )
  196. {
  197. case 0:
  198. strTemp.Format("%c", chBuffer[nStartBit]);
  199. break;
  200. case 1:
  201. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  202. break;
  203. case 2:
  204. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  205. break;
  206. case 3:
  207. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  208. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  209. break;
  210. case 4:
  211. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  212. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  213. break;
  214. case 5:
  215. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  216. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  217. break;
  218. case 6:
  219. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  220. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  221. break;
  222. default:
  223. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  224. }
  225. nValue = atoi(strTemp);
  226. //strTemp.Format("%d",
  227. }
  228. else if( nDataLen == 2 )
  229. {
  230. char chBuffer[16] = {0};
  231. memset(chBuffer, 0, sizeof(chBuffer));
  232. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  233. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  234. itoa(nValue, chBuffer, 2);
  235. //LOG4C((LOG_NOTICE, "nValue 1 = %d , chBuffer=%s",nValue,chBuffer ));
  236. CString strTemp;
  237. switch( nEndBit - nStartBit )
  238. {
  239. case 0:
  240. strTemp.Format("%c", chBuffer[nStartBit]);
  241. break;
  242. case 1:
  243. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  244. break;
  245. case 2:
  246. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  247. break;
  248. case 3:
  249. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  250. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  251. break;
  252. case 4:
  253. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  254. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  255. break;
  256. case 5:
  257. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  258. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  259. break;
  260. case 6:
  261. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  262. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  263. break;
  264. default:
  265. LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
  266. }
  267. nValue = atoi(strTemp);
  268. //LOG4C((LOG_NOTICE, "nValue 2 = %d",nValue));
  269. }
  270. //itoa(ntohs(g_vtMGEEquipData[nEquipIndex].MGEStatus.Status1), buffer, 16);
  271. }
  272. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  273. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  274. {
  275. nStartReg = (int)nStartBit / 8;
  276. nEndReg = (int)(nEndBit + 1) / 8;
  277. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  278. }
  279. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  280. {
  281. nStartReg = (int)nStartBit / 8;
  282. nEndReg = (int)(nEndBit + 1) / 8;
  283. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  284. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  285. }
  286. EnterCriticalSection( &g_csAscReadOneData );
  287. pBaseVar->m_dbData = (double)(nValue * fCoef);
  288. if( pBaseVar->m_nRearm != 0 )
  289. {
  290. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  291. }
  292. LeaveCriticalSection( &g_csAscReadOneData );
  293. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  294. }
  295. else
  296. {
  297. // 联合类型变量做特殊处理
  298. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  299. {
  300. if( nDataLen == 2 )
  301. {
  302. //int类型
  303. union __UNION_VAR_INT{
  304. char ch[2];
  305. int value;
  306. }unionVarInt;
  307. unionVarInt.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  308. unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  309. EnterCriticalSection( &g_csAscReadOneData );
  310. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  311. if( pBaseVar->m_nRearm != 0 )
  312. {
  313. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  314. }
  315. LeaveCriticalSection( &g_csAscReadOneData );
  316. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  317. }
  318. else if( nDataLen == 4 )
  319. {
  320. //float类型
  321. union __UNION_VAR_FLOAT{
  322. char ch[4];
  323. float value;
  324. }unionVarFloat;
  325. unionVarFloat.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  326. unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  327. unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  328. unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  329. EnterCriticalSection( &g_csAscReadOneData );
  330. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  331. if( pBaseVar->m_nRearm != 0 )
  332. {
  333. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  334. }
  335. LeaveCriticalSection( &g_csAscReadOneData );
  336. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  337. }
  338. else if( nDataLen == 8 )
  339. {
  340. //double类型
  341. union __UNION_VAR_DOUBLE{
  342. char ch[8];
  343. double value;
  344. }unionVarDouble;
  345. unionVarDouble.ch[0] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
  346. unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
  347. unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
  348. unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
  349. unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
  350. unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
  351. unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
  352. unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
  353. EnterCriticalSection( &g_csAscReadOneData );
  354. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  355. if( pBaseVar->m_nRearm != 0 )
  356. {
  357. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  358. }
  359. LeaveCriticalSection( &g_csAscReadOneData );
  360. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  361. }
  362. }
  363. else
  364. {
  365. switch( nDataLen )
  366. {
  367. case 1:
  368. nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  369. break;
  370. case 2:
  371. nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  372. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  373. break;
  374. case 3: // 一般为字符串类型,不作处理
  375. case 4: // 保留,暂时不知道什么处理
  376. case 5: // 一般为字符串类型,不作处理
  377. case 6: // 一般为字符串类型,不作处理
  378. case 7: // 一般为字符串类型,不作处理
  379. case 8: // 保留,暂时没有用到
  380. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  381. break;
  382. }
  383. EnterCriticalSection( &g_csAscReadOneData );
  384. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  385. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  386. {
  387. double fScale = 1.0;
  388. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  389. {
  390. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  391. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  392. }
  393. pBaseVar->m_dbData = nValue *
  394. fCoef *
  395. fScale;
  396. }
  397. else
  398. {
  399. pBaseVar->m_dbData = (double)(nValue * fCoef);
  400. }
  401. if( pBaseVar->m_nRearm != 0 )
  402. {
  403. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  404. }
  405. LeaveCriticalSection( &g_csAscReadOneData );
  406. //TRACE2("普通变量ID%d, 值=%d\r\n", iVarID, nValue);
  407. }
  408. }
  409. EnterCriticalSection( &g_csAscReadOneData );
  410. pDev->m_dwOnlineTick = GetTickCount();
  411. LeaveCriticalSection( &g_csAscReadOneData );
  412. return 0;
  413. }
  414. void UnInitAscDll()
  415. {
  416. if (g_hASCLibModule != NULL && ( NULL != pASCDLLUnInit ))
  417. {
  418. pASCDLLUnInit();
  419. }
  420. DeleteCriticalSection( &g_csAscReadOneData );
  421. if (g_hASCLibModule!= NULL)
  422. {
  423. AfxFreeLibrary(g_hASCLibModule);
  424. g_hASCLibModule = NULL;
  425. }
  426. }
  427. double AscSingleResponseWriteData(char chDevUid[20], int iVarID)
  428. {
  429. int nDeviceIndex = -1, nVarIndex = -1;
  430. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  431. if( bFind == FALSE ) return -1;
  432. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  433. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  434. char tmpData[8] = {0};
  435. int iResValue = pASC_DLLRequestWrStatusData(pDev->m_iPort, pDev->m_iDevideaddr, tmpData);
  436. if (iResValue != 0 )
  437. return -1;
  438. int ii=0;
  439. int k=0;
  440. int iNum = pBaseVar->m_iDataLen;
  441. unsigned char *StrValue = new unsigned char[2*iNum];
  442. memset(StrValue, 0, 2*iNum);
  443. while ( ii<4*iNum)
  444. {
  445. WORD wHigh,wLow;
  446. wHigh = AsciiToBYTE(tmpData[ii]);
  447. wLow = AsciiToBYTE(tmpData[ii+1]);
  448. StrValue[k] = ( wHigh << 4 ) | ( wLow & 0x00FF );
  449. ii+=2;
  450. k++;
  451. }
  452. Sleep(1000);
  453. CString strResult;
  454. union unStuctResponse
  455. {
  456. char chValue[4];
  457. float dbValue;
  458. }unStuctResponse;
  459. memset(&unStuctResponse.chValue, 0, sizeof(unStuctResponse.chValue));
  460. memset(unStuctResponse.chValue, 0, sizeof(unStuctResponse.chValue));
  461. memcpy(unStuctResponse.chValue, (const char*)StrValue, 4);
  462. EnterCriticalSection( &g_csAscReadOneData );
  463. pBaseVar->m_dbData = unStuctResponse.dbValue;
  464. LeaveCriticalSection( &g_csAscReadOneData );
  465. CString strTime;
  466. CTime tm;
  467. tm = CTime::GetCurrentTime();
  468. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  469. delete []StrValue;
  470. return 0;
  471. }
  472. int AscSingleResponseSpecialVarData(char chDevUid[20], int iVarID, int iSpecialFlag, char chMsg[80])
  473. {
  474. int nDeviceIndex = -1, nVarIndex = -1;
  475. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  476. if( bFind == FALSE ) return -1;
  477. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  478. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  479. EnterCriticalSection( &g_csAscReadOneData );
  480. pDev->m_dwOnlineTick = GetTickCount();
  481. LeaveCriticalSection( &g_csAscReadOneData );
  482. CString strMemos, strWarnContent;
  483. if (iSpecialFlag == 10000 || iSpecialFlag == 10001)
  484. strWarnContent = SpecLineWarnAndWater(pBaseVar, iSpecialFlag, chMsg);
  485. if (iSpecialFlag == 10002 || iSpecialFlag == 10003)
  486. strWarnContent = SpecLineWeihuMakeWaterDianzhuFz(pBaseVar, iSpecialFlag, chMsg);
  487. if( (iSpecialFlag >= 10007 && iSpecialFlag <= 10010) )
  488. {
  489. strWarnContent = LDSSpecLineWarnAndWater(pBaseVar, iSpecialFlag, chMsg);
  490. }
  491. //if( pBaseVar->m_strDesc=="自循环风机层北侧漏水" )
  492. // LOG4C((LOG_NOTICE, "自循环风机层北侧漏水 iSpecialFlag=%d ,值=%d",iSpecialFlag,(int)pBaseVar->m_dbData));
  493. EnterCriticalSection( &g_csAscReadOneData );
  494. pDev->m_dwOnlineTick = GetTickCount();
  495. LeaveCriticalSection( &g_csAscReadOneData );
  496. return 0;
  497. }
  498. CString LDSSpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  499. {
  500. DWORD nDLWarn = 0;
  501. DWORD nLsPos = 0;
  502. float fLspos = 0;
  503. if (iSpecialFlag == 10007)//LDS线缆1当前电缆告警
  504. {
  505. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  506. EnterCriticalSection( &g_csAscReadOneData );
  507. pBaseVar->m_nStatus = (int)nDLWarn;
  508. LeaveCriticalSection( &g_csAscReadOneData );
  509. EnterCriticalSection( &g_csAscReadOneData );
  510. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  511. LeaveCriticalSection( &g_csAscReadOneData );
  512. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆告警=%d\r\n", pBaseVar->m_nVarID, (int)nDLWarn));
  513. }
  514. else if (iSpecialFlag == 10008)//LDS线缆1漏水位置
  515. {
  516. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  517. if( nDLWarn > 0 )
  518. {
  519. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  520. nLsPos = ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) << 8 ) |
  521. ( ( (AsciiToBYTE(chMsg[6]) << 4) | ( AsciiToBYTE(chMsg[7]) & 0x00FF ) ) & 0x0000FFFF );
  522. EnterCriticalSection( &g_csAscReadOneData );
  523. pBaseVar->m_dbData = (double)nLsPos/10;
  524. LeaveCriticalSection( &g_csAscReadOneData );
  525. }
  526. else
  527. {
  528. EnterCriticalSection( &g_csAscReadOneData );
  529. pBaseVar->m_dbData = 0;
  530. LeaveCriticalSection( &g_csAscReadOneData );
  531. }
  532. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆漏水=%d", pBaseVar->m_nVarID, (int)pBaseVar->m_dbData ));
  533. }
  534. else if (iSpecialFlag == 10009)//LDS线缆2当前电缆告警
  535. {
  536. nDLWarn = (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF );
  537. EnterCriticalSection( &g_csAscReadOneData );
  538. pBaseVar->m_nStatus = (int)nDLWarn;
  539. LeaveCriticalSection( &g_csAscReadOneData );
  540. EnterCriticalSection( &g_csAscReadOneData );
  541. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  542. LeaveCriticalSection( &g_csAscReadOneData );
  543. }
  544. else if (iSpecialFlag == 10010)//LDS线缆2漏水位置
  545. {
  546. nDLWarn = (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF );
  547. if( nDLWarn > 0 )
  548. {
  549. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  550. nLsPos = ( ( (AsciiToBYTE(chMsg[8]) << 4) | ( AsciiToBYTE(chMsg[9]) & 0x00FF ) ) << 8 ) |
  551. ( ( (AsciiToBYTE(chMsg[10]) << 4) | ( AsciiToBYTE(chMsg[11]) & 0x00FF ) ) & 0x0000FFFF );
  552. EnterCriticalSection( &g_csAscReadOneData );
  553. pBaseVar->m_dbData = (double)nLsPos/10;
  554. LeaveCriticalSection( &g_csAscReadOneData );
  555. }
  556. else
  557. {
  558. EnterCriticalSection( &g_csAscReadOneData );
  559. pBaseVar->m_dbData = 0;
  560. LeaveCriticalSection( &g_csAscReadOneData );
  561. }
  562. //LOG4C((LOG_NOTICE, "变量id=%d, 电缆漏水=%d", pBaseVar->m_nVarID, (int)pBaseVar->m_dbData ));
  563. }
  564. CString strWarnContent = "";
  565. return strWarnContent;
  566. }
  567. CString SpecLineWarnAndWater(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  568. {
  569. DWORD nDLWarn = 0;
  570. DWORD nLsPos = 0;
  571. float fLspos = 0;
  572. if (iSpecialFlag == 10000)//电缆告警
  573. {
  574. //memcpy(chReadWarnData, chMsg, sizeof(chReadWarnData));
  575. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  576. EnterCriticalSection( &g_csAscReadOneData );
  577. pBaseVar->m_nStatus = (int)nDLWarn;
  578. LeaveCriticalSection( &g_csAscReadOneData );
  579. nLsPos = ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) << 8 ) |
  580. ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) & 0x0000FFFF );
  581. fLspos = ((float)nLsPos)/10;
  582. EnterCriticalSection( &g_csAscReadOneData );
  583. pBaseVar->m_dbData = (int)nDLWarn;//fLspos;
  584. LeaveCriticalSection( &g_csAscReadOneData );
  585. if( pBaseVar->m_strDesc=="自循环风机托水盘漏水" )
  586. LOG4C((LOG_NOTICE, "自循环风机托水盘漏水 m_nStatus=%d,m_dbData=%d %s", pBaseVar->m_nStatus,(int)pBaseVar->m_dbData,chMsg ));
  587. //TRACE("变量id=%d, 电缆告警=%d\r\n", pBaseVar->m_nVarID, (int)nDLWarn);
  588. }
  589. if (iSpecialFlag == 10001)//电缆漏水
  590. {
  591. nDLWarn = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
  592. if( nDLWarn > 0 )
  593. {
  594. //memcpy(chReadLsPosData, chMsg + 2, sizeof(chReadLsPosData));
  595. nLsPos = ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) << 8 ) |
  596. ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) & 0x0000FFFF );
  597. EnterCriticalSection( &g_csAscReadOneData );
  598. pBaseVar->m_dbData = (double)nLsPos/10;
  599. LeaveCriticalSection( &g_csAscReadOneData );
  600. }
  601. else
  602. {
  603. EnterCriticalSection( &g_csAscReadOneData );
  604. pBaseVar->m_dbData = 0;
  605. LeaveCriticalSection( &g_csAscReadOneData );
  606. }
  607. //TRACE("变量id=%d, 电缆漏水=%.1f\r\n", pBaseVar->m_nVarID, pBaseVar->m_dbData );
  608. }
  609. CString strWarnContent = "";
  610. return strWarnContent;
  611. }
  612. //读取维护、漏水电阻阀值
  613. CString SpecLineWeihuMakeWaterDianzhuFz(CBaseVar *pBaseVar, int iSpecialFlag, char chMsg[80])
  614. {
  615. DWORD nWeihu = 0;
  616. DWORD nMakerwater = 0;
  617. float fValue = 0;
  618. CString strWarnContent = "";
  619. if (iSpecialFlag == 10002)
  620. {
  621. nWeihu = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  622. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  623. fValue = ((float)nWeihu);
  624. //strWarnContent.Format("维护电阻阀值: %f", fValue);
  625. }
  626. if (iSpecialFlag == 10003)
  627. {
  628. nMakerwater = ( ( (AsciiToBYTE(chMsg[4]) << 4) | ( AsciiToBYTE(chMsg[5]) & 0x00FF ) ) << 8 ) |
  629. ( ( (AsciiToBYTE(chMsg[6]) << 4) | ( AsciiToBYTE(chMsg[7]) & 0x00FF ) ) & 0x0000FFFF );
  630. fValue = ((float)nMakerwater);
  631. //strWarnContent.Format("漏水电阻阀值: %f", fValue);
  632. }
  633. EnterCriticalSection( &g_csAscReadOneData );
  634. pBaseVar->m_dbData = fValue;
  635. LeaveCriticalSection ( &g_csAscReadOneData );
  636. return strWarnContent;
  637. }