123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- // MGE.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "CommRS232.h"
- #include "Head.h"
- #include "winsock2.h"
- #include "ProtocolModbus.h"
- #include <stdlib.h>
- #include ".\modbusrtu.h"
- #pragma comment(lib, "ws2_32.lib ")
- vector<__COM_STRUCT> g_vtComStruct;
- //vector<__BASIC_STRUCT> g_vtReadBasicInfo;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval );
- int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80]);
- int GetCommIndex( int nCommPort, int nAddr );
- // CMGEApp
- BEGIN_MESSAGE_MAP(CModbusrtuApp, CWinApp)
- END_MESSAGE_MAP()
- // CMGEApp 构造
- CModbusrtuApp::CModbusrtuApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CMGEApp 对象
- CModbusrtuApp theApp;
- // CMGEApp 初始化
- BOOL CModbusrtuApp::InitInstance()
- {
- CWinApp::InitInstance();
- LOG4C((LOG_NOTICE, "Enter InitInstance"));
- return TRUE;
- }
- int CModbusrtuApp::ExitInstance()
- {
- // TODO: 在此添加专用代码和/或调用基类
- // 关闭已经打开的串口
- for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
- {
- if( g_vtComStruct[i].pComm )
- {
- delete g_vtComStruct[i].pComm;
- g_vtComStruct[i].pComm = NULL;
- }
- }
- return CWinApp::ExitInstance();
- }
- /************************************************************************/
- /* 函数:RTU_DLLInit[4/25/2016 Home];
- /* 描述:对RTU设备发送采集数据指令;
- /* 参数:;
- /* [IN] nCommPort:串口号;
- /* [IN] nAddr:指定的设备地址值;
- /* [IN] SetBasePara:用于设置RTU设备的参数;
- /* [IN] nDataLen:数据长度;
- /* [IN] chMsg:请求的命令行;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int RTU_DLLInit(IN int nCommPort, IN int nAddr, IN SETBASEPARAM SetBasePara, IN int nDataLen, IN char chMsg[80] )
- {
- return CommDataCollect(nCommPort, nAddr, SetBasePara, nDataLen, chMsg);
- }
- void RTU_DLLUnInit()
- {
- // 关闭串口,放到退出Dll时去关闭 ExitInstance
- return;
- }
- /************************************************************************/
- /* 函数:RTU_DLLInitCom[4/25/2016 Home];
- /* 描述:初始化串口;
- /* 参数:;
- /* [IN] nAddr:指定设备地址值;
- /* [IN] nCommPort:串口号;
- /* [IN] nRate:波特率;
- /* [IN] nDataBit:数据位;
- /* [IN] nStopBit:停止位;
- /* [IN] nParity:校验位;
- /* [IN] nInterval:超时值;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL RTU_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
- {
- LOG4C((LOG_NOTICE, "RTU_DLLInitCom return true"));
- return TRUE;
- }
- else
- {
- LOG4C((LOG_NOTICE, "RTU_DLLInitCom return false"));
- return FALSE;
- }
- }
- BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
- {
- CCommAsyn *pComm = NULL;
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 )
- {
- pComm = new CCommRS232;
- PORTPARAM portParam;
- portParam.StartAddr = nAddr; //起止地址
- portParam.PortNo = nCommPort; //Com端口
- portParam.BaudRate = nRate; //波特率
- portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
- portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
- portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
- portParam.Interval = nInterval; //间隔时间
- if(!pComm->InitParam(&portParam))
- {
- LOG4C((LOG_NOTICE, "Comm%d Open lost, nAddr = %d, Rate = %d, DataBit = %d, StopBit = %d, Parity = %d",
- nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity));
- delete pComm;
- pComm = NULL;
- return FALSE;
- }
- else
- {
- COM_STRUCT tagComStruct;
- tagComStruct.nAddr = nAddr;
- tagComStruct.nCommPort = nCommPort;
- tagComStruct.pComm = pComm;
- // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
- g_vtComStruct.push_back( tagComStruct );
- }
- }
- return TRUE;
- }
- int RTU_DLLRequestWrStatusData(int nCommPort, int nAddr, char *pData)
- {
- return 0;
- }
- int RTU_DLLWrite(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, double Data, int nDataLen)
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_MODBUS_RTU_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- int iResult = pComm->WriteCommand(SetBasePara, Data, nDataLen);
- return iResult;
- }
- int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80])
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_MODBUS_RTU_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- return pComm->WorkMain(SetBasePara, nDataLen, chMsg);
- }
- int GetCommIndex( int nCommPort, int nAddr )
- {
- for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
- {
- if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
- {
- return i;
- }
- }
- return -1;
- }
|