⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_i2cdlg.cpp

📁 SmartARM2200开发平台光盘资料中的所有的源码和测试程序以及应用程序
💻 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 "EasyUSBD13.h"					//包含USB动态库头文件

#define CMD_READ_I2C		0x01		//读命令
#define CMD_WRITE_I2C		0x02		//写命令

/***************************************************

            读 I2C  器件函数
  
****************************************************/
void CUSB_I2CDlg::OnRead_I2C() 
{
	unsigned char tmp;

	//(1) 发送读命令
	tmp = CMD_READ_I2C;
	int ret = WriteDataD13(1, &tmp, 1, 1000);
	if (ret != 1)
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}
	
	//(2) 接收读任务的响应
	ret = ReadDataD13(0, &tmp, 1, 1000);
	if ((ret != 1)||(tmp != 0x01))
	{
		MessageBox("设备读任务没有响应!");
		return;
	}	
	
	unsigned char recbuff[258];		//接收缓冲区
	
	//(3) 接收读到的数据, 接收到的第1.2字节为实际读取到的字节数.
	ret = ReadDataD13(2, recbuff, 256 + 2, 2000);
	if (ret != (256 + 2))
	{
		MessageBox("接收数据失败!");
		return;
	}	

	//(4) 将接收到的数据显示出来
	m_Edit.SetData(&recbuff[2], 256);	
	
	//(5) 在提示框中提示接收到的字节数
	unsigned short actlen = (recbuff[0] << 8) + recbuff[1];	     //计算实际收到的数据

	CString strLen;
	strLen.Format("%d",actlen);

	m_DispResult = "读I2C器件正确,一共读到" + strLen + "个字节";
	UpdateData(FALSE);
}



/***************************************************

            写 I2C  器件函数
  
****************************************************/
void CUSB_I2CDlg::OnWrite_I2C() 
{
	unsigned char tmp;
	
	//(1) 发送写命令
	tmp = CMD_WRITE_I2C;
	int ret = WriteDataD13(1, &tmp, 1, 1000);
	if (ret != 1)
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}
	
	//(2) 接收写任务的响应
	ret = ReadDataD13(0, &tmp, 1, 1000);
	if ((ret != 1)||(tmp != 0x02))
	{
		MessageBox("设备写任务没有响应!");
		return;
	}	

	unsigned char sendbuff[256];
	m_Edit.GetData(sendbuff,256);		//以得编辑框中输入的数据
		
	//(3) 发送要写入的数据
	ret = WriteDataD13(3, sendbuff, 256, 2000);
	if (ret != 256)
	{
		MessageBox("发送数据失败!");
		return;
	}	

	//(4) 接收写入的字节数
	unsigned char Recbuff[2];
	ret = ReadDataD13(0, Recbuff, 2, 1000);
	if (ret != 2)
	{	
		MessageBox("通信错误!");
	}
	else
	{
		int actlen = Recbuff[0] * 256 + Recbuff[1];
		
		CString strLen;
		strLen.Format("%d",actlen);
		m_DispResult = "写入数据成功,共写入" + strLen + "个字节";
	}
	
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -