📄 11view.cpp
字号:
// 11View.cpp : implementation of the CMy11View class
//
#include "stdafx.h"
#include "11.h"
#include "11Doc.h"
#include "11View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CTime cLastColtTime;
/////////////////////////////////////////////////////////////////////////////
// CMy11View
IMPLEMENT_DYNCREATE(CMy11View, CView)
BEGIN_MESSAGE_MAP(CMy11View, CView)
//{{AFX_MSG_MAP(CMy11View)
// 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)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy11View construction/destruction
CMy11View::CMy11View()
{
// TODO: add construction code here
for(int i=0;i<1000;i++)
m_RecBuff[i] = 0;
}
CMy11View::~CMy11View()
{
}
BOOL CMy11View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy11View drawing
void CMy11View::OnDraw(CDC* pDC)
{
CMy11Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString szVal[20],str;
for(int i=0;i<20;i++)
{
str.Format("第%.2d点:",i+1);
szVal[i].Format("%.2f",fVal[i]);
pDC->TextOut(20,100+i*15,str);
pDC->TextOut(100,100+i*15,szVal[i]);
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMy11View printing
BOOL CMy11View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy11View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy11View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy11View diagnostics
#ifdef _DEBUG
void CMy11View::AssertValid() const
{
CView::AssertValid();
}
void CMy11View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy11Doc* CMy11View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy11Doc)));
return (CMy11Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy11View message handlers
void CMy11View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
nColtAddr = 0 ;
nColts = 0;
if(m_Ports.InitPort(this,1,4800,'N',8,1,EV_RXCHAR|EV_RXFLAG,1024))
{
this->m_Ports.StartMonitoring();
SetCommVal();
}
unsigned int nDummy;
m_pThread=(HANDLE) _beginthreadex(NULL,0,CommThread,this,CREATE_SUSPENDED,&nDummy);//开辟DI线程
ResumeThread(m_pThread);
}
UINT CMy11View::CommThread(LPVOID pParam)
{
CMy11View *pView = (CMy11View *)pParam;
while(1)
{
CTime cNowTime = CTime::GetCurrentTime();
if(cNowTime.GetMinute()*60 + cNowTime.GetSecond() -
cLastColtTime.GetMinute()*60 - cLastColtTime.GetSecond() > 1)
pView->SetCommVal();
Sleep(100);
}
}
void CMy11View::SetCommVal()
{
int HAddr,LAddr,m_Xnh;
int nHAdd,nLAdd;
nHAdd = ExchangeAscII((nColtAddr>>4)&0x0f);
nLAdd = ExchangeAscII(nColtAddr&0x0f);
m_Xnh = nHAdd^nLAdd^0x52^0x44;
HAddr = ExchangeAscII((m_Xnh>>4)&0x0f);
LAddr = ExchangeAscII(m_Xnh&0x0f);
BYTE OutBuff[8] = {0x40,nHAdd,nLAdd,0x52,0x44,HAddr,LAddr,0x0d};
m_Ports.WriteToPort(OutBuff,8);
cLastColtTime = CTime::GetCurrentTime();
nColtAddr++;
if(nColtAddr > 19)//9 define max addr numbers
nColtAddr = 0;
}
LONG CMy11View::OnCommunication(WPARAM ch, LPARAM port)
{
if(port == 1)
{
m_RecBuff[nColts] += (BYTE)(char *)(ch);
nColts++;
if(nColts == 24)
{
DataProcessTemp(m_RecBuff);
nColts = 0;
ResetBuffVal();
SetCommVal();
}
}
return 0;
}
void CMy11View::DataProcessTemp(BYTE m_Inbuff[])
{
int nTempAddr = nColtAddr - 1;
if(nTempAddr < 0)
nTempAddr = 19;
int nHAdd,nLAdd;
nHAdd = ExchangeAscII((nTempAddr>>4)&0x0f);
nLAdd = ExchangeAscII(nTempAddr&0x0f);
if(m_Inbuff[0] == 0x40)
{
if(m_Inbuff[1] == nHAdd && m_Inbuff[2] == nLAdd)
{
if(m_Inbuff[3] == 0x52 && m_Inbuff[4] == 0x44)
{
int nzTemp[5];
float fTemp;
nzTemp[0] = m_Inbuff[7];
nzTemp[1] = m_Inbuff[8];
nzTemp[2] = m_Inbuff[9];
nzTemp[3] = m_Inbuff[10];
for(int i = 0 ; i < 4; i ++)
{
if(nzTemp[i] > 0x39)
nzTemp[i] -= 0x37;
else
nzTemp[i] -= 0x30;
}
fTemp=float(nzTemp[1]+(nzTemp[0]<<4)+(nzTemp[3]<<8)+(nzTemp[2]<<12))/10;
fVal[nTempAddr] = fTemp;
RedrawWindow();
}
}
}
}
void CMy11View::ResetBuffVal()
{
for(int i=0;i<1000;i++)
m_RecBuff[i] = 0;
}
BYTE CMy11View::ExchangeAscII(BYTE bInput)
{
BYTE bRef = 0;
if(bInput > 9)
bRef = bInput+0x37;
else
bRef = bInput+0x30;
return bRef;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -