📄 commport.cpp
字号:
// CommPort.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "CommPort.h"
#include "DlgCommPortSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CCommPortApp
BEGIN_MESSAGE_MAP(CCommPortApp, CWinApp)
//{{AFX_MSG_MAP(CCommPortApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommPortApp construction
CCommPortApp::CCommPortApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
CCommPortApp::~CCommPortApp()
{
POSITION pos = m_listComm.GetStartPosition();
while(pos)
{
int nPort;
CComm* pComm = NULL;
m_listComm.GetNextAssoc(pos, (void*&)nPort, (void*&)pComm);
delete pComm;
}
m_listComm.RemoveAll();
}
CComm* CCommPortApp::GetComm(int nCommPort)
{
CComm *pComm = NULL;
if(!m_listComm.Lookup((LPVOID)nCommPort, (void*&)pComm))
{
pComm = new CComm;
pComm->m_nCommPort = nCommPort;
m_listComm.SetAt((LPVOID)nCommPort, (LPVOID)pComm);
}
return pComm;
}
void CCommPortApp::DelComm(int nCommPort)
{
CComm *pComm = NULL;
if(m_listComm.Lookup((LPVOID)nCommPort, (void*&)pComm))
{
m_listComm.RemoveKey((LPVOID)nCommPort);
delete pComm;
}
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CCommPortApp object
CCommPortApp theApp;
void SetPrjPath(const char* szPath)
{//修改配置路径
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.m_strPath = CString(szPath) + "\\";
}
bool SetComm(int *pnComm,
int *pnBaud,
int *pnDataBit,
float *pfStopBit,
int *pnTimeOut,
char *pcParity,
long *pnTimeInterval)
{//串口选择和参数设置(多参数方式)
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if(pnComm && pnBaud && pnDataBit && pfStopBit &&
pnTimeOut && pcParity && pnTimeInterval)
{
CDlgCommPortSet dlg;
dlg.m_strCommPort.Format("COM%d", *pnComm);
dlg.m_strBaud.Format("%d", *pnBaud);
dlg.m_strDataBit.Format("%d", *pnDataBit);
dlg.m_strStopBit.Format("%g", *pfStopBit);
dlg.m_strTimeOut.Format("%dms", *pnTimeOut);
dlg.m_strParity.Format("%c", *pcParity);
dlg.m_strTimeInterval.Format("%dms", *pnTimeInterval);
if(dlg.DoModal()==IDOK && dlg.m_bModifyFlag)
{
if(dlg.m_strCommPort.GetLength()>3)
*pnComm = atoi(dlg.m_strCommPort.Mid(3));
*pnBaud = atoi(dlg.m_strBaud);
*pnDataBit = atoi(dlg.m_strDataBit);
*pfStopBit = (float)atof(dlg.m_strStopBit);
*pnTimeOut = atoi(dlg.m_strTimeOut);
if(dlg.m_strParity.GetLength()>1)
*pcParity = dlg.m_strParity.GetAt(0);
*pnTimeInterval = atoi(dlg.m_strTimeInterval);
CComm::LoadSaveCommPortParams(*pnComm,
*pnBaud,
*pnDataBit,
*pfStopBit,
*pnTimeOut,
*pcParity,
*pnTimeInterval,
FALSE);
return TRUE;
}
}
return FALSE;
}
bool OpenComm(int nComm,
int nBaud,
int nDataBit,
float fStopBit,
int nTimeOut,
char cParity,
long nTimeInterval)
{//打开串口(多参数方式)
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CComm *pComm = theApp.GetComm(nComm);
ASSERT(pComm);
pComm->m_nCommPort = nComm;
pComm->m_Baud = nBaud;
pComm->m_nDataBit = nDataBit;
pComm->m_fStopBit = fStopBit;
pComm->m_cParity = cParity;
pComm->m_Timeout = nTimeOut;
pComm->m_nTimeInterval = nTimeInterval;
pComm->m_bParamSetted = TRUE;
return pComm->Open();
}
bool SetCommEx(int *pnComm)
{//串口选择和参数设置(简单参数方式)
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if(pnComm)
{
int nBaud;
int nDataBit;
float fStopBit;
int nTimeOut;
char cParity;
long nTimeInterval;
CComm::LoadSaveCommPortParams(*pnComm,
nBaud,
nDataBit,
fStopBit,
nTimeOut,
cParity,
nTimeInterval,
TRUE);
return SetComm(pnComm,
&nBaud,
&nDataBit,
&fStopBit,
&nTimeOut,
&cParity,
&nTimeInterval);
}
return FALSE;
}
bool OpenCommEx(int nComm)
{//打开串口(简单参数方式)
AFX_MANAGE_STATE(AfxGetStaticModuleState());
int nBaud;
int nDataBit;
float fStopBit;
int nTimeOut;
char cParity;
long nTimeInterval;
CComm::LoadSaveCommPortParams(nComm,
nBaud,
nDataBit,
fStopBit,
nTimeOut,
cParity,
nTimeInterval,
TRUE);
return OpenComm(nComm,
nBaud,
nDataBit,
fStopBit,
nTimeOut,
cParity,
nTimeInterval);
}
bool CloseComm(int nComm)
{//关闭指定的串口
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.DelComm(nComm);
return true;
}
bool WriteComm(int nComm, const char* pData, int nLen)
{//向指定的串口写数据
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CComm *pComm = theApp.GetComm(nComm);
ASSERT(pComm);
return pComm->Write(pData, nLen);
}
int ReadComm(int nComm, char* pData, int nLen)
{//从指定的串口写数据
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CComm *pComm = theApp.GetComm(nComm);
ASSERT(pComm);
return pComm->Read(pData, nLen);
}
BOOL CCommPortApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
char szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
m_strPath = szPath;
if(m_strPath.ReverseFind('\\')!=-1)
m_strPath = m_strPath.Left(m_strPath.ReverseFind('\\')+1);
return CWinApp::InitInstance();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -