📄 mainfrm.cpp
字号:
// MainFrm.cpp : CMainFrame 类的实现
//
#include "stdafx.h"
#include "SerialData.h"
#include "MainFrm.h"
#include "SerialDataDoc.h"
#include "SerialDataView.h"
#include ".\mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//接受数据消息
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_WM_CREATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // 状态行指示器
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
// CMainFrame 构造/析构
CMainFrame::CMainFrame()
{
// TODO: 在此添加成员初始化代码
i = 0;
}
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("未能创建工具栏\n");
return -1; // 未能创建
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("未能创建状态栏\n");
return -1; // 未能创建
}
// TODO: 如果不需要工具栏可停靠,则删除这三行
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
AfxBeginThread(ConTrolSendL, NULL);//启动线程
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return TRUE;
}
// CMainFrame 诊断
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CMDIFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CMDIFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame 消息处理程序
LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
CMDIChildWnd *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame();
CSerialDataView *pView = (CSerialDataView*)pChild->GetActiveView();
if(port == pView->ports)
{
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,8,0x1021);
if(nGetCrc == (mybuff[8]<<8|mybuff[9]))
{
CStringChang::Bytes2String(mybuff,10,tempBuf); //字节数据转换为可打印字符串
nGetCtring.Format("%s",tempBuf);
pView->PostMessage(WM_GETSTR_FROMFRM,(WPARAM)&nGetCtring,0); //发送消息数据
//清空缓冲区内容
i=0;
}
}
if(i == 16)
{
unsigned short nGetCr = CCRC::RunCRC16(mybuff,14,0x1021);
if(nGetCr == (mybuff[14]<<8|mybuff[15]))
{
CStringChang::Bytes2String(mybuff,16,tempBuf); //字节数据转换为可打印字符串
nGetCtring.Format("%s",tempBuf);
//CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
//CMDIChildWnd *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame();
//CSerialDataView *pView = (CSerialDataView*)pChild->GetActiveView();
pView->PostMessage(WM_GETSTR_FROMFRM,(WPARAM)&nGetCtring,0); //发送消息数据
}
i=0;
}
}
return 0;
}
UINT CMainFrame::ConTrolSendL(LPVOID lPvoid)
{
CSerialControl nSerialn;
while(1)
{
nSerialn.SetCommVal(1);
Sleep(1000);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -