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

📄 mainfrm.cpp

📁 多文档界面设计配置文件读写串口操作串口封装类
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SerialSun.h"

#include "MainFrm.h"

#include "CRC.h"
#include "OutInfoToFile.h"
#include "SerialSunDoc.h"
#include "SerialSunView.h"
#include "SerialPort.h"
#include "StringChang.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
		
    //接受数据消息
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
    
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_VIEW_DATA,DataView)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	i=0;
	m_dataview=NULL;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{ 
    CMDIFrameWnd   *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
	CMDIChildWnd   *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame();
	CSerialSunView *pView   =   (CSerialSunView*)pChild->GetActiveView();
	if(port == 4)
	{
		
		mybuff[i] = char(ch);
		i++;

		if(i == 1)
		{
		if(0xDB != mybuff[0])
			{
				i = 0;
				
			}
		}
		if(i == 2)
		{
		if(0x90 != mybuff[1])
			{
				i = 0;
				
			}
		}
		if(i == 3)
		{
		if(0xDB != mybuff[2])
			{
				i = 0;
				nGetCtring = "校验错误";
			}
		}
		if(i == 4)
		{
		if(0x90 != mybuff[3])
			{
				i = 0;
				nGetCtring = "校验错误";
			}
		}
		
		if(i == 10)
		{
			unsigned short nGetCrc = CCRC::RunCRC16(mybuff,10,0x1021);
			if(nGetCrc == 0)
			{
			
				CStringChang::Bytes2String(mybuff,10,tempBuf);				//字节数据转换为可打印字符串			
				nGetCtring.Format("%s",tempBuf);
				pView->PostMessage(WM_GETSTR_FROMFRM,(WPARAM)&nGetCtring,0);   //发送消息数据

				DOORDATA DoorData;				
				CString outinfo;  //显示信息
				CMainFrame::GetDoorData(mybuff,DoorData);
				outinfo.Format("站点%d  %s",DoorData.RTUID,nGetCtring);
				AfxOutInfoToFile(outinfo);
				 
				i=0;//清空缓冲区内容  
			}
		}
		if(i == 16)
		{

			unsigned short nGetCr = CCRC::RunCRC16(mybuff,16,0x1021);
			if(nGetCr == 0)
			{
				CStringChang::Bytes2String(mybuff,16,tempBuf);				//字节数据转换为可打印字符串			
				nGetCtring.Format("%s",tempBuf);

     			pView->PostMessage(WM_GETSTR_FROMFRM,(WPARAM)&nGetCtring,0);   //发送消息数据

				DOORDATA DoorData;				
				CString outinfo;  //显示信息
				CMainFrame::GetDoorData(mybuff,DoorData);
				outinfo.Format("站点%d  %s",DoorData.RTUID,nGetCtring);
				AfxOutInfoToFile(outinfo);
			}

			i=0;
		} 
	}
	return 0;
}
//将协议字符串转换为各个数据
void CMainFrame::GetDoorData(const unsigned char* aData,DOORDATA &DoorData)
{
	
	DoorData.RTUID     = aData[4];                  //站号
	DoorData.UPLEVEL   = aData[6]<<8 | aData[5];    //上游水位
	DoorData.DOWNLEVEL = aData[8]<<8 | aData[7];    //下游水位
	DoorData.DOORLEVEL = aData[10]<<8 | aData[9];   //闸门开度
	DoorData.POWERLEVEL = aData[12]<<8 | aData[11]; //电池电量
	DoorData.AUTOHAND =  aData[13]&0x01;            //手自动
	DoorData.UP   = aData[13]>>4&0x01;              //升
	DoorData.DOWN = aData[13]>>5&0x01;              //降
	DoorData.STOP = aData[13]>>6&0x01;    		    //停
} 
void CMainFrame::DataView(void)
{
	if(m_dataview!=NULL)
	{
		m_dataview->MDIActivate();
		return;
	}

	m_dataview = new CChildFrame();

	CCreateContext context;
	context.m_pNewViewClass=RUNTIME_CLASS(CSerialSunView);
	if(!m_dataview->LoadFrame(IDI_ICON_DATA,WS_MAXIMIZE|WS_OVERLAPPEDWINDOW,this,&context))
		return;
	m_dataview->ShowWindow(SW_SHOWMAXIMIZED);
	m_dataview->SetWindowText("数据显示界面");
	m_dataview->InitialUpdateFrame(NULL,true);
}

⌨️ 快捷键说明

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