123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- // MGE.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "icpdas.h"
- #include "CommRS232.h"
- #include "Head.h"
- #include "winsock2.h"
- #include "ProtocolModbus.h"
- #include <stdlib.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,
- BYTE Start,//起始位
- BYTE StartAddr[2],//起始地址
- int nRegNum,//读取的寄存器个数
- BYTE FuncCode[2],
- int nDataLen,
- BYTE byWithAddrFlag,
- char chMsg[80]);
- int GetCommIndex( int nCommPort, int nAddr );
- // CMGEApp
- BEGIN_MESSAGE_MAP(CIcpdasApp, CWinApp)
- END_MESSAGE_MAP()
- // CMGEApp 构造
- CIcpdasApp::CIcpdasApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CMGEApp 对象
- CIcpdasApp theApp;
- // CMGEApp 初始化
- BOOL CIcpdasApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- int CIcpdasApp::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();
- }
- int ICPDAS_DLLInit( int nCommPort,
- int nAddr,
- BYTE Start,//起始位
- BYTE StartAddr[2],//起始地址
- int nRegNum,//读取的寄存器个数
- BYTE FuncCode[2],
- int nDataLen,
- BYTE byWithAddrFlag,
- char chMsg[80])
- {
- return CommDataCollect(nCommPort,
- nAddr,
- Start,
- StartAddr,
- nRegNum,
- FuncCode,
- nDataLen,
- byWithAddrFlag, chMsg);
- }
- void ICPDAS_DLLUnInit()
- {
- // 关闭串口,放到退出Dll时去关闭 ExitInstance
- return;
- }
- //初始化串口
- BOOL ICPDAS_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
- {
- return TRUE;
- }
- else
- {
- 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))
- {
- 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 ICPDAS_DLLRequestStatusData(int nCommPort, int nAddr, int nByteNum, char *pData, int ivarid)
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- nByteNum = pComm->m_pProtocol->m_structResponse.StatusStruct.RtnByteNum;
- if( nByteNum == 0) // 设备没有请求到数据
- {
- return ERR_CODE_ICPDAS_COM_READ_NO_DATA;
- }
- int i = 0;
- int nNum = nByteNum;
- while( i < nNum )
- {
- pData[i] = pComm->m_pProtocol->m_structResponse.StrRtnMsg[i];
- i++;
- }
- return 0;
- }
- int CommDataCollect(int nCommPort,
- int nAddr,
- BYTE Start,//起始位
- BYTE StartAddr[2],//起始地址
- int nRegNum,//读取的寄存器个数
- BYTE FuncCode[2],
- int nDataLen,
- BYTE byWithAddrFlag,
- char chMsg[80])
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- return pComm->WorkMain(nAddr, Start, StartAddr, nRegNum, FuncCode, nDataLen,byWithAddrFlag, chMsg);
- }
- int GetCommIndex( int nCommPort, int nAddr )
- {
- //BOOL bOpenFlag = FALSE;
- for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
- {
- if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
- {
- return i;
- // bOpenFlag = TRUE;
- }
- //else
- //{
- // if ((g_vtComStruct[i].nAddr == nAddr) && bOpenFlag)
- // return i;
-
- //}
- }
- return -1;
- }
- int ICPDAS_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], int nDataLen)
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- int iResult = pComm->WriteCommand(DataBuffer, nDataLen);
- return iResult;
- }
|