123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- // MGE.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "Head.h"
- #include "winsock2.h"
- #include <stdlib.h>
- #include ".\modbustcp.h"
- #include<vector>
- #pragma comment(lib, "ws2_32.lib ")
- vector<__NET_STRUCT> g_vtNetStruct;
- vector<RESPONSE_STRUCT>g_Response;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- BOOL InitNet( int nAddr, int nNetPort, char strIpAddr[32]);
- int NetDataCollect(int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
- WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
- int nFuncCode);
- int GetNetIndex(int nNetPort, int nAddr, char strIpAddr[32]);
- BEGIN_MESSAGE_MAP(CModbustcpApp, CWinApp)
- END_MESSAGE_MAP()
- CModbustcpApp::CModbustcpApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CMGEApp 对象
- CModbustcpApp theApp;
- // CMGEApp 初始化
- BOOL CModbustcpApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- int CModbustcpApp::ExitInstance()
- {
- // TODO: 在此添加专用代码和/或调用基类
- return CWinApp::ExitInstance();
- }
- int GetNetIndex( int nNetPort, int nAddr, char strIpAddr[32])
- {
- for( int i = 0; i < (int)g_vtNetStruct.size(); i++ )
- {
- if( (g_vtNetStruct[i].nNetPort == nNetPort) && (strcmp(g_vtNetStruct[i].strIpAddr, strIpAddr) == 0))// && g_vtComStruct[i].nAddr == nAddr)
- {
- return i;
- }
- }
- return -1;
- }
- int TCP_DLLInit( int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
- WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
- int nFuncCode)
- {
- //TRACE("开始请求寄存器起始地址=%d\r\n", wStartAddr);
- return NetDataCollect(nNetPort, nAddr, strIpAddr, wRegNum, wStartAddr,
- wTranProcessID,wProtocolID,nFuncCode);
- }
- int TCP_DLLWrite( int nNetPort, int nAddr, char strIpAddr[32], WORD wRegNum,
- WORD wStartAddr, WORD wTranProcessID, WORD wProtocolID,
- int nFuncCode, int nRegValue)
- {
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
- if( nNetIndex == -1 )
- {
- TRACE("打开端口错误\r\n");
- return ERR_CODE_MODBUS_TCP_NET_FAULT;
- }
- CClientSocket *pClientSkt = NULL;
- pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
- //TRACE("Init m_structResponse\r\n");
- memset(&pClientSkt->m_structWriteResponse, 0, sizeof(pClientSkt->m_structWriteResponse));
- //pClientSkt->m_structResponse.nReadResult = 0;
-
- REQUESTWRPARAM tagRequestWrite;
- memset( &tagRequestWrite, 0, sizeof(REQUESTWRPARAM) );
- WORD wByteNum = sizeof(tagRequestWrite.Header.EquipAddr) + //设备地址
- sizeof(tagRequestWrite.Header.FuncCode) + //功能码
- sizeof(tagRequestWrite.StartAddr) + //起始地址
- sizeof(tagRequestWrite.RegNum) + //寄存器个数
- sizeof(tagRequestWrite.DataLen) + //数据长度
- sizeof(tagRequestWrite.RegValue); //数据
- tagRequestWrite.Header.TranProcessID = htons(wTranProcessID);
- tagRequestWrite.Header.ProtocolID = htons(wProtocolID);
- tagRequestWrite.Header.FuncCode = nFuncCode;
- tagRequestWrite.Header.EquipAddr = (BYTE)nAddr;
- tagRequestWrite.Header.ByteNum = htons(wByteNum);
- tagRequestWrite.RegNum = htons(wRegNum);
- tagRequestWrite.DataLen = wRegNum * 2;
- tagRequestWrite.StartAddr = htons(wStartAddr);
- tagRequestWrite.RegValue = htons(nRegValue);
- return pClientSkt->Write(tagRequestWrite);
- }
- void TCP_DLLUnInit()
- {
- CClientSocket *pClientSocket = NULL;
- // 关闭已经打开的串口
- for( unsigned int i = 0; i < g_vtNetStruct.size(); i++ )
- {
- pClientSocket = g_vtNetStruct[i].pClientskt;
- if( pClientSocket )
- {
- if( pClientSocket->m_hRunObject != NULL )
- {
- SetEvent( pClientSocket->m_hRunObject );
- }
- if( pClientSocket->m_hReConnectSrvThread != NULL )
- {
- MTVERIFY( WaitForSingleObject( pClientSocket->m_hReConnectSrvThread, INFINITE ) != WAIT_FAILED );
- CloseHandle( pClientSocket->m_hReConnectSrvThread );
- pClientSocket->m_hReConnectSrvThread = NULL;
- }
- pClientSocket->DisConnection();
- delete pClientSocket;
- pClientSocket = NULL;
- }
- }
- return;
- }
- //初始化网络
- BOOL TCP_DLLInitNet( int nAddr, int nNetPort, char strIpAddr[32])
- {
- if (InitNet( nAddr, nNetPort, strIpAddr))
- {
- CClientSocket *pClientSkt = NULL;
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr );
- pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
- MTVERIFY( pClientSkt->m_hReConnectSrvThread = ::CreateThread(NULL, 0, pClientSkt->ReConnectSrvThread, pClientSkt, 0, 0) );
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- //关闭网络
- void TCP_DLLCloseNet( int nAddr, int nNetPort, char strIpAddr[32])
- {
- // 关闭已经打开的串口
- for( unsigned int i = 0; i < g_vtNetStruct.size(); i++ )
- {
- if( (g_vtNetStruct[i].nNetPort == nNetPort) && (strcmp(g_vtNetStruct[i].strIpAddr, strIpAddr) == 0))
- {
- if( g_vtNetStruct[i].pClientskt )
- {
- if( g_vtNetStruct[i].pClientskt->m_hRunObject != NULL )
- {
- SetEvent( g_vtNetStruct[i].pClientskt->m_hRunObject );
- }
- if( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread != NULL )
- {
- MTVERIFY( WaitForSingleObject( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread, INFINITE ) != WAIT_FAILED );
- CloseHandle( g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread );
- g_vtNetStruct[i].pClientskt->m_hReConnectSrvThread = NULL;
- }
- g_vtNetStruct[i].pClientskt->DisConnection();
- if( g_vtNetStruct[i].pClientskt )
- {
- delete g_vtNetStruct[i].pClientskt;
- g_vtNetStruct[i].pClientskt = NULL;
- }
- }
- break;
- }
- }
- }
- BOOL InitNet( int nAddr, int nNetPort, char strIpAddr[32])
- {
- CClientSocket *pClientSkt = NULL;
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr );
- pClientSkt = new CClientSocket;
- char chNetPort[8] = {0};
- itoa(nNetPort, chNetPort, 10);
- NET_STRUCT tagNetStruct = {0};
- if( nNetIndex == -1 )
- {
- if(!pClientSkt->Connection(strIpAddr, chNetPort))
- {
- delete pClientSkt;
- pClientSkt = NULL;
- //pClientSkt->m_bSocket = FALSE;
- return FALSE;
- }
- else
- {
- pClientSkt->m_bSocket = TRUE;
- strcpy(pClientSkt->m_chIpAddr, strIpAddr);
- pClientSkt->m_nNetPort = nNetPort;
- tagNetStruct.nAddr = nAddr;
- tagNetStruct.nNetPort = nNetPort;
- tagNetStruct.pClientskt = pClientSkt;
- memcpy(tagNetStruct.strIpAddr, strIpAddr, sizeof(tagNetStruct.strIpAddr));
- // 网络端口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
- g_vtNetStruct.push_back( tagNetStruct );
- }
- }
- else
- {
- if( g_vtNetStruct[nNetIndex].pClientskt == NULL )
- {
- if(!pClientSkt->Connection(strIpAddr, chNetPort))
- {
- delete pClientSkt;
- pClientSkt = NULL;
- //pClientSkt->m_bSocket = FALSE;
- return FALSE;
- }
- else
- {
- pClientSkt->m_bSocket = TRUE;
- strcpy(pClientSkt->m_chIpAddr, strIpAddr);
- pClientSkt->m_nNetPort = nNetPort;
- g_vtNetStruct[nNetIndex].pClientskt = pClientSkt;
- ResetEvent( g_vtNetStruct[nNetIndex].pClientskt->m_hRunObject );
- }
- }
- else
- {
- if( g_vtNetStruct[nNetIndex].pClientskt->m_bSocket == TRUE )
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- int TCP_DLLRequestStatusData(int nNetPort, int nAddr, char strIpAddr[32], char *pData)
- {
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
- if( nNetIndex == -1 )
- {
- TRACE("打开端口失败!\r\n");
- return ERR_CODE_MODBUS_TCP_NET_FAULT;
- }
- CClientSocket *pNet = NULL;
- pNet = g_vtNetStruct[nNetIndex].pClientskt;
- int nByteNum = pNet->m_structResponse.DataLen;//pNet->m_structResponse.StatusStruct.RtnByteNum[0];
- do
- {
- if( nByteNum == 0 ) // 一直等到收到数据
- {
- nByteNum = pNet->m_structResponse.DataLen;
- Sleep(10);
- }
- else
- {
- break;
- }
- }while( WaitForSingleObject( pNet->m_hRunObject, 125L ) == WAIT_TIMEOUT );
- //TRACE("====DataLen = %d\r\n", pNet->m_structResponse.DataLen);
- if( nByteNum == 0 )//设备没有请求到数据
- {
- //TRACE("设备没有请求到数据\r\n");
- return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
- }
- if( pNet->m_structResponse.nReadResult == 0 ) // 数据没有读成功
- {
- return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
- }
- int i = 0;
-
- while( i < nByteNum )
- {
- pData[i] = pNet->m_structResponse.StrRtnMsg[i];
- i++;
- }
- #if 0
- int nValue;
- WORD wdValue;
- memcpy(&wdValue, pNet->m_structResponse.StrRtnMsg, sizeof(WORD));
- //wdValue = htons(wdValue);
- nValue = htons(wdValue);
- TRACE(">>>>>>>>>结束请求,数据内容=%d\r\n", nValue);
- #endif
- return 0;
- }
- int TCP_DLLRequestWrStatusData(int nNetPort, int nAddr, char strIpAddr[32], int &nWriteResult)
- {
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
- if( nNetIndex == -1 )
- {
- TRACE("打开端口失败!\r\n");
- nWriteResult = 0;
- return ERR_CODE_MODBUS_TCP_NET_FAULT;
- }
- CClientSocket *pNet = NULL;
- pNet = g_vtNetStruct[nNetIndex].pClientskt;
- int nRegNum = pNet->m_structWriteResponse.RegNum;//pNet->m_structResponse.StatusStruct.RtnByteNum[0];
- do
- {
- if( nRegNum == 0 ) // 一直等到收到数据
- {
- nRegNum = pNet->m_structWriteResponse.RegNum;
- Sleep(10);
- }
- else
- {
- break;
- }
- }while( WaitForSingleObject( pNet->m_hRunObject, 125L ) == WAIT_TIMEOUT );
- //TRACE("====DataLen = %d\r\n", pNet->m_structResponse.DataLen);
- if( nRegNum == 0 )//设备没有请求到数据
- {
- //TRACE("设备没有请求到数据\r\n");
- nWriteResult = 0;
- return ERR_CODE_MODBUS_TCP_NET_READ_NO_DATA;
- }
- nWriteResult = 1;
- return 0;
- }
- int NetDataCollect(int nNetPort, int nAddr, char strIpAddr[32],
- WORD wRegNum, WORD wStartAddr, WORD wTranProcessID,
- WORD wProtocolID, int nFuncCode)
- {
- REQUESTPARAM SetBasePara = {0};
- int nNetIndex = GetNetIndex( nNetPort, nAddr, strIpAddr);
- if( nNetIndex == -1 )
- {
- TRACE("打开端口错误\r\n");
- return ERR_CODE_MODBUS_TCP_NET_FAULT;
- }
- CClientSocket *pClientSkt = NULL;
- pClientSkt = g_vtNetStruct[nNetIndex].pClientskt;
- //TRACE("Init m_structResponse\r\n");
- memset(&pClientSkt->m_structResponse, 0, sizeof(pClientSkt->m_structResponse));
- //pClientSkt->m_structResponse.nReadResult = 0;
-
- WORD wByteNum = sizeof(SetBasePara.RegNum) +
- sizeof(SetBasePara.StartAddr) +
- sizeof(SetBasePara.Header.EquipAddr) +
- sizeof(SetBasePara.Header.FuncCode);
- SetBasePara.Header.TranProcessID = htons(wTranProcessID);
- SetBasePara.Header.ProtocolID = htons(wProtocolID);
- SetBasePara.Header.FuncCode = nFuncCode;
- SetBasePara.Header.EquipAddr = (BYTE)nAddr;
- SetBasePara.Header.ByteNum = htons(wByteNum);
- SetBasePara.RegNum = htons(wRegNum);
- SetBasePara.StartAddr = htons(wStartAddr);
- return pClientSkt->WorkMain(SetBasePara);
- }
|