📄 dvpview.cpp
字号:
// dvpView.cpp : implementation of the CDvpView class
//
#include "stdafx.h"
#include "dvp.h"
#include "dvpView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
CDvpView * DvpView;
IMPLEMENT_DYNCREATE(CDvpView, CFormView)
BEGIN_MESSAGE_MAP(CDvpView, CFormView)
//{{AFX_MSG_MAP(CDvpView)
ON_BN_CLICKED(IDC_HEX_RX_CHECK, OnHexRxCheck)
ON_CBN_SELCHANGE(IDC_COM_COMBO, OnSelchangeComCombo)
ON_CBN_SELCHANGE(IDC_RATE_COMBO, OnSelchangeRateCombo)
ON_BN_CLICKED(IDC_INST_TX_CHECK, OnInstTxCheck)
ON_BN_CLICKED(IDC_HEX_TX_CHECK, OnHexTxCheck)
ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CLR_RX, OnClrRx)
ON_BN_CLICKED(IDC_OPEN_LEDDLG, OnOpenLeddlg)
ON_BN_CLICKED(IDC_OPEN_KEYDLG, OnOpenKeydlg)
ON_BN_CLICKED(IDC_OPEN_TIMER, OnOpenTimerdlg)
ON_BN_CLICKED(IDC_OPEN_EPROM, OnOpenEprom)
ON_BN_CLICKED(IDC_OPEN_ETC, OnOpenEtc)
ON_BN_CLICKED(IDC_OPEN_IRDA, OnOpenIrda)
//}}AFX_MSG_MAP
// Standard printing commands
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDvpView construction/destruction
CDvpView::CDvpView()
: CFormView(CDvpView::IDD)
{
//{{AFX_DATA_INIT(CDvpView)
m_HexRx = FALSE;
m_HexTx = FALSE;
m_InstTx = FALSE;
m_StopRx = FALSE;
m_Rate = 115200;
m_ComPort = -1;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CDvpView::~CDvpView()
{
}
void CDvpView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDvpView)
DDX_Check(pDX, IDC_HEX_RX_CHECK, m_HexRx);
DDX_Check(pDX, IDC_HEX_TX_CHECK, m_HexTx);
DDX_Check(pDX, IDC_INST_TX_CHECK, m_InstTx);
DDX_Check(pDX, IDC_STOP_RX_CHECK, m_StopRx);
DDX_CBIndex(pDX, IDC_RATE_COMBO, m_Rate);
DDX_CBIndex(pDX, IDC_COM_COMBO, m_ComPort);
//}}AFX_DATA_MAP
}
BOOL CDvpView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CDvpView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
Txcount = 0;
Rxcount = 0;
ResizeParentToFit();
BeginRead();
CButton * ck = NULL;
ck = (CButton *)this->GetDlgItem(IDC_HEX_RX_CHECK);
ck->SetCheck(1);
m_HexRx = true;
ck = (CButton *)this->GetDlgItem(IDC_HEX_TX_CHECK);
ck->SetCheck(1);
m_HexTx = true;
RxBuffer = new CMemFile;
//建立处理LED的窗体
LedDlg = new CLedDialog();
LedDlg->Create(IDD_LED_DIALOG , this);
//建立处理键盘的窗体
KeyDlg = new CKeyDialog();
KeyDlg->Create(IDD_KEY_DIALOG , this);
//建立处理数码管的窗体
DgDevDlg = new CDgDevDialog();
DgDevDlg->Create(IDD_DIGDEV_DIALOG , this);
//建立处理eprom的窗体
EpDlg = new CEpromDialg();
EpDlg->Create(IDD_EPROM_DIALOG , this);
//建立处理蜂鸣器、中断的窗体
EtcDlg = new CEtcDialog();
EtcDlg->Create(IDD_ETC_DIALOG , this);
//建立处理红外、运放的窗体
IRDADlg = new CIRDADialog();
IRDADlg->Create(IDD_IRDA_DIALOG , this);
//指向view的指针,在以上窗体中通过它调用公共函数比如串口数据发送等
DvpView = this;
}
/////////////////////////////////////////////////////////////////////////////
// CDvpView printing
BOOL CDvpView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDvpView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDvpView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CDvpView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CDvpView diagnostics
#ifdef _DEBUG
void CDvpView::AssertValid() const
{
CFormView::AssertValid();
}
void CDvpView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CDvpDoc* CDvpView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDvpDoc)));
return (CDvpDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDvpView message handlers
//加入字符串到接收Edit中
void CDvpView::AddStrToRxEdit(unsigned char * data , int len)
{
char * t_buf = new char[len + 1];
memcpy(t_buf,data,len);
t_buf[len] = 0;
CString str;
CEdit * edt = (CEdit *)this->GetDlgItem(IDC_DATA_RX);
edt->GetWindowText(str);
if(this->m_HexRx) //如果显示为16进制则转换为16进制显示
{
str = str + ConverBufToHex((unsigned char *)t_buf , len);
}
else//否则直接显示
{
str = str + CString(t_buf);
}
delete t_buf;
edt->SetWindowText(str);
}
//------------------响应接收数据格式改变--------------------------------
void CDvpView::OnHexRxCheck()
{
this->m_HexRx = !this->m_HexRx;
CString str;
CEdit * edt = (CEdit *)this->GetDlgItem(IDC_DATA_RX);
edt->GetWindowText(str);
unsigned char * buf;
int len = 0;
if(this->m_HexRx)//如果显示为16进制则转换为16进制
{
str = ConverBufToHex((unsigned char *)(LPCTSTR)str , str.GetLength());
}
else//否则把16进制数据转换为实际数据
{
buf = new unsigned char[str.GetLength() + 1];
len = ConverHexToBuff(str , buf , str.GetLength());
buf[len] = 0;
str = buf;
delete buf;
}
edt->SetWindowText(str);
}
//------------------响应数据立即发送的改变-------------------------
void CDvpView::OnInstTxCheck()
{
this->m_InstTx = !this->m_InstTx;
}
//---------------------响应串口的改变------------------------------
void CDvpView::OnSelchangeComCombo()
{
// TODO: Add your control notification handler code here
CComboBox * bo =(CComboBox *)this->GetDlgItem(IDC_COM_COMBO);
this->m_ComPort = bo->GetCurSel() + 1;
if(this->m_ComPort <= 0)
return;
OpenComPort();
}
//-----------------------响应波特率改变--------------------------------
void CDvpView::OnSelchangeRateCombo()
{
// TODO: Add your control notification handler code here
CComboBox * bo =(CComboBox *)this->GetDlgItem(IDC_RATE_COMBO);
CString str;
bo->GetWindowText(str);
if(str == "")
return;
this->m_Rate = atoi(str);
OpenComPort();
}
//------------------------打开串口------------------------------------
bool CDvpView::OpenComPort()
{
if(this->m_ComPort <= 0)
return false;
if(!((CDvpApp *)AfxGetApp())->OpenComPort(this->m_ComPort , this->m_Rate))
{
char str[50];
wsprintf(str, "COM%d口不能打开", this->m_ComPort);
MessageBox(str);
return false;
}
return true;
};
//------------------------关闭串口------------------------------------
bool CDvpView::CloseComPort()
{
if(!((CDvpApp *)AfxGetApp())->CloseComPort())
{
return false;
}
return true;
};
//处理接收数据Edit按键的按下事件,该函数在dvp.cpp中的事件过滤函数中调用
void CDvpView::DealRxKeyDown(unsigned char *key)
{
*key = 0; //不管按下什么键都转换成无效键
}
//处理发送数据Edit的按键事件,该函数在dvp.cpp中的事件过滤函数中调用
void CDvpView::DealTxKeyDown(unsigned char *key)
{
static unsigned char WaitSend[3] = {0,0,0};
//如果是16进制发送,则只能按下0-9 a-f,A-F,空格和退格
if(this->m_HexTx)
{
if(*key >='a' && *key <= 'f')//小写转换为大写
*key = *key - 32;
if(!((*key >='0' && *key <= '9') ||
(*key >='A' && *key <= 'F') ||
*key == ' ' ||
*key == 8 ||
*key == 0x16 || *key == 0x18 || *key == 0x03))
{
*key = 0;
}
}
//如果为直接发送
if(*key != 8 && *key != 0 && this->m_InstTx)
{
if(this->m_HexTx)//如果为16进制发送则要等待接收两个字符,然后16进制转换成实际数据发送
{
if(*key != ' ')
{
if(WaitSend[0] == 0)
WaitSend[0] = *key;
else if(WaitSend[1] == 0)
{
unsigned char buf[1];
WaitSend[1] = *key;
WaitSend[3] = 0;
ConverHexToBuff(CString(WaitSend) , buf , 1);
((CDvpApp *)AfxGetApp())->SendData(buf , 1);
WaitSend[0] = 0;
WaitSend[1] = 0;
}
}
}
else//如果不是16进制发送则直接发送
{
WaitSend[0] = 0;
WaitSend[1] = 0;
((CDvpApp *)AfxGetApp())->SendData(key , 1);
}
}
}
//--------------------------发送数据转换成十六进制显示-----------------------
void CDvpView::OnHexTxCheck()
{
// TODO: Add your control notification handler code here
this->m_HexTx = !this->m_HexTx;
CString str;
CEdit * edt = (CEdit *)this->GetDlgItem(IDC_DATA_TX);
edt->GetWindowText(str);
unsigned char * buf;
int len = 0;
if(this->m_HexTx)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -