tcpprocess.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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,0 ) );// "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. LOG4C((LOG_NOTICE, "TCP:desc = %s 读数据失败 ", pBaseVar->m_strDesc ));
  123. return nRet;
  124. }
  125. //LOG4C((LOG_NOTICE, "TCP:[ip:%s,port:%d,addr:%d]desc = %s 数据:%s (%d,%d,%d,%d)", pDev->m_strIp,pDev->m_iIpport,pDev->m_iDevideaddr,
  126. // pBaseVar->m_strDesc,chMsg ,pBaseVar->m_nVarItemID,pBaseVar->m_iDataLen,pBaseVar->m_nStartBit,pBaseVar->m_nEndBit));
  127. DWORD nValue = 0;
  128. WORD wdValue = 0;
  129. double fCoef = pBaseVar->m_dbCoefficient;
  130. int nDataLen = pBaseVar->m_iDataLen;
  131. int nVarItemID = pBaseVar->m_nVarItemID;
  132. if( nVarItemID > 0 )//BIT位变量
  133. {
  134. int nStartBit = pBaseVar->m_nStartBit;
  135. int nEndBit = pBaseVar->m_nEndBit;
  136. int nStartReg, nEndReg;
  137. if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
  138. {
  139. if( nDataLen == 1 )
  140. {
  141. nValue = chMsg[0];
  142. }
  143. else if( nDataLen == 2 )
  144. {
  145. memcpy(&wdValue, chMsg, sizeof(WORD));
  146. nValue = (DWORD)htons(wdValue);
  147. }
  148. }
  149. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  150. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  151. {
  152. nStartReg = (int)nStartBit / 8;
  153. nEndReg = (int)(nEndBit + 1) / 8;
  154. nValue = chMsg[nStartReg];
  155. }
  156. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  157. {
  158. nStartReg = (int)nStartBit / 8;
  159. nEndReg = (int)(nEndBit + 1) / 8;
  160. memcpy(&wdValue, chMsg, sizeof(WORD));
  161. nValue = (DWORD)htons(wdValue);
  162. }
  163. EnterCriticalSection( &g_csTcpReadOneData );
  164. pBaseVar->m_dbData = (double)(nValue * fCoef);
  165. if( pBaseVar->m_nRearm != 0 )
  166. {
  167. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  168. }
  169. LeaveCriticalSection( &g_csTcpReadOneData );
  170. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  171. }
  172. else
  173. {
  174. // 联合类型变量做特殊处理
  175. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  176. {
  177. if( nDataLen == 2 )
  178. {
  179. //int类型
  180. union __UNION_VAR_INT{
  181. char ch[2];
  182. int value;
  183. }unionVarInt;
  184. unionVarInt.ch[0] = chMsg[1];
  185. unionVarInt.ch[1] = chMsg[0];
  186. EnterCriticalSection( &g_csTcpReadOneData );
  187. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  188. if( pBaseVar->m_nRearm != 0 )
  189. {
  190. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  191. }
  192. LeaveCriticalSection( &g_csTcpReadOneData );
  193. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  194. }
  195. else if( nDataLen == 4 )
  196. {
  197. //float类型
  198. union __UNION_VAR_FLOAT{
  199. char ch[4];
  200. float value;
  201. }unionVarFloat;
  202. unionVarFloat.ch[0] = chMsg[3];
  203. unionVarFloat.ch[1] = chMsg[2];
  204. unionVarFloat.ch[2] = chMsg[1];
  205. unionVarFloat.ch[3] = chMsg[0];
  206. EnterCriticalSection( &g_csTcpReadOneData );
  207. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  208. if( pBaseVar->m_nRearm != 0 )
  209. {
  210. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  211. }
  212. LeaveCriticalSection( &g_csTcpReadOneData );
  213. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  214. }
  215. else if( nDataLen == 8 )
  216. {
  217. //double类型
  218. union __UNION_VAR_DOUBLE{
  219. char ch[8];
  220. double value;
  221. }unionVarDouble;
  222. unionVarDouble.ch[0] = chMsg[7];
  223. unionVarDouble.ch[1] = chMsg[6];
  224. unionVarDouble.ch[2] = chMsg[5];
  225. unionVarDouble.ch[3] = chMsg[4];
  226. unionVarDouble.ch[4] = chMsg[3];
  227. unionVarDouble.ch[5] = chMsg[2];
  228. unionVarDouble.ch[6] = chMsg[1];
  229. unionVarDouble.ch[7] = chMsg[0];
  230. EnterCriticalSection( &g_csTcpReadOneData );
  231. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  232. if( pBaseVar->m_nRearm != 0 )
  233. {
  234. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  235. }
  236. LeaveCriticalSection( &g_csTcpReadOneData );
  237. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  238. }
  239. }
  240. else
  241. {
  242. switch( nDataLen )
  243. {
  244. case 1:
  245. nValue = chMsg[0];
  246. break;
  247. case 2:
  248. WORD wdValue;
  249. memcpy(&wdValue, chMsg, sizeof(WORD));
  250. //wdValue = htons(wdValue);
  251. nValue = htons(wdValue);
  252. break;
  253. case 3: // 一般为字符串类型,不作处理
  254. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  255. break;
  256. case 4: // 保留,暂时不知道什么处理
  257. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  258. break;
  259. case 5: // 一般为字符串类型,不作处理
  260. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  261. break;
  262. case 6: // 一般为字符串类型,不作处理
  263. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  264. break;
  265. case 7: // 一般为字符串类型,不作处理
  266. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  267. break;
  268. case 8: // 保留,暂时没有用到
  269. LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
  270. break;
  271. }
  272. EnterCriticalSection( &g_csTcpReadOneData );
  273. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  274. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  275. {
  276. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  277. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  278. CString strUid = pDev->m_strUid;
  279. if( strUid.Find("5.4.63") != -1 ) // 暂时还没有好的方案处理C2000设备计算公式,只好做特殊处理,待完善
  280. {
  281. // (读数/4096*20-最小电流值)*((最大比例-最小比例)/(最大电流值-最小电流值)) +最小比例
  282. if( nValue > 0 )
  283. {
  284. pBaseVar->m_dbData = ((double)nValue / 4096 * 20 - pBaseVar->m_nMinValues) * fScale + pBaseVar->m_nMinConvtRate;
  285. }
  286. else
  287. {
  288. pBaseVar->m_dbData = 0.0;
  289. }
  290. }
  291. else
  292. {
  293. pBaseVar->m_dbData = nValue *
  294. fCoef *
  295. fScale;
  296. }
  297. }
  298. else
  299. {
  300. pBaseVar->m_dbData = (double)(nValue * fCoef);
  301. }
  302. if( pBaseVar->m_nRearm != 0 )
  303. {
  304. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  305. }
  306. LeaveCriticalSection( &g_csTcpReadOneData );
  307. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_TcpReadOneData.dbData);
  308. }
  309. }
  310. EnterCriticalSection(&g_csTcpReadOneData);
  311. pDev->m_dwOnlineTick = GetTickCount();
  312. LeaveCriticalSection(&g_csTcpReadOneData);
  313. return 0;
  314. }
  315. int TcpRequestWriteData(
  316. int nNetPort,
  317. int nDevAddr,
  318. char chIpAddr[32],
  319. int nRegNum,
  320. int nRegStartAddr,
  321. int nFuncCode,
  322. int nRegValue)
  323. {
  324. int nRet = 0;
  325. g_nIndex++;
  326. //ResetEvent( g_hSemNet );
  327. //TRACE("开始请求一次数据\r\n");
  328. nRet = pTCP_DLLWrite(nNetPort,
  329. nDevAddr,
  330. chIpAddr,
  331. nRegNum,
  332. nRegStartAddr,
  333. 0,
  334. 0,
  335. nFuncCode,
  336. nRegValue);
  337. return nRet;
  338. }
  339. int TcpSingleResponseWriteData(char chDevUid[20], int iVarID)
  340. {
  341. int nDeviceIndex = -1, nVarIndex = -1;
  342. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  343. if( bFind == FALSE ) return -1;
  344. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  345. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  346. int nWriteResult;
  347. int nRet = pTCP_DLLRequestWrStatusData(pDev->m_iIpport,
  348. pDev->m_iDevideaddr,
  349. (char *)(LPCTSTR)pDev->m_strIp,
  350. nWriteResult);
  351. if( nRet != 0 )
  352. {
  353. return nRet;
  354. }
  355. return 0;
  356. }
  357. void UnInitTcpDll()
  358. {
  359. if (g_hTCPLibModule != NULL && ( NULL != pTCP_DLLUnInit ))
  360. {
  361. if( g_hSemNet )
  362. {
  363. MTVERIFY( SetEvent(g_hSemNet) );
  364. }
  365. pTCP_DLLUnInit();
  366. }
  367. if( g_hSemNet )
  368. {
  369. MTVERIFY( CloseHandle( g_hSemNet ));
  370. }
  371. DeleteCriticalSection( &g_csTcpReadOneData );
  372. if (g_hTCPLibModule!= NULL)
  373. {
  374. AfxFreeLibrary(g_hTCPLibModule);
  375. g_hTCPLibModule = NULL;
  376. }
  377. }