rtuprocess.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #include "stdafx.h"
  2. #include "rtuprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. #include "SysLib.h"
  7. HANDLE g_RTURequestThread = NULL;
  8. HANDLE g_RTUResponseThread = NULL;
  9. DWORD g_dwRtuVariantTick = 0;
  10. CRITICAL_SECTION g_csRtuReadOneData;
  11. //rtu动态库输出函数
  12. HINSTANCE g_hRTULibModule = NULL;
  13. RTU_DLLInitCom pRTU_DLLInitCom = NULL;
  14. RTU_DLLInit pRTU_DLLInit = NULL;
  15. RTU_DLLUnInit pRTU_DLLUnInit = NULL;
  16. RTU_DLLWrite pRTU_DLLWrite = NULL;
  17. RTU_DLLRequestWrStatusData pRTU_DLLRequestWrStatusData = NULL;
  18. int RtuResponseValue();
  19. int RtuCommandSend();
  20. void UnInitRtuDll();
  21. BOOL LoadRtuDll(CString strpath)
  22. {
  23. char strFile[256] = {0};
  24. g_hRTULibModule = NULL;
  25. sprintf(strFile, "%s\\dll\\modbusrtu.dll", strpath);
  26. InitializeCriticalSection( &g_csRtuReadOneData );
  27. g_hRTULibModule = AfxLoadLibrary(strFile);
  28. //RTU 动态库初始化
  29. if (NULL != g_hRTULibModule)
  30. {
  31. pRTU_DLLInitCom =( RTU_DLLInitCom)::GetProcAddress(g_hRTULibModule, "RTU_DLLInitCom");
  32. pRTU_DLLInit = (RTU_DLLInit)::GetProcAddress(g_hRTULibModule, "RTU_DLLInit");
  33. pRTU_DLLUnInit = (RTU_DLLUnInit)::GetProcAddress(g_hRTULibModule, "RTU_DLLUnInit");
  34. pRTU_DLLWrite = (RTU_DLLWrite)::GetProcAddress(g_hRTULibModule, "RTU_DLLWrite");
  35. pRTU_DLLRequestWrStatusData = (RTU_DLLRequestWrStatusData)::GetProcAddress(g_hRTULibModule, "RTU_DLLRequestWrStatusData");
  36. return TRUE;
  37. }
  38. else
  39. {
  40. return FALSE;
  41. }
  42. }
  43. BOOL InitRtuComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  44. {
  45. if( NULL != pRTU_DLLInitCom )
  46. {
  47. //LOG4C((LOG_NOTICE, "Enter pRTU_DLLInitCom"));
  48. if (pRTU_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  49. return TRUE;
  50. else
  51. return FALSE;
  52. }
  53. else
  54. {
  55. LOG4C((LOG_NOTICE, "pRTU_DLLInitCom is null!"));
  56. return FALSE;
  57. }
  58. }
  59. //用于只发送一次读命令特殊变量的请求
  60. int RtuRequestSpecData(SETBASEPARAM SetBasePara,
  61. int nDataLen,
  62. int nPort,
  63. int nDevAddr,
  64. char chDevUid[20],
  65. int iVarID,
  66. int iSpecialFlag)
  67. {
  68. char chMsg[80] = {0};
  69. int nRet = pRTU_DLLInit(nPort,
  70. nDevAddr,
  71. SetBasePara,
  72. nDataLen,
  73. chMsg);
  74. if ( nRet == 0)
  75. {
  76. //TRACE("Enter AscSingleResponseSpecialVarData\r\n");
  77. nRet = RtuSingleResponseSpecialVarData( chDevUid,
  78. iVarID,
  79. iSpecialFlag,
  80. chMsg);
  81. //TRACE("Leave AscSingleResponseSpecialVarData\r\n");
  82. }
  83. else
  84. {
  85. //TRACE("Rtu设备请求失败,变量ID=%d\r\n", iVarID);
  86. }
  87. //TRACE("Rtu设备请求成功\r\n");
  88. return nRet;
  89. }
  90. int RtuSingleResponseSpecialVarData(char chDevUid[20], int iVarID, int iSpecialFlag, char chMsg[80])
  91. {
  92. int nDeviceIndex = -1, nVarIndex = -1;
  93. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  94. if( bFind == FALSE ) return -1;
  95. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  96. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  97. EnterCriticalSection( &g_csRtuReadOneData );
  98. pDev->m_dwOnlineTick = GetTickCount();
  99. LeaveCriticalSection( &g_csRtuReadOneData );
  100. int nValue = 0;
  101. CString strMemos, strWarnContent;
  102. double fCoef = pBaseVar->m_dbCoefficient;
  103. if (iSpecialFlag == 10004)
  104. {
  105. short int wdValue;
  106. memcpy(&wdValue, chMsg, sizeof(short int));
  107. nValue = ntohs(wdValue);
  108. //pBaseVar->m_dbData = (double)(nValue * fCoef);
  109. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  110. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  111. {
  112. double fScale = 1.0;
  113. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  114. {
  115. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  116. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  117. }
  118. if( nValue < 0.0 )
  119. {
  120. pBaseVar->m_dbData = 0.0;
  121. }
  122. else
  123. {
  124. pBaseVar->m_dbData = nValue * fCoef * fScale;
  125. }
  126. }
  127. else
  128. {
  129. if( nValue < 0.0 )
  130. {
  131. pBaseVar->m_dbData = 0.0;
  132. }
  133. else
  134. {
  135. pBaseVar->m_dbData = (double)(nValue * fCoef);
  136. }
  137. }
  138. if( pBaseVar->m_nRearm != 0 )
  139. {
  140. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  141. }
  142. }
  143. if (iSpecialFlag == 10005)
  144. {
  145. short int wdValue;
  146. memcpy(&wdValue, chMsg+2, sizeof(short int));
  147. nValue = ntohs(wdValue);
  148. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  149. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  150. {
  151. double fScale = 1.0;
  152. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  153. {
  154. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  155. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  156. }
  157. if( nValue < 0.0 )
  158. {
  159. pBaseVar->m_dbData = 0.0;
  160. }
  161. else
  162. {
  163. pBaseVar->m_dbData = nValue *
  164. fCoef *
  165. fScale;
  166. }
  167. }
  168. else
  169. {
  170. if( nValue < 0.0 )
  171. {
  172. pBaseVar->m_dbData = 0.0;
  173. }
  174. else
  175. {
  176. pBaseVar->m_dbData = (double)(nValue * fCoef);
  177. }
  178. }
  179. if( pBaseVar->m_nRearm != 0 )
  180. {
  181. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  182. }
  183. }
  184. if( 10006 == iSpecialFlag )
  185. {
  186. short int wdValue = 0;
  187. short int whValue = 0, wlValue = 0;
  188. memcpy(&wdValue, chMsg, sizeof(short int));
  189. whValue = ntohs(wdValue);
  190. memcpy(&wdValue, chMsg+2, sizeof(short int));
  191. wlValue = ntohs(wdValue);
  192. //pBaseVar->m_dbData = (double)( (wlValue * 10000 + whValue) * fCoef);
  193. //if( pBaseVar->m_nRearm != 0 )
  194. //{
  195. // pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  196. //}
  197. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  198. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  199. {
  200. double fScale = 1.0;
  201. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  202. {
  203. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  204. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  205. }
  206. if( nValue < 0.0 )
  207. {
  208. pBaseVar->m_dbData = 0.0;
  209. }
  210. else
  211. {
  212. pBaseVar->m_dbData = (wlValue * 10000 + whValue) *
  213. fCoef *
  214. fScale;
  215. }
  216. }
  217. else
  218. {
  219. if( nValue < 0.0 )
  220. {
  221. pBaseVar->m_dbData = 0.0;
  222. }
  223. else
  224. {
  225. pBaseVar->m_dbData = (double)((wlValue * 10000 + whValue) * fCoef);
  226. }
  227. }
  228. if( pBaseVar->m_nRearm != 0 )
  229. {
  230. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  231. }
  232. }
  233. EnterCriticalSection(&g_csRtuReadOneData);
  234. pDev->m_dwOnlineTick = GetTickCount();
  235. LeaveCriticalSection(&g_csRtuReadOneData);
  236. return 0;
  237. }
  238. int RtuSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
  239. {
  240. int nDeviceIndex = -1, nVarIndex = -1;
  241. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  242. if( bFind == FALSE )
  243. {
  244. return -1;
  245. }
  246. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  247. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  248. EnterCriticalSection(&g_csRtuReadOneData);
  249. pDev->m_dwOnlineTick = GetTickCount();
  250. LeaveCriticalSection(&g_csRtuReadOneData);
  251. int nVarItemID = pBaseVar->m_nVarItemID;
  252. int nDataLen = pBaseVar->m_iDataLen;
  253. int nValue;
  254. DWORD wdValue;
  255. double fCoef = pBaseVar->m_dbCoefficient;
  256. if( nVarItemID > 0 )//BIT位变量
  257. {
  258. int nStartBit = pBaseVar->m_nStartBit;
  259. int nEndBit = pBaseVar->m_nEndBit;
  260. int nStartReg, nEndReg;
  261. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Modbus Ascii码暂时没有碰到,没有经过调试,待测试
  262. {
  263. if( nDataLen == 1 )
  264. {
  265. char chBuffer[8] = {0};
  266. memset(chBuffer, 0, sizeof(chBuffer));
  267. BYTE byValue = chMsg[0];
  268. DigitToBinary(byValue, chBuffer, 8) ;
  269. CString strTemp;
  270. switch( nEndBit - nStartBit )
  271. {
  272. case 0:
  273. strTemp.Format("%c", chBuffer[nStartBit]);
  274. nValue = atoi(strTemp);
  275. break;
  276. case 1:
  277. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  278. nValue = AsciiToBYTE(strTemp[0]) * 2 + AsciiToBYTE(strTemp[1]);
  279. break;
  280. case 2:
  281. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  282. nValue = AsciiToBYTE(strTemp[0]) * 4 + AsciiToBYTE(strTemp[1]) * 2 + AsciiToBYTE(strTemp[2]);
  283. break;
  284. case 3:
  285. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  286. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  287. nValue = AsciiToBYTE(strTemp[0]) * 8 + AsciiToBYTE(strTemp[1]) * 4 + AsciiToBYTE(strTemp[2]) * 2 + AsciiToBYTE(strTemp[0]);
  288. break;
  289. case 4:
  290. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  291. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  292. break;
  293. case 5:
  294. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  295. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  296. break;
  297. case 6:
  298. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  299. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  300. break;
  301. }
  302. //nValue = atoi(strTemp);
  303. //strTemp.Format("%d",
  304. }
  305. else if( nDataLen == 2 )
  306. {
  307. char chBuffer[16] = {0};
  308. //chMsg[0] = 0x00;
  309. //chMsg[2] = 0x00;
  310. //chMsg[2] = 0x0E;
  311. //chMsg[3] = 0x0F;
  312. //WORD wValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  313. // ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  314. WORD wValue;
  315. memcpy(&wValue, chMsg, sizeof(WORD));
  316. wValue = ntohs(wValue);
  317. DigitToBinary(wValue, chBuffer, 16) ;
  318. CString strTemp;
  319. switch( nEndBit - nStartBit )
  320. {
  321. case 0:
  322. strTemp.Format("%c", chBuffer[nStartBit]);
  323. nValue = atoi(strTemp);
  324. break;
  325. case 1:
  326. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  327. nValue = AsciiToBYTE(strTemp[0]) * 2 + AsciiToBYTE(strTemp[1]);
  328. break;
  329. case 2:
  330. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  331. nValue = AsciiToBYTE(strTemp[0]) * 4 + AsciiToBYTE(strTemp[1]) * 2 + AsciiToBYTE(strTemp[2]);
  332. break;
  333. case 3:
  334. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  335. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  336. nValue = AsciiToBYTE(strTemp[0]) * 8 + AsciiToBYTE(strTemp[1]) * 4 + AsciiToBYTE(strTemp[2]) * 2 + AsciiToBYTE(strTemp[0]);
  337. break;
  338. case 4:
  339. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  340. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  341. break;
  342. case 5:
  343. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  344. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  345. break;
  346. case 6:
  347. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  348. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  349. break;
  350. }
  351. }
  352. }
  353. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  354. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  355. {
  356. nStartReg = (int)nStartBit / 8;
  357. nEndReg = (int)(nEndBit + 1) / 8;
  358. nValue = chMsg[nStartReg];
  359. }
  360. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  361. {
  362. nStartReg = (int)nStartBit / 8;
  363. nEndReg = (int)(nEndBit + 1) / 8;
  364. memcpy(&wdValue, chMsg, sizeof(WORD));
  365. nValue = ntohs((WORD)wdValue);
  366. }
  367. EnterCriticalSection( &g_csRtuReadOneData );
  368. pBaseVar->m_dbData = (double)(nValue * fCoef);
  369. if( pBaseVar->m_nRearm != 0 )
  370. {
  371. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  372. }
  373. LeaveCriticalSection( &g_csRtuReadOneData );
  374. //LOG4C((LOG_NOTICE, "desc = %s, value = %d", pBaseVar->m_strDesc, (int)pBaseVar->m_dbData));
  375. }
  376. else
  377. {
  378. // 联合类型变量做特殊处理
  379. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  380. {
  381. if( nDataLen == 2 )
  382. {
  383. //int类型
  384. union __UNION_VAR_INT{
  385. char ch[2];
  386. int value;
  387. }unionVarInt;
  388. unionVarInt.ch[0] = chMsg[1];
  389. unionVarInt.ch[1] = chMsg[0];
  390. EnterCriticalSection( &g_csRtuReadOneData );
  391. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  392. if( pBaseVar->m_nRearm != 0 )
  393. {
  394. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  395. }
  396. LeaveCriticalSection( &g_csRtuReadOneData );
  397. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  398. }
  399. else if( nDataLen == 4 )
  400. {
  401. //float类型
  402. union __UNION_VAR_FLOAT{
  403. char ch[4];
  404. float value;
  405. }unionVarFloat;
  406. unionVarFloat.ch[0] = chMsg[3];
  407. unionVarFloat.ch[1] = chMsg[2];
  408. unionVarFloat.ch[2] = chMsg[1];
  409. unionVarFloat.ch[3] = chMsg[0];
  410. EnterCriticalSection( &g_csRtuReadOneData );
  411. if( unionVarFloat.value < 0.0 )
  412. {
  413. pBaseVar->m_dbData = 0.0;
  414. }
  415. else
  416. {
  417. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  418. }
  419. if( pBaseVar->m_nRearm != 0 )
  420. {
  421. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  422. }
  423. LeaveCriticalSection( &g_csRtuReadOneData );
  424. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  425. }
  426. else if( nDataLen == 8 )
  427. {
  428. //double类型
  429. union __UNION_VAR_DOUBLE{
  430. char ch[8];
  431. double value;
  432. }unionVarDouble;
  433. unionVarDouble.ch[0] = chMsg[7];
  434. unionVarDouble.ch[1] = chMsg[6];
  435. unionVarDouble.ch[2] = chMsg[5];
  436. unionVarDouble.ch[3] = chMsg[4];
  437. unionVarDouble.ch[4] = chMsg[3];
  438. unionVarDouble.ch[5] = chMsg[2];
  439. unionVarDouble.ch[6] = chMsg[1];
  440. unionVarDouble.ch[7] = chMsg[0];
  441. EnterCriticalSection( &g_csRtuReadOneData );
  442. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  443. if( pBaseVar->m_nRearm != 0 )
  444. {
  445. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  446. }
  447. LeaveCriticalSection( &g_csRtuReadOneData );
  448. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  449. }
  450. }
  451. else
  452. {
  453. switch( nDataLen )
  454. {
  455. case 1:
  456. nValue = chMsg[0];
  457. break;
  458. case 2:
  459. short int wdValue;
  460. memcpy(&wdValue, chMsg, sizeof(short int));
  461. nValue = ntohs(wdValue);
  462. break;
  463. case 3: // 一般为字符串类型,不作处理
  464. break;
  465. case 4: // 保留,暂时不知道什么处理
  466. break;
  467. case 5: // 一般为字符串类型,不作处理
  468. break;
  469. case 6: // 一般为字符串类型,不作处理
  470. break;
  471. case 7: // 一般为字符串类型,不作处理
  472. break;
  473. case 8: // 保留,暂时没有用到
  474. break;
  475. }
  476. EnterCriticalSection( &g_csRtuReadOneData );
  477. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  478. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  479. {
  480. double fScale = 1.0;
  481. if( pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues > 0 )
  482. {
  483. fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  484. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  485. }
  486. if( nValue < 0.0 )
  487. {
  488. pBaseVar->m_dbData = 0.0;
  489. }
  490. else
  491. {
  492. pBaseVar->m_dbData = nValue *
  493. fCoef *
  494. fScale;
  495. }
  496. }
  497. else
  498. {
  499. if( nValue < 0.0 )
  500. {
  501. pBaseVar->m_dbData = 0.0;
  502. }
  503. else
  504. {
  505. pBaseVar->m_dbData = (double)(nValue * fCoef);
  506. }
  507. }
  508. if( pBaseVar->m_nRearm != 0 )
  509. {
  510. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  511. }
  512. //LOG4C((LOG_NOTICE, "desc = %s, value = %d", pBaseVar->m_strDesc, (int)pBaseVar->m_dbData));
  513. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  514. LeaveCriticalSection( &g_csRtuReadOneData );
  515. }
  516. }
  517. EnterCriticalSection(&g_csRtuReadOneData);
  518. pDev->m_dwOnlineTick = GetTickCount();
  519. LeaveCriticalSection(&g_csRtuReadOneData);
  520. return 0;
  521. }
  522. void UnInitRtuDll()
  523. {
  524. if (g_hRTULibModule != NULL && ( NULL != pRTU_DLLUnInit ))
  525. {
  526. pRTU_DLLUnInit();
  527. }
  528. DeleteCriticalSection( &g_csRtuReadOneData );
  529. if (g_hRTULibModule!= NULL)
  530. {
  531. AfxFreeLibrary(g_hRTULibModule);
  532. g_hRTULibModule = NULL;
  533. }
  534. }
  535. int RtuRequestWrData(char chDevUid[20], int iVarID, double data)//用于请求写命令操作
  536. {
  537. SETBASEPARAM SetBasePara;
  538. memset( &SetBasePara, 0, sizeof(SETBASEPARAM) );
  539. int nDeviceIndex = -1, nVarIndex = -1;
  540. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  541. if( bFind == FALSE ) return -1;
  542. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  543. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  544. SetBasePara.nAddr = pDev->m_iDevideaddr;
  545. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  546. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  547. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  548. if (pRTU_DLLWrite(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, data, pBaseVar->m_iDataLen) == 0)
  549. return 0;
  550. else
  551. return -1;
  552. }
  553. double RtuSingleResponseWriteData(char chDevUid[20], int iVarID)
  554. {
  555. return 0;
  556. }