// CommInterface.cpp: implementation of the CCommInterface class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CommInterface.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCommInterface::CCommInterface() { } CCommInterface::~CCommInterface() { list::iterator it; for( it = m_listComm.begin(); it != m_listComm.end(); ) { m_listComm.erase( it++ ); } } CCommProcess* CCommInterface::FindComm(int nCommPort) { list::iterator it; COM_STRUCT tagComm; for( it = m_listComm.begin(); it != m_listComm.end(); ) { tagComm = *(it++); if( tagComm.nCommPort == nCommPort && tagComm.pComm != NULL ) { return tagComm.pComm; } else { //LOG4C((LOG_NOTICE,"串口不存在")); } } return NULL; } BOOL CCommInterface::OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval) { if( FindComm( nCommPort ) == NULL ) { 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; //间隔时间 COM_STRUCT tagComm; tagComm.nCommPort = nCommPort; tagComm.pComm = new CCommProcess(); if( !tagComm.pComm->OpenComm( &portParam ) ) { //LOG4C((LOG_NOTICE , "CCommInterface : 打开串口失败")); delete tagComm.pComm; tagComm.pComm = NULL; return FALSE; } else { m_listComm.insert(m_listComm.end(), tagComm); } } return TRUE; } BOOL CCommInterface::CloseComm() { list::iterator it; COM_STRUCT tagComm; for( it = m_listComm.begin(); it != m_listComm.end(); ) { tagComm = *(it++); tagComm.pComm->CloseComm(); } return TRUE; }