ascprocess.cpp 24 KB

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