// Daikin.cpp : 定义 DLL 的初始化例程。 // #include "stdafx.h" #include "Daikin.h" #include ".\daikin.h" #include "CommRS232.h" #include "Head.h" #include "winsock2.h" #include "ProtocolModbus.h" vector<__COM_STRUCT> g_vtComStruct; #ifdef _DEBUG #define new DEBUG_NEW #endif // // 注意! // // 如果此 DLL 动态链接到 MFC // DLL,从此 DLL 导出并 // 调入 MFC 的任何函数在函数的最前面 // 都必须添加 AFX_MANAGE_STATE 宏。 // // 例如: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // 此处为普通函数体 // } // // 此宏先于任何 MFC 调用 // 出现在每个函数中十分重要。这意味着 // 它必须作为函数中的第一个语句 // 出现,甚至先于所有对象变量声明, // 这是因为它们的构造函数可能生成 MFC // DLL 调用。 // // 有关其他详细信息, // 请参阅 MFC 技术说明 33 和 58。 // // CDaikinApp BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval ); int CommDataCollect( int nCommPort, //串口号 int nAddr, //地址 int nVer, //版本号 int nCid1, //控制标识码 int nCid2, //命令信息 WORD wLenId, //INFO字节长度 int nCmdID, //命令ID int nDataLen, //请求数据长度 int nCmdPos, //变量索引 int nCmdLen, //变量长度 char chMsg[80], //读到的变量值 char *byDataFlag); //保留未用 int GetCommIndex( int nCommPort, int nAddr ); BEGIN_MESSAGE_MAP(CDaikinApp, CWinApp) END_MESSAGE_MAP() // CDaikinApp 构造 CDaikinApp::CDaikinApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } // 唯一的一个 CDaikinApp 对象 CDaikinApp theApp; // CDaikinApp 初始化 BOOL CDaikinApp::InitInstance() { CWinApp::InitInstance(); return TRUE; } int CDaikinApp::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 DAIKIN_DLLInit( int nCommPort, //串口号 int nAddr, //地址 int nVer, //版本号 int nCid1, //控制标识码 int nCid2, //命令信息 WORD wLenId, //INFO字节长度 int nCmdID, //命令ID int nDataLen, //请求数据长度 int nCmdPos, //变量索引 int nCmdLen, //变量长度 char chMsg[80], //读到的变量值 char *byDataFlag) //保留未用 { return CommDataCollect( nCommPort, nAddr, nVer, nCid1, nCid2, wLenId, nCmdID, nDataLen, nCmdPos, nCmdLen, chMsg, byDataFlag); } void DAIKIN_DLLUnInit() { // 关闭串口,放到退出Dll时去关闭 ExitInstance return; } //初始化串口 BOOL DAIKIN_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; } } int DAIKIN_DLLSetParam( int nCommPort, //端口 int nAddr, //地址 int nVer, //版本号 int nCid1, //控制标识码 int nCid2, //命令信息 WORD wLenId, //INFO字节长度 int nCmdType, //命令类型 int nSetValue) //设定值 { int nCommIndex = GetCommIndex( nCommPort, nAddr ); if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT; CCommAsyn *pComm = NULL; pComm = g_vtComStruct[nCommIndex].pComm; int iResult = pComm->WriteCommand( nCommPort, nAddr, nVer, nCid1, nCid2, wLenId, nCmdType, nSetValue ); return iResult; } int DAIKIN_DLLRemoteCtrl( int nCommPort, //端口 int nAddr, //地址 int nSetType, //设定类型 int nSetIndex, //设定值索引,只针对特殊变量(例如:空调制冷、制热温度) int nSetValue) //设定值 { int nCommIndex = GetCommIndex( nCommPort, nAddr ); if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT; CCommAsyn *pComm = NULL; pComm = g_vtComStruct[nCommIndex].pComm; int iResult = pComm->WriteCommand( nCommPort, nAddr, nSetType, nSetIndex, nSetValue ); return iResult; } 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 CommDataCollect( int nCommPort, //串口号 int nAddr, //地址 int nVer, //版本号 int nCid1, //控制标识码 int nCid2, //命令信息 WORD wLenId, //INFO字节长度 int nCmdID, //命令ID int nDataLen, //请求数据长度 int nCmdPos, //变量索引 int nCmdLen, //变量长度 char chMsg[80], //读到的变量值 char *byDataFlag) //保留未用 { int nCommIndex = GetCommIndex( nCommPort, nAddr ); if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT; CCommAsyn *pComm = NULL; pComm = g_vtComStruct[nCommIndex].pComm; return pComm->WorkMain(nAddr, nVer, nCid1, nCid2, wLenId, nCmdLen, nDataLen, nCmdPos, nCmdLen, chMsg, byDataFlag); } 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; }