123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // MGE.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "CommRS232.h"
- #include "Head.h"
- #include "winsock2.h"
- #include "ProtocolModbus.h"
- #include <stdlib.h>
- #include ".\Gree.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, int nDataLen,int nReadPos, char chMsg[80]);
- int GetCommIndex( int nCommPort, int nAddr );
- // CMGEApp
- BEGIN_MESSAGE_MAP(CGreeApp, CWinApp)
- END_MESSAGE_MAP()
- // CMGEApp 构造
- CGreeApp::CGreeApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CMGEApp 对象
- CGreeApp theApp;
- // CMGEApp 初始化
- BOOL CGreeApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- int CGreeApp::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 GREE_DLLInit( int nCommPort, int nAddr, int nDataLen, int nReadPos, char chMsg[80] )
- {
- return CommDataCollect(nCommPort, nAddr, nDataLen, nReadPos, chMsg);
- }
- void GREE_DLLUnInit()
- {
- // 关闭串口,放到退出Dll时去关闭 ExitInstance
- return;
- }
- //初始化串口
- BOOL GREE_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 GREE_DLLWrCom( int nCommPort, int nAddr,
- int nWorkStatus, int nWorkMode, int nFanSpeed,
- int nPutWind, int nAirTrade, int nSleep, int nLight,
- int nTempSetPoint )
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_GREE_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- int iResult;
- iResult = pComm->WriteCommand(nAddr, nWorkStatus, nWorkMode, nFanSpeed,
- nPutWind, nAirTrade, nSleep, nLight, nTempSetPoint);
- return iResult;
- }
- int CommDataCollect(int nCommPort, int nAddr, int nDataLen, int nReadPos, char chMsg[80])
- {
- int nCommIndex = GetCommIndex( nCommPort, nAddr );
- if( nCommIndex == -1 ) return ERR_CODE_GREE_COM_FAULT;
- CCommAsyn *pComm = NULL;
- pComm = g_vtComStruct[nCommIndex].pComm;
- return pComm->WorkMain(nAddr, nDataLen, nReadPos, 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;
- }
|