tcpprocess.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "stdafx.h"
  2. #include "tcpprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. #include "Syslib.h"
  7. HANDLE g_TCPResponseThread = NULL;
  8. HANDLE g_TCPRequestThread = NULL;
  9. DWORD g_dwTcpVariantTick = 0;
  10. CRITICAL_SECTION g_csTcpReadOneData;
  11. HANDLE g_hSemNet;
  12. // 临时测试变量
  13. int g_nIndex = 0;
  14. //rtu动态库输出函数
  15. HINSTANCE g_hTCPLibModule = NULL;
  16. TCP_DLLInitNet pTCP_DLLInitNet = NULL;
  17. TCP_DLLInit pTCP_DLLInit = NULL;
  18. TCP_DLLUnInit pTCP_DLLUnInit = NULL;
  19. TCP_DLLRequestStatusData pTCP_DLLRequestStatusData = NULL;
  20. TCP_DLLWrite pTCP_DLLWrite = NULL;
  21. TCP_DLLRequestWrStatusData pTCP_DLLRequestWrStatusData = NULL;
  22. TCP_DLLCloseNet pTCP_DllCloseNet = NULL;
  23. int TcpCommandSend();
  24. int TcpResponseValue();
  25. BOOL LoadTcpDll(CString strpath)
  26. {
  27. char strFile[256] = {0};
  28. g_hTCPLibModule = NULL;
  29. sprintf(strFile, "%s\\dll\\modbustcp.dll", strpath);
  30. InitializeCriticalSection( &g_csTcpReadOneData );
  31. MTVERIFY( g_hSemNet = CreateEvent( NULL, TRUE, TRUE, "Server::ModbusTcpNet" ) );
  32. g_hTCPLibModule = AfxLoadLibrary(strFile);
  33. //RTU 动态库初始化
  34. if (NULL != g_hTCPLibModule)
  35. {
  36. pTCP_DLLInitNet =( TCP_DLLInitNet)::GetProcAddress(g_hTCPLibModule, "TCP_DLLInitNet");
  37. pTCP_DllCloseNet =( TCP_DLLCloseNet)::GetProcAddress(g_hTCPLibModule, "TCP_DLLCloseNet");
  38. pTCP_DLLInit = (TCP_DLLInit)::GetProcAddress(g_hTCPLibModule, "TCP_DLLInit");
  39. pTCP_DLLUnInit = (TCP_DLLUnInit)::GetProcAddress(g_hTCPLibModule, "TCP_DLLUnInit");
  40. pTCP_DLLRequestStatusData = (TCP_DLLRequestStatusData)::GetProcAddress(g_hTCPLibModule, "TCP_DLLRequestStatusData");
  41. pTCP_DLLWrite = (TCP_DLLWrite)::GetProcAddress(g_hTCPLibModule, "TCP_DLLWrite");
  42. pTCP_DLLRequestWrStatusData = (TCP_DLLRequestWrStatusData)::GetProcAddress(g_hTCPLibModule, "TCP_DLLRequestWrStatusData");
  43. return TRUE;
  44. }
  45. else
  46. {
  47. return FALSE;
  48. }
  49. }
  50. BOOL InitTcpNet(int nAddr, int nNetPort, char strIpAddr[32])
  51. {
  52. if (pTCP_DLLInitNet(nAddr, nNetPort, strIpAddr))//初始化串口
  53. return TRUE;
  54. else
  55. return FALSE;
  56. }
  57. void CloseTcpNet(int nAddr, int nNetPort, char strIpAddr[32])
  58. {
  59. if( pTCP_DllCloseNet )
  60. {
  61. pTCP_DllCloseNet(nAddr, nNetPort, strIpAddr);
  62. }
  63. }
  64. int TcpRequestData(
  65. int nNetPort,
  66. int nDevAddr,
  67. char chIpAddr[32],
  68. int nRegNum,
  69. int nRegStartAddr,
  70. int nFuncCode)
  71. {
  72. int nRet = 0;
  73. #if 0
  74. if( WaitForSingleObject(g_hSemNet, 0) == WAIT_OBJECT_0 ) // 有信号才发送数据
  75. {
  76. g_nIndex++;
  77. ResetEvent( g_hSemNet );
  78. //TRACE("开始请求一次数据\r\n");
  79. nRet = pTCP_DLLInit(nNetPort,
  80. nDevAddr,
  81. chIpAddr,
  82. nRegNum,
  83. nRegStartAddr,
  84. 0,
  85. 0,
  86. nFuncCode);
  87. return nRet;
  88. }
  89. else
  90. {
  91. return ERR_CODE_MODBUS_TCP_NET_BUSY;
  92. }
  93. #else
  94. nRet = pTCP_DLLInit(nNetPort,
  95. nDevAddr,
  96. chIpAddr,
  97. nRegNum,
  98. nRegStartAddr,
  99. 0,
  100. 0,
  101. nFuncCode);
  102. return nRet;
  103. #endif
  104. }
  105. int TcpSingleResponseData(char chDevUid[20], int iVarID)
  106. {
  107. int nDeviceIndex = -1, nVarIndex = -1;
  108. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  109. if( bFind == FALSE ) return -1;
  110. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  111. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  112. EnterCriticalSection(&g_csTcpReadOneData);
  113. pDev->m_dwOnlineTick = GetTickCount();
  114. LeaveCriticalSection(&g_csTcpReadOneData);
  115. char chMsg[80] = {0};
  116. int nRet = pTCP_DLLRequestStatusData(pDev->m_iIpport,
  117. pDev->m_iDevideaddr,
  118. (char *)(LPCTSTR)pDev->m_strIp,
  119. chMsg);
  120. if( nRet != 0 )
  121. {
  122. return nRet;
  123. }
  124. DWORD nValue = 0;
  125. WORD wdValue = 0;
  126. double fCoef = pBaseVar->m_dbCoefficient;
  127. int nDataLen = pBaseVar->m_iDataLen;
  128. int nVarItemID = pBaseVar->m_nVarItemID;
  129. if( nVarItemID > 0 )//BIT位变量
  130. {
  131. int nStartBit = pBaseVar->m_nStartBit;
  132. int nEndBit = pBaseVar->m_nEndBit;
  133. int nStartReg, nEndReg;
  134. if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
  135. {
  136. if( nDataLen == 1 )
  137. {
  138. nValue = chMsg[0];
  139. }
  140. else if( nDataLen == 2 )
  141. {
  142. memcpy(&wdValue, chMsg, sizeof(WORD));
  143. nValue = (DWORD)htons(wdValue);
  144. }
  145. }
  146. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  147. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  148. {
  149. nStartReg = (int)nStartBit / 8;
  150. nEndReg = (int)(nEndBit + 1) / 8;
  151. nValue = chMsg[nStartReg];
  152. }
  153. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  154. {
  155. nStartReg = (int)nStartBit / 8;
  156. nEndReg = (int)(nEndBit + 1) / 8;
  157. memcpy(&wdValue, chMsg, sizeof(WORD));
  158. nValue = (DWORD)htons(wdValue);
  159. }
  160. EnterCriticalSection( &g_csTcpReadOneData );
  161. pBaseVar->m_dbData = (double)(nValue * fCoef);
  162. if( pBaseVar->m_nRearm != 0 )
  163. {
  164. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  165. }
  166. LeaveCriticalSection( &g_csTcpReadOneData );
  167. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  168. }
  169. else
  170. {
  171. // 联合类型变量做特殊处理
  172. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  173. {
  174. if( nDataLen == 2 )
  175. {
  176. //int类型
  177. union __UNION_VAR_INT{
  178. char ch[2];
  179. int value;
  180. }unionVarInt;
  181. unionVarInt.ch[0] = chMsg[1];
  182. unionVarInt.ch[1] = chMsg[0];
  183. EnterCriticalSection( &g_csTcpReadOneData );
  184. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  185. if( pBaseVar->m_nRearm != 0 )
  186. {
  187. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  188. }
  189. LeaveCriticalSection( &g_csTcpReadOneData );
  190. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  191. }
  192. else if( nDataLen == 4 )
  193. {
  194. //float类型
  195. union __UNION_VAR_FLOAT{
  196. char ch[4];
  197. float value;
  198. }unionVarFloat;
  199. unionVarFloat.ch[0] = chMsg[3];
  200. unionVarFloat.ch[1] = chMsg[2];
  201. unionVarFloat.ch[2] = chMsg[1];
  202. unionVarFloat.ch[3] = chMsg[0];
  203. EnterCriticalSection( &g_csTcpReadOneData );
  204. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  205. if( pBaseVar->m_nRearm != 0 )
  206. {
  207. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  208. }
  209. LeaveCriticalSection( &g_csTcpReadOneData );
  210. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  211. }
  212. else if( nDataLen == 8 )
  213. {
  214. //double类型
  215. union __UNION_VAR_DOUBLE{
  216. char ch[8];
  217. double value;
  218. }unionVarDouble;
  219. unionVarDouble.ch[0] = chMsg[7];
  220. unionVarDouble.ch[1] = chMsg[6];
  221. unionVarDouble.ch[2] = chMsg[5];
  222. unionVarDouble.ch[3] = chMsg[4];
  223. unionVarDouble.ch[4] = chMsg[3];
  224. unionVarDouble.ch[5] = chMsg[2];
  225. unionVarDouble.ch[6] = chMsg[1];
  226. unionVarDouble.ch[7] = chMsg[0];
  227. EnterCriticalSection( &g_csTcpReadOneData );
  228. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  229. if( pBaseVar->m_nRearm != 0 )
  230. {
  231. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  232. }
  233. LeaveCriticalSection( &g_csTcpReadOneData );
  234. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  235. }
  236. }
  237. else
  238. {
  239. switch( nDataLen )
  240. {
  241. case 1:
  242. nValue = chMsg[0];
  243. break;
  244. case 2:
  245. WORD wdValue;
  246. memcpy(&wdValue, chMsg, sizeof(WORD));
  247. //wdValue = htons(wdValue);
  248. nValue = htons(wdValue);
  249. break;
  250. case 3: // 一般为字符串类型,不作处理
  251. break;
  252. case 4: // 保留,暂时不知道什么处理
  253. break;
  254. case 5: // 一般为字符串类型,不作处理
  255. break;
  256. case 6: // 一般为字符串类型,不作处理
  257. break;
  258. case 7: // 一般为字符串类型,不作处理
  259. break;
  260. case 8: // 保留,暂时没有用到
  261. break;
  262. }
  263. EnterCriticalSection( &g_csTcpReadOneData );
  264. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  265. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  266. {
  267. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  268. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  269. CString strUid = pDev->m_strUid;
  270. if( strUid.Find("5.4.63") != -1 ) // 暂时还没有好的方案处理C2000设备计算公式,只好做特殊处理,待完善
  271. {
  272. // (读数/4096*20-最小电流值)*((最大比例-最小比例)/(最大电流值-最小电流值)) +最小比例
  273. if( nValue > 0 )
  274. {
  275. pBaseVar->m_dbData = ((double)nValue / 4096 * 20 - pBaseVar->m_nMinValues) * fScale + pBaseVar->m_nMinConvtRate;
  276. }
  277. else
  278. {
  279. pBaseVar->m_dbData = 0.0;
  280. }
  281. }
  282. else
  283. {
  284. pBaseVar->m_dbData = nValue *
  285. fCoef *
  286. fScale;
  287. }
  288. }
  289. else
  290. {
  291. pBaseVar->m_dbData = (double)(nValue * fCoef);
  292. }
  293. if( pBaseVar->m_nRearm != 0 )
  294. {
  295. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  296. }
  297. LeaveCriticalSection( &g_csTcpReadOneData );
  298. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_TcpReadOneData.dbData);
  299. }
  300. }
  301. EnterCriticalSection(&g_csTcpReadOneData);
  302. pDev->m_dwOnlineTick = GetTickCount();
  303. LeaveCriticalSection(&g_csTcpReadOneData);
  304. return 0;
  305. }
  306. int TcpRequestWriteData(
  307. int nNetPort,
  308. int nDevAddr,
  309. char chIpAddr[32],
  310. int nRegNum,
  311. int nRegStartAddr,
  312. int nFuncCode,
  313. int nRegValue)
  314. {
  315. int nRet = 0;
  316. g_nIndex++;
  317. //ResetEvent( g_hSemNet );
  318. //TRACE("开始请求一次数据\r\n");
  319. nRet = pTCP_DLLWrite(nNetPort,
  320. nDevAddr,
  321. chIpAddr,
  322. nRegNum,
  323. nRegStartAddr,
  324. 0,
  325. 0,
  326. nFuncCode,
  327. nRegValue);
  328. return nRet;
  329. }
  330. int TcpSingleResponseWriteData(char chDevUid[20], int iVarID)
  331. {
  332. int nDeviceIndex = -1, nVarIndex = -1;
  333. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  334. if( bFind == FALSE ) return -1;
  335. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  336. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  337. int nWriteResult;
  338. int nRet = pTCP_DLLRequestWrStatusData(pDev->m_iIpport,
  339. pDev->m_iDevideaddr,
  340. (char *)(LPCTSTR)pDev->m_strIp,
  341. nWriteResult);
  342. if( nRet != 0 )
  343. {
  344. return nRet;
  345. }
  346. return 0;
  347. }
  348. void UnInitTcpDll()
  349. {
  350. if (g_hTCPLibModule != NULL && ( NULL != pTCP_DLLUnInit ))
  351. {
  352. if( g_hSemNet )
  353. {
  354. MTVERIFY( SetEvent(g_hSemNet) );
  355. }
  356. pTCP_DLLUnInit();
  357. }
  358. if( g_hSemNet )
  359. {
  360. MTVERIFY( CloseHandle( g_hSemNet ));
  361. }
  362. DeleteCriticalSection( &g_csTcpReadOneData );
  363. if (g_hTCPLibModule!= NULL)
  364. {
  365. AfxFreeLibrary(g_hTCPLibModule);
  366. g_hTCPLibModule = NULL;
  367. }
  368. }