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

📄 usb_i2cdlg.cpp

📁 easyarm2200整个光盘内容
💻 CPP
字号:
// USB_I2CDlg.cpp : implementation file
//

#include "stdafx.h"
#include "USB_I2C.h"
#include "USB_I2CDlg.h"
#include "EasyUSB.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_RecDisp = _T("");
	m_SendData = _T("");
	m_addr = 0;
	m_len = 0;
	m_WC02Addr = _T("");
	m_ResultDisp = _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);
	//{{AFX_DATA_MAP(CUSB_I2CDlg)
	DDX_Control(pDX, IDC_WC02ADDR, c_WC02Addr);
	DDX_Text(pDX, IDC_REC, m_RecDisp);
	DDX_Text(pDX, IDC_SEND, m_SendData);
	DDX_Text(pDX, IDC_ADDR, m_addr);
	DDX_Text(pDX, IDC_LEN, m_len);
	DDX_Text(pDX, IDC_WC02ADDR, m_WC02Addr);
	DDX_Text(pDX, IDC_RESULTDISP, m_ResultDisp);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUSB_I2CDlg, CDialog)
	//{{AFX_MSG_MAP(CUSB_I2CDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_SEND, On_Change_Send)
	ON_BN_CLICKED(IDC_READ24WC02, OnRead_CAT24WC02)
	ON_BN_CLICKED(IDC_WRITE24WC02, OnWrite_CAT24WC02)
	//}}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_WC02Addr = "0xA0";
	c_WC02Addr.EnableWindow(FALSE);
	
	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;
}


//////////////下面为一些数据类型转换函数/////////////////////////////////////
/***********************************************************
** 函数名称: CString IntToASCII(unsigned char input)
** 功能描述: 将两位的无符号整数转换为字符串
** 输   入: unsigned char input : 16进制数
** 输    出: input  的字符串形式  

** 例    如: 0xFF - > "FF" 
************************************************************/
CString IntToASCII(unsigned char input)
{
	char temp[2];
	char exchange;
	CString output = "";		
	itoa(input,temp,16);		//结果高位放于temp[0],低位放于temp[1]
	
	if (temp[1] == 0)	        //因为如果input为0x0F 时,结果为 'F','0';	
	{						    //所以要交换一下temp[1]与temp[0]
		exchange = temp[1];
		temp[1]  = temp[0];
		temp[0]  = exchange;
	}	
	
	if (temp[0] == 0)  output += "0";
	   else	           output += temp[0];
	   
	if (temp[1] == 0)  output += "0";
	   else            output += temp[1];
	   
	   return output;
}

/***********************************************************
** 函数名称: void StrToHex(CString str,__int16 len,unsigned char *databuff)
** 功能描述: 将一串字符串转换为一串两位的16进制数
** 输   入: CString   str              : 输入的字符串
__int16   outlen           : 输出的字节的长度

  ** 输    出: unsigned char  *databuff   : 输出的16进制数串
  
	** 例    如: "FF" - > 0xFF ; "1236" - > 12 36
	** 备    注:len <= 256
************************************************************/
void StrToHex(CString str,__int16 outlen,unsigned char *databuff)
{
	__int16 temp;
	for (__int16 i = 0; i < outlen; i++)
	{
		CString StrChar = str.Mid(2 * i,2);			//取出其中的两个字符
		sscanf(StrChar,"%x",&temp);			//转换为16进制数
		databuff[i] = (unsigned char)temp;
	}
}


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

	    发送数据框输入数据处理

****************************************/
void CUSB_I2CDlg::On_Change_Send() 
{
	char tmp;								
    __int16 TxtLen = 0;
	
	UpdateData(TRUE);						//取得数据
	
	TxtLen = m_SendData.GetLength();
	
	if (TxtLen == 0) return;
	
	if (TxtLen > 256 * 3)							//编辑框输入字节不允许超过167字节
	{
		m_SendData.Delete(TxtLen -1);
		AfxMessageBox("CAT24WC02存储空间只有256个字节");
	}
	else
	{
		tmp = m_SendData.GetAt(TxtLen - 1);
		if ((tmp > 'A' - 1) && (tmp < 'F' + 1)||
			(tmp > 'a' - 1) && (tmp < 'f' + 1)||
			(tmp > '0' - 1) && (tmp < '9' + 1))
		{									//只许输入0 ~ 9 ;A(a) ~ F(f)
			m_SendData.MakeUpper();       //转为大字
		}
		else
		{
			m_SendData.Delete(TxtLen - 1);    //否则不接收
		}
	}
	
	if (((TxtLen + 1 ) % 3) == 0)	         	//如入空格
	{
		m_SendData += " ";
	}
	
	UpdateData(FALSE);
	UpdateData(TRUE);
	((CEdit *)GetDlgItem(IDC_SEND))->SetSel(m_SendData.GetLength(),
		m_SendData.GetLength(),TRUE);	
}


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

	    定义读写I2C命令结构体

****************************************/
typedef struct tagCmd
{
	unsigned char Cmd;
	unsigned char Addr;
	unsigned char Eaddr;
	unsigned short Len;
}I2C_COMMAND;

#define ADDRESS_24WC02		0xA0		//EASYARM2200开发板上的CAT24WC02地址
#define CMD_READ_24WC02		0x01		//读命令
#define CMD_WRITE_24WC02	0x02		//写命令


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

			读CAT24WC02函数

****************************************/
void CUSB_I2CDlg::OnRead_CAT24WC02() 
{
	I2C_COMMAND  ReadI2C;
	
	UpdateData(TRUE);
	if (m_addr > 255)
	{
		MessageBox("CAT24WC02存储地址空间0 至 255!");
		return;
	}

	if ((m_addr + m_len) > 256)
	{
		MessageBox("读操作超出范围!");
		return;
	}

	if (m_len == 0)
	{
		MessageBox("不能读0个字节!");
		return;
	}

	ReadI2C.Cmd = CMD_READ_24WC02;
	ReadI2C.Addr = ADDRESS_24WC02;
	ReadI2C.Eaddr = m_addr;
	ReadI2C.Len = m_len;

	int ret;
	unsigned char ack;

	//(1) 发送读命令
	ret = WriteData(1,(unsigned char *)&ReadI2C,sizeof(I2C_COMMAND),1000);
	if (ret != sizeof(I2C_COMMAND))
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}
	
	//(2) 接收读任务的响应
	ret = ReadData(0,&ack,1,1000);
	if ((ret != 1)||(ack != 0x01))
	{
		MessageBox("设备读任务没有响应!");
		return;
	}	

	unsigned char recbuff[258];		//接收缓冲区

	//(3) 接收读到的数据,接收到的第1.2字节为实际读取到的字节数.
	ret = ReadData(2,recbuff,ReadI2C.Len + 2,2000);
	if (ret != (ReadI2C.Len + 2))
	{
		MessageBox("接收数据失败!");
		return;
	}

	unsigned short actlen = recbuff[0] * 256 + recbuff[1];	
											//计算实际收到的数据
	m_RecDisp = "";
	for (int i = 0; i < actlen; i++)
	{
		m_RecDisp += IntToASCII(recbuff[i + 2]) + " "; 
	}
	
	CString strLen;
	strLen.Format("%d",actlen);

	m_ResultDisp = "读取数据成功,共读取到" + strLen + "个字节";
	
	UpdateData(FALSE);
}


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

	    写CAT24WC02函数

****************************************/
void CUSB_I2CDlg::OnWrite_CAT24WC02() 
{
	I2C_COMMAND  WriteI2C;
	unsigned char sendbuff[258];				 //发送缓冲区
	
	UpdateData(TRUE);
	if (m_addr > 255)
	{
		MessageBox("CAT24WC02存储地址空间0 至 255");
		return;
	}
	
	if ((m_addr + m_len) > 256)
	{
		MessageBox("写操作超出存储地址空间");
		return;
	}

	int len = m_SendData.GetLength();
	for(WORD i = 0; i < len; i = i + 3)			 //写编辑框的三个字符对应卡的一个字节		
	{
		CString temp = m_SendData.Mid(i, 2);	 //两个字符两个字符地取
		StrToHex(temp,1,&sendbuff[i / 3]);		 //转换为16进制数
	}
	
	WriteI2C.Cmd = CMD_WRITE_24WC02;
	WriteI2C.Addr = ADDRESS_24WC02;
	WriteI2C.Eaddr = m_addr;
	WriteI2C.Len = len / 3;

	if (WriteI2C.Len == 0)
	{
		MessageBox("不能写0个字节");
		return;
	}
	
	int ret;
	unsigned char ack;
	
	//(1) 发送写命令
	ret = WriteData(1,(unsigned char *)&WriteI2C,sizeof(I2C_COMMAND),1000);
	if (ret != sizeof(I2C_COMMAND))
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}
	
	//(2) 接收写任务的响应
	ret = ReadData(0,&ack,1,1000);
	if ((ret != 1)||(ack != 0x02))
	{
		MessageBox("设备写任务没有响应!");
		return;
	}	
		
	//(3) 发送要写入的数据
	ret = WriteData(3,sendbuff,WriteI2C.Len,2000);
	if (ret != (WriteI2C.Len))
	{
		MessageBox("发送数据失败!");
		return;
	}
	
	//(4) 接收写入的字节数
	unsigned char Recbuff[2];
	ret = ReadData(0,Recbuff,2,1000);
	if (ret != 2)
	{	
		MessageBox("通信错误!");
	}
	else
	{
		int actlen = Recbuff[0] * 256 + Recbuff[1];
		
		CString strLen;
		strLen.Format("%d",actlen);
		m_ResultDisp = "写入数据成功,共写入" + strLen + "个字节";
	}

	UpdateData(FALSE);
}

⌨️ 快捷键说明

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