rtuprocess.cpp 18 KB

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