📄 usb_i2cdlg.cpp
字号:
// USB_I2CDlg.cpp : implementation file
//
#include "stdafx.h"
#include "USB_I2C.h"
#include "USB_I2CDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CUSB_I2CDlg dialog
CUSB_I2CDlg::CUSB_I2CDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUSB_I2CDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUSB_I2CDlg)
m_I2CAddr = _T("");
m_DispResult = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUSB_I2CDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT, m_Edit);
//{{AFX_DATA_MAP(CUSB_I2CDlg)
DDX_Text(pDX, IDC_I2C_ADDR, m_I2CAddr);
DDX_Text(pDX, IDC_DISPRESULT, m_DispResult);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUSB_I2CDlg, CDialog)
//{{AFX_MSG_MAP(CUSB_I2CDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_READI2C, OnRead_I2C)
ON_BN_CLICKED(IDC_WRITEI2C, OnWrite_I2C)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUSB_I2CDlg message handlers
BOOL CUSB_I2CDlg::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
// TODO: Add extra initialization here
m_I2CAddr = "A0";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CUSB_I2CDlg::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 CUSB_I2CDlg::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 CUSB_I2CDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*********************************************************************************************
下 面 开 始 为 与 USB 相 关 的 程 序
*********************************************************************************************/
#include "EasyUSB2410.h" /* 包含USB动态库头文件 */
#define CMD_READ_CAT1025 0x01 /* 读CAT1025 命令 */
#define CMD_WRITE_CAT1025 0x02 /* 写CAT1025 命令 */
/***************************************************
读 I2C 器件函数
****************************************************/
void CUSB_I2CDlg::OnRead_I2C()
{
unsigned char buff[259]; /* 接收缓冲区 */
int ret;
buff[0] = CMD_READ_CAT1025; /* 读 CAT125 命令 */
buff[1] = 256 / 256; /* 读长度 */
buff[2] = 256 % 256;
//(1) 发送数据
ret = WriteData2410(1, buff, sizeof(buff), 1000);
if (ret != sizeof(buff))
{
MessageBox("发送数据失败!");
return;
}
//(2) 接收数据
ret = ReadData2410(0, buff, sizeof(buff), 2000);
if (ret != sizeof(buff))
{
MessageBox("接收数据失败!");
return;
}
//(3) 将接收到的数据显示出来
m_Edit.SetData(&buff[3], 256);
//(4) 在提示框中显示接收到的字节数
unsigned short actlen = (buff[1] << 8) + buff[2]; //计算实际收到的数据
CString strLen;
strLen.Format("%d",actlen);
m_DispResult = "读I2C器件正确,一共读到" + strLen + "个字节";
UpdateData(FALSE);
}
/***************************************************
写 I2C 器件函数
****************************************************/
void CUSB_I2CDlg::OnWrite_I2C()
{
unsigned char buff[259];
int ret;
buff[0] = CMD_WRITE_CAT1025;
buff[1] = (unsigned char)(256 / 256);
buff[2] = (unsigned char)(256 % 256);
m_Edit.GetData(&buff[3], 256); //得到编辑框中输入的数据
//(1) 发送数据
ret = WriteData2410(1, buff, sizeof(buff), 2000);
if (ret != sizeof(buff))
{
MessageBox("发送数据失败!");
return;
}
//(2) 接收数据
ret = ReadData2410(0, buff, sizeof(buff), 1000);
if (ret != sizeof(buff))
{
MessageBox("通信错误!");
}
else
{
int actlen = buff[1] * 256 + buff[2];
CString strLen;
strLen.Format("%d",actlen);
m_DispResult = "写入数据成功,共写入" + strLen + "个字节";
}
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -