// Upskehua.cpp : 定义 DLL 的初始化例程。 // #include "stdafx.h" #include "Upskehua.h" #include "CommRS232.h" #include "Head.h" #include "winsock2.h" #include "ProtocolModbus.h" #include #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, char szCmd[32], char chSendMsg[32], int nDataLen, char chMsg[UPS_KE_HUA_MAX_MSG]); int GetCommIndex( int nCommPort, int nAddr ); // CMGEApp BEGIN_MESSAGE_MAP(CUpskehuaApp, CWinApp) END_MESSAGE_MAP() // CMGEApp 构造 CUpskehuaApp::CUpskehuaApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } // 唯一的一个 CMGEApp 对象 CUpskehuaApp theApp; // CMGEApp 初始化 BOOL CUpskehuaApp::InitInstance() { CWinApp::InitInstance(); return TRUE; } int CUpskehuaApp::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 UPSKEHUA_DLLInit(int nCommPort, int nAddr, char szCmd[32], char chSendMsg[32], int nDataLen, char chMsg[UPS_KE_HUA_MAX_MSG]) { return CommDataCollect(nCommPort, nAddr, szCmd, chSendMsg, nDataLen, chMsg); } void UPSKEHUA_DLLUnInit() { // 关闭串口,放到退出Dll时去关闭 ExitInstance return; } //初始化串口 BOOL UPSKEHUA_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 CommDataCollect(int nCommPort, int nAddr, char szCmd[32], char chSendMsg[32], int nDataLen, char chMsg[UPS_KE_HUA_MAX_MSG]) { int nCommIndex = GetCommIndex( nCommPort, nAddr ); if( nCommIndex == -1 ) return ERR_CODE_UPSKEHUA_COM_FAULT; CCommAsyn *pComm = NULL; pComm = g_vtComStruct[nCommIndex].pComm; return pComm->WorkMain(nCommPort, nAddr, szCmd, chSendMsg, nDataLen,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) { if (g_vtComStruct[i].nAddr == nAddr) return i; // bOpenFlag = TRUE; } //else //{ // if ((g_vtComStruct[i].nAddr == nAddr) && bOpenFlag) // return i; //} } return -1; } int UPSKEHUA_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], int nDataLen) { int nCommIndex = GetCommIndex( nCommPort, nAddr ); if( nCommIndex == -1 ) return ERR_CODE_UPSKEHUA_COM_FAULT; CCommAsyn *pComm = NULL; pComm = g_vtComStruct[nCommIndex].pComm; int iResult = pComm->WriteCommand(DataBuffer, nDataLen); return iResult; }