📄 myview.cpp
字号:
// MyView.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "My.h"
#include "MyDoc.h"
#include "MyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CFormView)
BEGIN_MESSAGE_MAP(CMyView, CFormView)
//{{AFX_MSG_MAP(CMyView)
ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication)
ON_COMMAND(ID_SEND_DATA, OnSendData)
ON_COMMAND(ID_STOP_DATA, OnStopData)
ON_WM_TIMER()
ON_UPDATE_COMMAND_UI(ID_SEND_DATA, OnUpdateSendData)
ON_UPDATE_COMMAND_UI(ID_STOP_DATA, OnUpdateStopData)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
#define CR '\r'
#define LF '\n'
char m_chChecksum; //用于COM1的校验和计算
CString m_strRXhhCOM1; //用于存放COM1接收的半BYTE校验字节hh
CString m_strRXDataCOM1; //COM1接收数据
CString m_strRXDataCOM2; //COM2接收数据
UINT m_nRXErrorCOM1; //COM1接收数据错误帧数
UINT m_nRXErrorCOM2; //COM2接收数据错误帧数
UINT m_nRXCounterCOM1; //COM1接收数据错误帧数
UINT m_nRXCounterCOM2; //COM2接收数据错误帧数CString
CSerialPort m_ComPort[2];
CMyView::CMyView()
: CFormView(CMyView::IDD)
{
//{{AFX_DATA_INIT(CMyView)
m_sAnodeVoltage = _T("");
m_sCellVoltage = _T("");
//}}AFX_DATA_INIT
// TODO: add construction code here
m_bConnected=FALSE;
}
CMyView::~CMyView()
{
}
void CMyView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyView)
DDX_Text(pDX, IDC_EDIT_ANODVOLTAGE, m_sAnodeVoltage);
DDX_Text(pDX, IDC_EDIT_CELLVOLTAGE, m_sCellVoltage);
//}}AFX_DATA_MAP
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CMyView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
m_chChecksum=0; //校验和置0
m_nRXErrorCOM1=0; //COM1接收数据错误帧数置0
m_nRXErrorCOM2=0; //COM2接收数据错误帧数置0
m_nRXCounterCOM1=0; //COM1接收数据错误帧数置0
m_nRXCounterCOM2=0; //COM2接收数据错误帧数置0
m_strRXhhCOM1.Empty(); //清空半BYTE校验hh存储变量
for(int i=0; i<2; i++)
{
if(m_ComPort[i].InitPort(this,i+1,9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
{
m_ComPort[i].StartMonitoring(); //启动串口监视线程
}
else
{
CString str;
str.Format("COM%d 没有发现,或被其它设备占用",i+1);
AfxMessageBox(str);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CMyView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CFormView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnSendData()
{
// TODO: Add your command handler code here
m_bConnected=!m_bConnected;
SetTimer(1,1000,NULL);
}
void CMyView::OnStopData()
{
// TODO: Add your command handler code here
m_bConnected=!m_bConnected;
KillTimer(1);
m_sAnodeVoltage="";
m_sCellVoltage="";
UpdateData(FALSE);
}
void CMyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
int m_nCellVoltage=rand()%1000; //产生10以内的槽电压的随机数
float m_fCellVoltage=(float)m_nCellVoltage/10;
CString str_CellVoltage;
str_CellVoltage.Format("%5.2f",m_fCellVoltage);
int m_nAnodeVoltage=rand()%1000; //产生4以内的阳极电压随机数
float m_fAnodeVoltage=(float)m_nAnodeVoltage/10;
CString str_AnodeVoltage;
str_AnodeVoltage.Format("%5.2f",m_fAnodeVoltage);
CTime Time;
Time=CTime::GetCurrentTime();
CString str_Time;
str_Time=Time.Format("%H:%M:%S");
CString str;
str="%"+str_CellVoltage+"%"+str_AnodeVoltage+"%"+str_Time;
SendString(str, 2); //串口2发送数据
CFormView::OnTimer(nIDEvent);
}
void CMyView::SendString(CString &str, int Port)
{
char checksum=0,cr=CR,lf=LF;
char c1,c2;
for(int i=0;i<str.GetLength();i++)
checksum = checksum^str[i];
c2=checksum & 0x0f;
c1=((checksum >> 4) & 0x0f);
if (c1 < 10)
c1+= '0';
else
c1 += 'A' - 10;
if (c2 < 10)
c2+= '0';
else
c2 += 'A' - 10;
CString str1;
str1='$'+str+"*"+c1+c2+cr+lf;
m_ComPort[Port-1].WriteToPort((char*)(LPCTSTR)str1);
}
LONG CMyView::OnCommunication(WPARAM ch,LPARAM Port)
{
static int count1=0,count2=0,count3=0;
static char c1,c2;
static int flag;
CString strCheck="";
if(Port==1) //COM1接收到数据
{
m_strRXDataCOM1 += (char)ch;
switch(ch)
{
case '$':
m_chChecksum=0; //开始计算CheckSum
flag=0;
break;
case '*':
flag=2;
c2=m_chChecksum & 0x0f;
c1=((m_chChecksum >> 4) & 0x0f);
if (c1 < 10) c1+= '0'; else c1 += 'A' - 10;
if (c2 < 10) c2+= '0'; else c2 += 'A' - 10;
break;
case CR:
break;
case LF:
m_strRXDataCOM1.Empty();
break;
default:
if(flag>0)
{
m_strRXhhCOM1 += ch; //得到收到的校验值hh
if(flag==1)
{
strCheck = strCheck+c1+c2; //计算得到的校验值hh
if(strCheck!=m_strRXhhCOM1) //如果校验有错
{
m_strRXDataCOM1.Empty();
m_nRXErrorCOM1++; //串口1错误帧数加1
}
else
{
m_nRXCounterCOM1++;
if(m_strRXDataCOM1.Left(1)=="$") //接收数据的第一个字符是$吗?
{
char tbuf[6];
char *temp=(char*)((LPCTSTR)m_strRXDataCOM1);
tbuf[0]=temp[1];
tbuf[1]=temp[2];
tbuf[2]=temp[3];
tbuf[3]=temp[4];
tbuf[4]=0; //0表示字符串的结束,必要
int data=atoi(tbuf);
//CString strDisplay1,strDisplay2;
//strDisplay1.Format("NO. %06d: The reseived data is %04d",m_nRXCounterCOM1,data);
//strDisplay2.Format("Error Counter=%04d.",m_nRXErrorCOM1);
//CDC* pDC=GetDC(); //准备数据显示
//int nColor=pDC->SetTextColor(RGB(255,255,0));
//pDC->TextOut(10,10,strDisplay1);//显示接收到的数据
//pDC->TextOut(30,30,strDisplay2);//显示错误帧数
//pDC->SetTextColor(nColor);
//ReleaseDC(pDC);
CString strTemp, strTemp1;
int n1 = m_strRXDataCOM1.Find('%');
strTemp = m_strRXDataCOM1.Right(m_strRXDataCOM1.GetLength() - n1 -1);
//析取出槽电压
n1 = strTemp.Find('%');
strTemp1 = strTemp.Right(strTemp.GetLength() - n1 -1);
m_sCellVoltage=strTemp.Left(n1);
//析取出阳极电压
n1 = strTemp1.Find('%');
strTemp = strTemp1.Right(strTemp1.GetLength() - n1 - 1);
m_sAnodeVoltage=strTemp1.Left(n1);
//析取出时间
n1 = strTemp.Find('*');
UpdateData(FALSE);
}
//CString str1="Y";
//m_ComPort[0].WriteToPort((LPCTSTR)str1);//发送应答信号Y
}
m_strRXhhCOM1.Empty();
}
flag--;
}
else
m_chChecksum ^= ch;
break;
}
}
return 0;
}
void CMyView::OnUpdateSendData(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_bConnected);
}
void CMyView::OnUpdateStopData(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_bConnected);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -