You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TwoLaserHead-PushJig/LaiPuLaser/CommPortMgr.cpp

143 lines
3.2 KiB
C++

#include "StdAfx.h"
#include "CommPortMgr.h"
#include "LogMgr.h"
CCommPortMgr::CCommPortMgr(void)
{
}
CCommPortMgr::~CCommPortMgr(void)
{
}
//打开com 口(dwBaudRate 是波特率)
bool CCommPortMgr::Open(DWORD dwPort, DWORD dwBaudRate)
{
if(IsOpen())//打开状态直接返回
return true;
CString str = "Function :[CCommPortMgr::Open][打开com 口]";
gLogMgr->WriteDebugLog(str);
CString str1;
str1.Format("%ld",dwPort);
str = "Result :[com 口<"+str1+">打开]";
bool ret;
if(m_Com.Open(dwPort,dwBaudRate) == false)
{
gLogMgr->WriteDebugLog(str,_LOG_ERROR);
ret = false;
}
else
{
str += "[OK]";
gLogMgr->WriteDebugLog(str);
ret = true;
}
return ret;
}
//打开串口, 使用类似"9600, n, 8, 1"的设置字符串设置串口
bool CCommPortMgr::Open(DWORD dwPort, char *szSetStr)
{
CString str = "Function :[CCommPortMgr::Open][打开com 口]";
//gLogMgr->WriteDebugLog(str);
CString str1;
str1.Format("%ld",dwPort);
str = "Result :[com 口<"+str1+">打开]";
bool ret;
if(m_Com.Open(dwPort,szSetStr) == false)
{
//gLogMgr->WriteDebugLog(str,_LOG_ERROR);
ret = false;
}
else
{
str += "[OK]";
//gLogMgr->WriteDebugLog(str);
ret = true;
}
return ret;
}
//绑定串口消息的窗口ID
void CCommPortMgr::SetWnd(HWND hWnd)
{
CString str = "Function :[SetWnd][绑定串口消息的响应窗口ID]";
gLogMgr->WriteDebugLog(str);
m_Com.SetWnd(hWnd);
}
//读取串口消息到szBuffer
CString CCommPortMgr::ReadBuf(char *szBuffer,DWORD dwBufferLength)
{
CString str = "Function :[ReadString][读取串口消息]";
//gLogMgr->WriteDebugLog(str);
m_Com.ReadString(szBuffer,dwBufferLength);
return str;
}
CString CCommPortMgr::ReadStr()
{
CString str = "Function :[ReadString][读取串口消息]";
gLogMgr->WriteDebugLog(str);
const int dwBufferLength = 500;
char szBuffer[dwBufferLength];
memset(szBuffer,'0',dwBufferLength);
m_Com.ReadString(szBuffer,dwBufferLength);
//转化为CString
str = szBuffer;
//gLogMgr->WriteDebugLog(str);
return str;
}
int CCommPortMgr::Read(LPVOID Buffer, DWORD dwBufferLength)
{
//CString str = "Function :[ReadString][读取串口消息]";
//gLogMgr->WriteDebugLog(str);
return m_Com.Read(Buffer,dwBufferLength);
}
//发送消息到串口
void CCommPortMgr::Write(const char *szBuffer)
{
//CString str = "Function :[Write][发送消息到串口]";
//gLogMgr->WriteDebugLog(str+szBuffer);
m_Com.Write(szBuffer);
}
void CCommPortMgr::WriteBuf(LPVOID Buffer, DWORD dwBufferLength)
{
//CString str = "Function :[Write][发送消息到串口]";
//gLogMgr->WriteDebugLog(str);
m_Com.Write(Buffer,dwBufferLength);
}
void CCommPortMgr::ClearCache()
{
m_Com.SetupPort();
}
//关闭串口
void CCommPortMgr::Close()
{
CString str = "Function :[Close][关闭串口]";
gLogMgr->WriteDebugLog(str);
m_Com.Close();
}
void CCommPortMgr::SetBufferSize(DWORD dwInputSize, DWORD dwOutputSize)
{
m_Com.SetBufferSize(dwInputSize,dwOutputSize);
}
int CCommPortMgr::GetInputSize()
{
gLogMgr->WriteDebugLog("BufferSize","",m_Com.GetInputSize());
return 0;
}