modbustcp.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // MGE.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "Head.h"
  5. #include "winsock2.h"
  6. #include <stdlib.h>
  7. #include ".\modbustcp.h"
  8. #include<vector>
  9. #pragma comment(lib, "ws2_32.lib ")
  10. vector<__NET_STRUCT> g_vtNetStruct;
  11. vector<RESPONSE_STRUCT>g_Response;
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #endif
  15. BOOL InitNet( int nAddr, int nNetPort, char strIpAddr[32]);
  16. int NetDataCollect(int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
  17. WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
  18. int nFuncCode);
  19. int GetNetIndex(int nNetPort, int nAddr, char strIpAddr[32]);
  20. BEGIN_MESSAGE_MAP(CModbustcpApp, CWinApp)
  21. END_MESSAGE_MAP()
  22. CModbustcpApp::CModbustcpApp()
  23. {
  24. // TODO: 在此处添加构造代码,
  25. // 将所有重要的初始化放置在 InitInstance 中
  26. }
  27. // 唯一的一个 CMGEApp 对象
  28. CModbustcpApp theApp;
  29. // CMGEApp 初始化
  30. BOOL CModbustcpApp::InitInstance()
  31. {
  32. CWinApp::InitInstance();
  33. return TRUE;
  34. }
  35. int CModbustcpApp::ExitInstance()
  36. {
  37. // TODO: 在此添加专用代码和/或调用基类
  38. return CWinApp::ExitInstance();
  39. }
  40. int GetNetIndex( int nNetPort, int nAddr, char strIpAddr[32])
  41. {
  42. for( int i = 0; i < (int)g_vtNetStruct.size(); i++ )
  43. {
  44. if( (g_vtNetStruct[i].nNetPort == nNetPort) && (strcmp(g_vtNetStruct[i].strIpAddr, strIpAddr) == 0))// && g_vtComStruct[i].nAddr == nAddr)
  45. {
  46. return i;
  47. }
  48. }
  49. return -1;
  50. }
  51. int TCP_DLLInit( int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
  52. WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
  53. int nFuncCode)
  54. {
  55. //TRACE("开始请求寄存器起始地址=%d\r\n", wStartAddr);
  56. return NetDataCollect(nNetPort, nAddr, strIpAddr, wRegNum, wStartAddr,
  57. wTranProcessID,wProtocolID,nFuncCode);
  58. }
  59. int TCP_DLLWrite( int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
  60. WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
  61. int nFuncCode, int nRegValue)
  62. {
  63. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
  64. if( nNetIndex == -1 )
  65. {
  66. TRACE("打开端口错误\r\n");
  67. return ERR_CODE_MODBUS_TCP_NET_FAULT;
  68. }
  69. CClientSocket *pClientSkt = NULL;
  70. pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
  71. //TRACE("Init m_structResponse\r\n");
  72. memset(&pClientSkt->m_structWriteResponse, 0, sizeof(pClientSkt->m_structWriteResponse));
  73. //pClientSkt->m_structResponse.nReadResult = 0;
  74. REQUESTWRPARAM tagRequestWrite;
  75. memset( &tagRequestWrite, 0, sizeof(REQUESTWRPARAM) );
  76. WORD wByteNum = sizeof(tagRequestWrite.Header.EquipAddr) + //设备地址
  77. sizeof(tagRequestWrite.Header.FuncCode) + //功能码
  78. sizeof(tagRequestWrite.StartAddr) + //起始地址
  79. sizeof(tagRequestWrite.RegNum) + //寄存器个数
  80. sizeof(tagRequestWrite.DataLen) + //数据长度
  81. sizeof(tagRequestWrite.RegValue); //数据
  82. tagRequestWrite.Header.TranProcessID = htons(wTranProcessID);
  83. tagRequestWrite.Header.ProtocolID = htons(wProtocolID);
  84. tagRequestWrite.Header.FuncCode = nFuncCode;
  85. tagRequestWrite.Header.EquipAddr = (BYTE)nAddr;
  86. tagRequestWrite.Header.ByteNum = htons(wByteNum);
  87. tagRequestWrite.RegNum = htons(wRegNum);
  88. tagRequestWrite.DataLen = wRegNum * 2;
  89. tagRequestWrite.StartAddr = htons(wStartAddr);
  90. tagRequestWrite.RegValue = htons(nRegValue);
  91. return pClientSkt->Write(tagRequestWrite);
  92. }
  93. void TCP_DLLUnInit()
  94. {
  95. CClientSocket *pClientSocket = NULL;
  96. // 关闭已经打开的串口
  97. for( unsigned int i = 0; i < g_vtNetStruct.size(); i++ )
  98. {
  99. pClientSocket = g_vtNetStruct[i].pClientskt;
  100. if( pClientSocket )
  101. {
  102. if( pClientSocket->m_hRunObject != NULL )
  103. {
  104. SetEvent( pClientSocket->m_hRunObject );
  105. }
  106. if( pClientSocket->m_hReConnectSrvThread != NULL )
  107. {
  108. MTVERIFY( WaitForSingleObject( pClientSocket->m_hReConnectSrvThread, INFINITE ) != WAIT_FAILED );
  109. CloseHandle( pClientSocket->m_hReConnectSrvThread );
  110. pClientSocket->m_hReConnectSrvThread = NULL;
  111. }
  112. pClientSocket->DisConnection();
  113. delete pClientSocket;
  114. pClientSocket = NULL;
  115. }
  116. }
  117. return;
  118. }
  119. //初始化网络
  120. BOOL TCP_DLLInitNet( int nAddr, int nNetPort, char strIpAddr[32])
  121. {
  122. if (InitNet( nAddr, nNetPort, strIpAddr))
  123. {
  124. CClientSocket *pClientSkt = NULL;
  125. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr );
  126. pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
  127. MTVERIFY( pClientSkt->m_hReConnectSrvThread = ::CreateThread(NULL, 0, pClientSkt->ReConnectSrvThread, pClientSkt, 0, 0) );
  128. return TRUE;
  129. }
  130. else
  131. {
  132. return FALSE;
  133. }
  134. }
  135. //关闭网络
  136. void TCP_DLLCloseNet( int nAddr, int nNetPort, char strIpAddr[32])
  137. {
  138. // 关闭已经打开的串口
  139. for( unsigned int i = 0; i < g_vtNetStruct.size(); i++ )
  140. {
  141. if( (g_vtNetStruct[i].nNetPort == nNetPort) && (strcmp(g_vtNetStruct[i].strIpAddr, strIpAddr) == 0))
  142. {
  143. if( g_vtNetStruct[i].pClientskt )
  144. {
  145. if( g_vtNetStruct[i].pClientskt->m_hRunObject != NULL )
  146. {
  147. SetEvent( g_vtNetStruct[i].pClientskt->m_hRunObject );
  148. }
  149. if( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread != NULL )
  150. {
  151. MTVERIFY( WaitForSingleObject( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread, INFINITE ) != WAIT_FAILED );
  152. CloseHandle( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread );
  153. g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread = NULL;
  154. }
  155. g_vtNetStruct[i].pClientskt->DisConnection();
  156. if( g_vtNetStruct[i].pClientskt )
  157. {
  158. delete g_vtNetStruct[i].pClientskt;
  159. g_vtNetStruct[i].pClientskt = NULL;
  160. }
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. BOOL InitNet( int nAddr, int nNetPort, char strIpAddr[32])
  167. {
  168. CClientSocket *pClientSkt = NULL;
  169. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr );
  170. pClientSkt = new CClientSocket;
  171. char chNetPort[8] = {0};
  172. itoa(nNetPort, chNetPort, 10);
  173. NET_STRUCT tagNetStruct = {0};
  174. if( nNetIndex == -1 )
  175. {
  176. if(!pClientSkt->Connection(strIpAddr, chNetPort))
  177. {
  178. delete pClientSkt;
  179. pClientSkt = NULL;
  180. //pClientSkt->m_bSocket = FALSE;
  181. return FALSE;
  182. }
  183. else
  184. {
  185. pClientSkt->m_bSocket = TRUE;
  186. strcpy(pClientSkt->m_chIpAddr, strIpAddr);
  187. pClientSkt->m_nNetPort = nNetPort;
  188. tagNetStruct.nAddr = nAddr;
  189. tagNetStruct.nNetPort = nNetPort;
  190. tagNetStruct.pClientskt = pClientSkt;
  191. memcpy(tagNetStruct.strIpAddr, strIpAddr, sizeof(tagNetStruct.strIpAddr));
  192. // 网络端口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  193. g_vtNetStruct.push_back( tagNetStruct );
  194. }
  195. }
  196. else
  197. {
  198. if( g_vtNetStruct[nNetIndex].pClientskt == NULL )
  199. {
  200. if(!pClientSkt->Connection(strIpAddr, chNetPort))
  201. {
  202. delete pClientSkt;
  203. pClientSkt = NULL;
  204. //pClientSkt->m_bSocket = FALSE;
  205. return FALSE;
  206. }
  207. else
  208. {
  209. pClientSkt->m_bSocket = TRUE;
  210. strcpy(pClientSkt->m_chIpAddr, strIpAddr);
  211. pClientSkt->m_nNetPort = nNetPort;
  212. g_vtNetStruct[nNetIndex].pClientskt = pClientSkt;
  213. ResetEvent( g_vtNetStruct[nNetIndex].pClientskt->m_hRunObject );
  214. }
  215. }
  216. else
  217. {
  218. if( g_vtNetStruct[nNetIndex].pClientskt->m_bSocket == TRUE )
  219. {
  220. return TRUE;
  221. }
  222. else
  223. {
  224. return FALSE;
  225. }
  226. }
  227. }
  228. return TRUE;
  229. }
  230. int TCP_DLLRequestStatusData(int nNetPort, int nAddr, char strIpAddr[32], char *pData)
  231. {
  232. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
  233. if( nNetIndex == -1 )
  234. {
  235. TRACE("打开端口失败!\r\n");
  236. return ERR_CODE_MODBUS_TCP_NET_FAULT;
  237. }
  238. CClientSocket *pNet = NULL;
  239. pNet = g_vtNetStruct[nNetIndex].pClientskt;
  240. int nByteNum = pNet->m_structResponse.DataLen;//pNet->m_structResponse.StatusStruct.RtnByteNum[0];
  241. do
  242. {
  243. if( nByteNum == 0 ) // 一直等到收到数据
  244. {
  245. nByteNum = pNet->m_structResponse.DataLen;
  246. Sleep(10);
  247. }
  248. else
  249. {
  250. break;
  251. }
  252. }while( WaitForSingleObject( pNet->m_hRunObject, 125L ) == WAIT_TIMEOUT );
  253. //TRACE("====DataLen = %d\r\n", pNet->m_structResponse.DataLen);
  254. if( nByteNum == 0 )//设备没有请求到数据
  255. {
  256. //TRACE("设备没有请求到数据\r\n");
  257. return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
  258. }
  259. if( pNet->m_structResponse.nReadResult == 0 ) // 数据没有读成功
  260. {
  261. return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
  262. }
  263. int i = 0;
  264. while( i < nByteNum )
  265. {
  266. pData[i] = pNet->m_structResponse.StrRtnMsg[i];
  267. i++;
  268. }
  269. #if 0
  270. int nValue;
  271. WORD wdValue;
  272. memcpy(&wdValue, pNet->m_structResponse.StrRtnMsg, sizeof(WORD));
  273. //wdValue = htons(wdValue);
  274. nValue = htons(wdValue);
  275. TRACE(">>>>>>>>>结束请求,数据内容=%d\r\n", nValue);
  276. #endif
  277. return 0;
  278. }
  279. int TCP_DLLRequestWrStatusData(int nNetPort, int nAddr, char strIpAddr[32], int &nWriteResult)
  280. {
  281. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
  282. if( nNetIndex == -1 )
  283. {
  284. TRACE("打开端口失败!\r\n");
  285. nWriteResult = 0;
  286. return ERR_CODE_MODBUS_TCP_NET_FAULT;
  287. }
  288. CClientSocket *pNet = NULL;
  289. pNet = g_vtNetStruct[nNetIndex].pClientskt;
  290. int nRegNum = pNet->m_structWriteResponse.RegNum;//pNet->m_structResponse.StatusStruct.RtnByteNum[0];
  291. do
  292. {
  293. if( nRegNum == 0 ) // 一直等到收到数据
  294. {
  295. nRegNum = pNet->m_structWriteResponse.RegNum;
  296. Sleep(10);
  297. }
  298. else
  299. {
  300. break;
  301. }
  302. }while( WaitForSingleObject( pNet->m_hRunObject, 125L ) == WAIT_TIMEOUT );
  303. //TRACE("====DataLen = %d\r\n", pNet->m_structResponse.DataLen);
  304. if( nRegNum == 0 )//设备没有请求到数据
  305. {
  306. //TRACE("设备没有请求到数据\r\n");
  307. nWriteResult = 0;
  308. return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
  309. }
  310. nWriteResult = 1;
  311. return 0;
  312. }
  313. int NetDataCollect(int nNetPort, int nAddr, char strIpAddr[32],
  314. WORD wRegNum, WORD wStartAddr, WORD wTranProcessID,
  315. WORD wProtocolID, int nFuncCode)
  316. {
  317. REQUESTPARAM SetBasePara = {0};
  318. int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
  319. if( nNetIndex == -1 )
  320. {
  321. TRACE("打开端口错误\r\n");
  322. return ERR_CODE_MODBUS_TCP_NET_FAULT;
  323. }
  324. CClientSocket *pClientSkt = NULL;
  325. pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
  326. //TRACE("Init m_structResponse\r\n");
  327. memset(&pClientSkt->m_structResponse, 0, sizeof(pClientSkt->m_structResponse));
  328. //pClientSkt->m_structResponse.nReadResult = 0;
  329. WORD wByteNum = sizeof(SetBasePara.RegNum) +
  330. sizeof(SetBasePara.StartAddr) +
  331. sizeof(SetBasePara.Header.EquipAddr) +
  332. sizeof(SetBasePara.Header.FuncCode);
  333. SetBasePara.Header.TranProcessID = htons(wTranProcessID);
  334. SetBasePara.Header.ProtocolID = htons(wProtocolID);
  335. SetBasePara.Header.FuncCode = nFuncCode;
  336. SetBasePara.Header.EquipAddr = (BYTE)nAddr;
  337. SetBasePara.Header.ByteNum = htons(wByteNum);
  338. SetBasePara.RegNum = htons(wRegNum);
  339. SetBasePara.StartAddr = htons(wStartAddr);
  340. return pClientSkt->WorkMain(SetBasePara);
  341. }