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

📄 usb_rw_fm24cl04dlg.cpp

📁 SmartARM2400系列开发板全套资料
💻 CPP
字号:
// USB_RW_FM24CL04Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "USB_RW_FM24CL04.h"
#include "USB_RW_FM24CL04Dlg.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_RW_FM24CL04Dlg dialog

CUSB_RW_FM24CL04Dlg::CUSB_RW_FM24CL04Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUSB_RW_FM24CL04Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUSB_RW_FM24CL04Dlg)
	m_FM24CL04_Addr = _T("");
	m_ReadLength = 0;
	m_StartAddr = 0;
	m_RecDisp = _T("");
	m_ResultDisp = _T("");
	m_SendData = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUSB_RW_FM24CL04Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUSB_RW_FM24CL04Dlg)
	DDX_Control(pDX, IDC_FM24CL04_ADDR, c_FM24CL04_Addr);
	DDX_Text(pDX, IDC_FM24CL04_ADDR, m_FM24CL04_Addr);
	DDX_Text(pDX, IDC_READ_LENGTH, m_ReadLength);
	DDX_Text(pDX, IDC_START_ADDR, m_StartAddr);
	DDX_Text(pDX, IDC_REC_DISP, m_RecDisp);
	DDX_Text(pDX, IDC_RESULT_DISP, m_ResultDisp);
	DDX_Text(pDX, IDC_SEND_DATA, m_SendData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUSB_RW_FM24CL04Dlg, CDialog)
	//{{AFX_MSG_MAP(CUSB_RW_FM24CL04Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BN_READ_FM24CL04, OnBnReadFm24cl04)
	ON_BN_CLICKED(IDC_BN_WRITE_FM24CL04, OnBnWriteFm24cl04)
	ON_EN_CHANGE(IDC_SEND_DATA, OnChangeSendData)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUSB_RW_FM24CL04Dlg message handlers
//初始化对话框
BOOL CUSB_RW_FM24CL04Dlg::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_FM24CL04_Addr ="0xA4";
	m_ReadLength = 512;
	c_FM24CL04_Addr.EnableWindow(FALSE);
	
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUSB_RW_FM24CL04Dlg::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_RW_FM24CL04Dlg::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_RW_FM24CL04Dlg::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_RW_FM24CL04Dlg::OnChangeSendData() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	char tmp;								
	__int16 TxtLen = 0;
	
	UpdateData(TRUE);						//取得数据
	
	TxtLen = m_SendData.GetLength();		//获取写入数据文本框的字符串长度
	
	if (TxtLen == 0) return;				//不能写入0字节
	
	if (TxtLen > 512 * 3)					//编辑框输入字节不允许超过512字节
	{
		m_SendData.Delete(TxtLen -1);
		AfxMessageBox("CAT24C04存储空间只有512个字节");
	}
	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_DATA))->SetSel(m_SendData.GetLength(),
		m_SendData.GetLength(),TRUE);	
	
}
/****************************************

  定义读写I2C器件FM24CL04命令结构体
  
****************************************/
typedef struct tagCmd
{
	unsigned char Cmd;			//R/W
	unsigned char Addr;			//器件地址
	short		  Eaddr;		//扩展地址
	unsigned short Len;			//读字节数
}I2C_COMMAND;

#define ADDRESS_24WC02		0xA4		//CAT24C04基地址
#define CMD_READ_24WC02		0x01		//读命令
#define CMD_WRITE_24WC02	0x02		//写命令

#include "EasyUSB2400.h"

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

  读 CAT24C04 函数
  
****************************************/
void CUSB_RW_FM24CL04Dlg::OnBnReadFm24cl04() 
{
	// TODO: Add your control notification handler code here
	I2C_COMMAND  ReadI2C;
	
	UpdateData(TRUE);
	if (m_StartAddr > 511)
	{
		MessageBox("CAT24C04 存储地址空间0 至 511!");
		return;
	}
	
	if ((m_StartAddr + m_ReadLength) > 512)
	{
		MessageBox("读操作超出范围!");
		return;
	}
	
	if (m_ReadLength == 0)
	{
		MessageBox("不能读0个字节!");
		return;
	}
	
	ReadI2C.Cmd   = CMD_READ_24WC02;	// 读写命令字
	ReadI2C.Addr  = ADDRESS_24WC02;		// 器件地址
	ReadI2C.Eaddr = m_StartAddr;		// 字节地址
	ReadI2C.Len   = m_ReadLength;		// 读取字节数
	
	int ret;
	unsigned char ack;
	
	//(1) 发送读命令
	ret = LPC2400_WriteData(1,(unsigned char *)&ReadI2C,sizeof(I2C_COMMAND),1000);
	if (ret != sizeof(I2C_COMMAND))
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}

	//(2) 接收读任务的响应
	ret = LPC2400_ReadData(0,&ack,1,2000);
	if ((ret != 1)||(ack != 0x01))
	{
		MessageBox("设备读任务没有响应!");
		return;
	}	

	unsigned char recbuff[514];		//接收缓冲区
	
	//(3) 接收读到的数据,接收到的第1.2字节为实际读取到的字节数.
	ret = LPC2400_ReadData(2,recbuff,ReadI2C.Len + 2,2000);
	if (ret != (ReadI2C.Len + 2))
	{
		MessageBox("接收数据失败!");
		return;
	}
	
	unsigned short actlen = recbuff[0] * 256 + recbuff[1];	
	if (actlen != m_ReadLength) {
		actlen = m_ReadLength;
	}
	
	//计算实际收到的数据
	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);
}
/**************************************************
**			写按扭触发事件
**************************************************/
void CUSB_RW_FM24CL04Dlg::OnBnWriteFm24cl04() 
{
	// TODO: Add your control notification handler code here
	I2C_COMMAND  WriteI2C;
	unsigned char sendbuff[514];				 //发送缓冲区
	short SendLen=0;
	
	UpdateData(TRUE);
	SendLen=m_SendData.GetLength()/3;
	if (m_StartAddr > 511)
	{
		MessageBox("CAT24C04存储地址空间0 至 511");
		return;
	}
	
	if ((m_StartAddr + SendLen) > 512)
	{
		MessageBox("写操作超出存储地址空间");
		return;
	}
	//changed int len 
	unsigned short 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_StartAddr;
	WriteI2C.Len   = len / 3;
	
	if (WriteI2C.Len == 0)
	{
		MessageBox("不能写0个字节");
		return;
	}
	
	int ret;
	unsigned char ack;
	
	//(1) 发送写命令
	ret = LPC2400_WriteData(1,(unsigned char *)&WriteI2C,sizeof(I2C_COMMAND),1000);
	if (ret != sizeof(I2C_COMMAND))
	{
		MessageBox("通信错误,请检查设备端及USB连接是否良好!");
		return;
	}

	//(2) 接收写任务的响应
	ret = LPC2400_ReadData(0,&ack,1,2000);
	if ((ret != 1)||(ack != 0x02))
	{
		MessageBox("设备写任务没有响应!");
		return;
	}	


	//(3) 发送要写入的数据
	ret = LPC2400_WriteData(3,sendbuff,WriteI2C.Len,2000);
	if (ret != (WriteI2C.Len))
	{
		MessageBox("发送数据失败!");
		return;
	}
	
	//(4) 接收写入的字节数
	unsigned char Recbuff[2];
	ret = LPC2400_ReadData(0,Recbuff,2,2000);
	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 + -