⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintaindoc.cpp

📁 串口收发工具
💻 CPP
字号:
// maintainDoc.cpp : implementation of the CMaintainDoc class
//

#include "stdafx.h"
#include "maintain.h"
#include "manager.h"
#include "maintainDoc.h"
#include "mainfrm.h"
#include "maintainview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern bool bShowData;
/////////////////////////////////////////////////////////////////////////////
// CMaintainDoc

IMPLEMENT_DYNCREATE(CMaintainDoc, CDocument)

BEGIN_MESSAGE_MAP(CMaintainDoc, CDocument)
	//{{AFX_MSG_MAP(CMaintainDoc)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CMaintainDoc construction/destruction

CMaintainDoc::CMaintainDoc()
{
	// TODO: add one-time construction code here
	m_pSerialLineInfo = new SerialLineInfo;
	if(m_pSerialLineInfo)
	{
		m_pSerialLineInfo->m_nComNo = 1;
		m_pSerialLineInfo->m_nComBaut = B9600;//B19200;
		m_pSerialLineInfo->m_nDataBit = BIT_8;
		m_pSerialLineInfo->m_nStopBit = STOP_1;
		m_pSerialLineInfo->m_nParity = P_NONE;
		}
	m_pSerialComm = new CSerialComm;
	CManager::SetGlobalSerialComm(m_pSerialComm);
	bOpenCom = false;
}

CMaintainDoc::~CMaintainDoc()
{
	if(m_pSerialLineInfo)
		delete m_pSerialLineInfo;
	if(m_pSerialComm)
	{
        m_pSerialComm->Close();
		delete m_pSerialComm;
	}
}

BOOL CMaintainDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

BOOL CMaintainDoc::SetSerialLineInfo(LPSerialLineInfo pSerialLineInfo)
{
	BeginWaitCursor();
	memcpy(m_pSerialLineInfo,pSerialLineInfo,sizeof(SerialLineInfo));

    CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
	
	if(pFrame->IsSerialLineOpen())
		CloseComm();

    if(!OpenComm())
	{
		EndWaitCursor();
		return FALSE;
	}
	EndWaitCursor();	
	return TRUE;
}

BOOL CMaintainDoc::OpenComm()
{ 
    if(!m_pSerialComm->Open(m_pSerialLineInfo->m_nComNo,
							   m_pSerialLineInfo->m_nComBaut,
		                       m_pSerialLineInfo->m_nDataBit,
						       m_pSerialLineInfo->m_nStopBit,
						       m_pSerialLineInfo->m_nParity,
						       m_pSerialLineInfo->m_FlowControl))
	{
		((CMainFrame*)AfxGetMainWnd())->SetComParaToStatusBar(0);
		bOpenCom = false;
		return false;
	}
	((CMainFrame*)AfxGetMainWnd())->SetComParaToStatusBar(1);
	bOpenCom = true;
	return true;
}

void CMaintainDoc::CloseComm()
{
	m_pSerialComm->Close();
	((CMainFrame*)AfxGetMainWnd())->SetComParaToStatusBar(0);
	bOpenCom = false;
}

void CMaintainDoc::GetCommOption(int &comno, int &Baut, int &DataBit, int &StopBit, int &Parity)
{
	comno = m_pSerialLineInfo->m_nComNo; 
    Baut = m_pSerialLineInfo->m_nComBaut;
	DataBit = m_pSerialLineInfo->m_nDataBit;
	StopBit = m_pSerialLineInfo->m_nStopBit;
	Parity = m_pSerialLineInfo->m_nParity;
}

BOOL CMaintainDoc::SendData(BYTE *pData, int nLength,BOOL bShow)
{
	if(bOpenCom)
	{
		if(m_pSerialComm->Send(pData,nLength))
		{
			if(bShowData)
				((CMainFrame*)AfxGetMainWnd())->ShowSendData(pData,nLength);
				/*
				POSITION pos = GetFirstViewPosition();
				while (pos != NULL)
				{
				CMaintainView *pView = (CMaintainView *)GetNextView(pos);
				if(nLength>0)
				pView->ShowData(pData,nLength,1);
				}   
			*/
			return TRUE;
		}
		//else
		//	AfxMessageBox(_T("数据发送错误,请确认串口已经打开且未被占用!"),MB_ICONQUESTION);
	}
	return FALSE;
}

int CMaintainDoc::ReceiveData(BYTE *pData, int nLength)
{
	int ReceiveLength =  m_pSerialComm->Receive(pData,nLength);
   
   ((CMainFrame*)AfxGetMainWnd())->ReceiveData(pData,ReceiveLength);
	
   if((bShowData)&&(ReceiveLength>0))
		((CMainFrame*)AfxGetMainWnd())->ShowRecData(pData,ReceiveLength);
   /*
   POSITION pos = GetFirstViewPosition();

   while (pos != NULL)
   {
		CMaintainView *pView = (CMaintainView *)GetNextView(pos);
		if(ReceiveLength>0)
			pView->ShowData(pData,ReceiveLength,2);
   } */  
   return ReceiveLength;
}

bool CMaintainDoc::SetRTS(bool bSetRTS)
{
	return m_pSerialComm->EnableRTS(bSetRTS);
}

//在1-维护时修改波特率为19200,2-规约测试时9600
void CMaintainDoc::ChangeComSet(int wokmode)
{
	m_pSerialLineInfo->m_nComBaut = (wokmode==1)?B19200:B9600;
	SetSerialLineInfo(m_pSerialLineInfo);
}


/////////////////////////////////////////////////////////////////////////////
// CMaintainDoc serialization

void CMaintainDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMaintainDoc diagnostics

#ifdef _DEBUG
void CMaintainDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMaintainDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMaintainDoc commands

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -