📄 vcusbpcdemodlg.cpp
字号:
// VCUSBPCDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VCUSBPCDemo.h"
#include "VCUSBPCDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include "usb.h"
TUSB *MyUSB;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVCUSBPCDemoDlg dialog
CVCUSBPCDemoDlg::CVCUSBPCDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVCUSBPCDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVCUSBPCDemoDlg)
m_sEditDownData = _T("");
m_sEditUpData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVCUSBPCDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVCUSBPCDemoDlg)
DDX_Text(pDX, IDC_EDITDOWNDATA, m_sEditDownData);
DDX_Text(pDX, IDC_EDITUPDATA, m_sEditUpData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVCUSBPCDemoDlg, CDialog)
//{{AFX_MSG_MAP(CVCUSBPCDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTNCDDATA, OnBtncddata)
ON_BN_CLICKED(IDC_BTNCDUP, OnBtncdup)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVCUSBPCDemoDlg message handlers
BOOL CVCUSBPCDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_sEditDownData="11 22 33 44 56 78 90 12 34 56 01 01 03 04 05 06";
UpdateData(FALSE);
MyUSB = new TUSB;
if (!MyUSB->OpenDevice())
MessageBox("USB设备打开失败,请检查设备连接");
return TRUE; // return TRUE unless you set the focus to a control
}
void CVCUSBPCDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVCUSBPCDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVCUSBPCDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVCUSBPCDemoDlg::OnBtncddata()
{
m_sEditDownData="00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
UpdateData(FALSE);
}
void CVCUSBPCDemoDlg::OnBtncdup()
{
m_sEditUpData="";
UpdateData(FALSE);
}
void CVCUSBPCDemoDlg::OnRadio1()
{
UpdateData(TRUE);
unsigned char buf[512];
unsigned char *str=new unsigned char[3];
int i,j,len;
j=1;
if (m_sEditDownData==""){
return;
}
for(i=0;i<512;i++)
buf[i]=0;
len= m_sEditDownData.GetLength() + 1;
// Edit1->GetTextBuf(str,len) ;
j=1;
for(i=0;i<len;i+=3){
str[0] = m_sEditDownData.GetAt(i);
str[1] = m_sEditDownData.GetAt(i+1);
str[2] = 0;
buf[j] = HexToInt(str);
j++;
}
buf[0]=j-1;
MyUSB->SendCommand(0);
MyUSB->SendCardCommand(buf);
MyUSB->GetCardResponse(buf) ;
m_sEditUpData="";
unsigned char *str1=new unsigned char[3];
str1[2]=0;
for(i=1;i<j;i++){
IntToHex(buf[i],2,str1);
m_sEditUpData+= (CString)str1 + " ";
}
//RichEdit1->Text=RichEdit1->Text+IntToHex(buf[i], 2)+" ";
UpdateData(FALSE);
}
void CVCUSBPCDemoDlg::OnRadio2()
{
UpdateData(TRUE);
unsigned char buf[512];
unsigned char *str=new unsigned char[3];
int i,j,len;
j=1;
if (m_sEditDownData==""){
return;
}
for(i=0;i<512;i++)
buf[i]=0;
len= m_sEditDownData.GetLength() + 1;
// Edit1->GetTextBuf(str,len) ;
j=1;
for(i=0;i<len;i+=3){
str[0] = m_sEditDownData.GetAt(i);
str[1] = m_sEditDownData.GetAt(i+1);
str[2] = 0;
buf[j] = HexToInt(str);
j++;
}
buf[0]=j-1;
MyUSB->SendCommand(1);
MyUSB->SendCardCommand(buf);
MyUSB->GetCardResponse(buf) ;
m_sEditUpData="";
unsigned char *str1=new unsigned char[3];
str1[2]=0;
for(i=1;i<j;i++){
IntToHex(buf[i],2,str1);
m_sEditUpData+= (CString)str1 + " ";
}
//RichEdit1->Text=RichEdit1->Text+IntToHex(buf[i], 2)+" ";
UpdateData(FALSE);
}
void CVCUSBPCDemoDlg::OnRadio3()
{
UpdateData(TRUE);
unsigned char buf[512];
unsigned char *str=new unsigned char[3];
int i,j,len;
j=1;
if (m_sEditDownData==""){
return;
}
for(i=0;i<512;i++)
buf[i]=0;
len= m_sEditDownData.GetLength() + 1;
// Edit1->GetTextBuf(str,len) ;
j=1;
for(i=0;i<len;i+=3){
str[0] = m_sEditDownData.GetAt(i);
str[1] = m_sEditDownData.GetAt(i+1);
str[2] = 0;
buf[j] = HexToInt(str);
j++;
}
buf[0]=j-1;
MyUSB->SendCommand(2);
MyUSB->SendCardCommand(buf);
MyUSB->GetCardResponse(buf) ;
m_sEditUpData="";
unsigned char *str1=new unsigned char[3];
str1[2]=0;
for(i=1;i<j;i++){
IntToHex(buf[i],2,str1);
m_sEditUpData+= (CString)str1 + " ";
}
//RichEdit1->Text=RichEdit1->Text+IntToHex(buf[i], 2)+" ";
UpdateData(FALSE);
}
void CVCUSBPCDemoDlg::OnRadio4()
{
UpdateData(TRUE);
unsigned char buf[512];
unsigned char *str=new unsigned char[3];
int i,j,len;
j=1;
if (m_sEditDownData==""){
return;
}
for(i=0;i<512;i++)
buf[i]=0;
len= m_sEditDownData.GetLength() + 1;
// Edit1->GetTextBuf(str,len) ;
j=1;
for(i=0;i<len;i+=3){
str[0] = m_sEditDownData.GetAt(i);
str[1] = m_sEditDownData.GetAt(i+1);
str[2] = 0;
buf[j] = HexToInt(str);
j++;
}
buf[0]=j-1;
MyUSB->SendCommand(3);
MyUSB->SendCardCommand(buf);
MyUSB->GetCardResponse(buf) ;
m_sEditUpData="";
unsigned char *str1=new unsigned char[3];
str1[2]=0;
for(i=1;i<j;i++){
IntToHex(buf[i],2,str1);
m_sEditUpData+= (CString)str1 + " ";
}
//RichEdit1->Text=RichEdit1->Text+IntToHex(buf[i], 2)+" ";
UpdateData(FALSE);
}
int CVCUSBPCDemoDlg::HexToInt(unsigned char *buf)
{
int lng=0;
int lngQuan=1;
char c;
for(int i=strlen((const char *)buf)-1;i>=0;i--){
c = buf[i];
if (c>=48 && c<=57){
c = c -48;
}else
{
if (c>='A' && c<='F')
{
c = 10 + (c-'A');
}else
{
if (c>='a' && c<='f')
{
c = 10 + (c-'a');
}
}
}
lng += lngQuan * c;
lngQuan = lngQuan * 16;
}
return lng;
}
void CVCUSBPCDemoDlg::IntToHex(int lng,int lngChar,unsigned char *str)
{
int iTemp=0;
int ii=24;
for(int i=lngChar-1;i>=0;i--){
if (lng == 0){
str[i]='0';
}else{
iTemp = lng % 16;
if (iTemp >= 10)
str[i] = 'A' + iTemp - 10;
else
str[i] = 48 + iTemp;
if (lng == 0)
break;
lng = lng / 16;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -