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

📄 f32x_bulkfiletransferdlg.cpp

📁 C8051F320 SOURCE CODE 内容有: * USB Bulk Driver Example * USB Bulk Firmware Example * Host Ap
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// F32x_BulkFileTransferDlg.cpp : implementation file
//

#include "stdafx.h"
#include "F32x_BulkFileTransfer.h"
#include "F32x_BulkFileTransferDlg.h"
#include "F32x_BulkFileTransferFunctions.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

DEFINE_GUID(GUID_INTERFACE_SILABS_BULK, 
0x37538c66, 0x9584, 0x42d3, 0x96, 0x32, 0xeb, 0xad, 0xa, 0x23, 0xd, 0x13);

#define SILABS_BULK_WRITEPIPE	"PIPE01"
#define SILABS_BULK_READPIPE		"PIPE00"

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CF32x_BulkFileTransferDlg dialog

CF32x_BulkFileTransferDlg::CF32x_BulkFileTransferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CF32x_BulkFileTransferDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CF32x_BulkFileTransferDlg)
	m_sTXFileName = _T("");
	m_sRXFileName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CF32x_BulkFileTransferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CF32x_BulkFileTransferDlg)
	DDX_Text(pDX, IDC_TX_FILE_NAME, m_sTXFileName);
	DDX_Text(pDX, IDC_RX_FILE_NAME, m_sRXFileName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CF32x_BulkFileTransferDlg, CDialog)
	//{{AFX_MSG_MAP(CF32x_BulkFileTransferDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_TRANSFER_DATA, OnTransferData)
	ON_BN_CLICKED(IDC_RECEIVE_DATA, OnReceiveData)
	ON_BN_CLICKED(IDC_BROWSE_TX_FILE, OnBrowseTxFile)
	ON_BN_CLICKED(IDC_BROWSE_RX_FILE, OnBrowseRxFile)
	ON_BN_CLICKED(IDC_UPDATE_DEVICE_LIST, OnUpdateDeviceList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CF32x_BulkFileTransferDlg message handlers

BOOL CF32x_BulkFileTransferDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	CComboBox*	pDevList	= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	// 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
	
	sgCUsbIF.SetGUID(GUID_INTERFACE_SILABS_BULK);

	// Get devices, init. combo box with their names
	FillDeviceList();
	
	if (pDevList)
	{
		// Open selected device.
		if (pDevList->GetCurSel() >= 0)
		{
			F32x_STATUS status = F32x_Open(pDevList->GetCurSel(), &m_hUSBDevice);

			// Open selected file.
			if (status == F32x_SUCCESS)
			{
				// Write file to device in MAX_PACKET_SIZE-byte chunks.
				// Get the write handle
				m_hUSBWrite = sgCUsbIF.OpenUSBfile(SILABS_BULK_WRITEPIPE);

				if (m_hUSBWrite == INVALID_HANDLE_VALUE)
				{
					CString sMessage;
					sMessage.Format("Error opening Write device: %s\n\nApplication is aborting.\nReset hardware and try again.",SILABS_BULK_WRITEPIPE);
					AfxMessageBox(sMessage,MB_OK|MB_ICONEXCLAMATION);
				}

				// Get the read handle
				m_hUSBRead = sgCUsbIF.OpenUSBfile(SILABS_BULK_READPIPE);

				if (m_hUSBRead == INVALID_HANDLE_VALUE)
				{
					CString sMessage;
					sMessage.Format("Error opening Read device: %s\n\nApplication is aborting.\nReset hardware and try again.",SILABS_BULK_READPIPE);
					AfxMessageBox(sMessage,MB_OK|MB_ICONEXCLAMATION);
				}
			}
		}
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CF32x_BulkFileTransferDlg::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 CF32x_BulkFileTransferDlg::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 CF32x_BulkFileTransferDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CF32x_BulkFileTransferDlg::OnTransferData() 
{
	CComboBox*	pDevList	= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	BeginWaitCursor();

	if (m_hUSBDevice != INVALID_HANDLE_VALUE)
	{
		F32x_STATUS status = F32x_SUCCESS;

		// Open selected file.
		if (status == F32x_SUCCESS)
		{
			// Write file to device in MAX_PACKET_SIZE-byte chunks.
			WriteFileData();
		}
	}

	EndWaitCursor();
}

void CF32x_BulkFileTransferDlg::OnReceiveData() 
{
	CComboBox*	pDevList	= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	BeginWaitCursor();

	//if (pDevList)
	if (m_hUSBDevice != INVALID_HANDLE_VALUE)
	{
		F32x_STATUS status = F32x_SUCCESS;

		// Open temporary file based on file name of input file
		if (status == F32x_SUCCESS)
		{
			// Read file data in MAX_PACKET_SIZE-byte chunks and write to temp file.
			// Compare temp. file with original
			ReadFileData();
		}
	}

	EndWaitCursor();	
}

void CF32x_BulkFileTransferDlg::OnBrowseTxFile() 
{
	CFileDialog fileDlg(TRUE);
	
	if (fileDlg.DoModal() == IDOK)
	{
		m_sTXFileName = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CF32x_BulkFileTransferDlg::OnBrowseRxFile() 
{
	CFileDialog fileDlg(TRUE);
	
	if (fileDlg.DoModal() == IDOK)
	{
		m_sRXFileName = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CF32x_BulkFileTransferDlg::OnOK() 
{
	// Close handle if open
	if (m_hUSBDevice != INVALID_HANDLE_VALUE)
	{
		F32x_Close(m_hUSBDevice);
		m_hUSBDevice = INVALID_HANDLE_VALUE;
	}
	
	CDialog::OnOK();
}

void CF32x_BulkFileTransferDlg::OnUpdateDeviceList() 
{
	FillDeviceList();
}


void CF32x_BulkFileTransferDlg::FillDeviceList()
{
	F32x_DEVICE_STRING	devStr;
	DWORD				dwNumDevices	= 0;
	CComboBox*			pDevList		= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	if (pDevList)
	{
		int numDevs = pDevList->GetCount();

		for (int i = 0; i < numDevs; i++)
		{
			pDevList->DeleteString(0);
		}

		F32x_STATUS status = F32x_GetNumDevices(&dwNumDevices);

		if (status == F32x_SUCCESS)
		{
			for (DWORD d = 0; d < dwNumDevices; d++)
			{
				status = F32x_GetProductString(d, devStr, F32x_RETURN_SERIAL_NUMBER);

				if (status == F32x_SUCCESS)
				{
					if (pDevList)				// Fill device list
						pDevList->AddString(devStr);
				}
			}
		}

		pDevList->SetCurSel(0);
	}
}


BOOL CF32x_BulkFileTransferDlg::WriteFileData()
{
	BOOL success = TRUE;

	if (m_sTXFileName.GetLength() > 0)
	{
		CFile file;

		if (file.Open(m_sTXFileName, CFile::modeRead | CFile::shareDenyNone))
		{
			DWORD		size			= file.GetLength();
			DWORD		dwBytesWritten	= 0;
			DWORD		dwBytesRead		= 0;
			BYTE		buf[MAX_PACKET_SIZE_WRITE];

			buf[0] = FT_WRITE_MSG;
			buf[1] = (char)(size & 0x000000FF);
			buf[2] = (char)((size & 0x0000FF00) >> 8);

			// Check size of file (GP)
			if (size > 4096)
			{
				AfxMessageBox("File size is too large.");
				success = FALSE;
			}

			// If file is not too big, send write file size message
			else if (DeviceWrite(buf, FT_MSG_SIZE, &dwBytesWritten))
			{
				if (dwBytesWritten == FT_MSG_SIZE)
				{
					DWORD numPkts		= (size / MAX_PACKET_SIZE_WRITE) + (((size % MAX_PACKET_SIZE_WRITE) > 0)? 1 : 0);
					DWORD numLoops		= (numPkts / MAX_WRITE_PKTS) + (((numPkts % MAX_WRITE_PKTS) > 0)? 1 : 0);
					DWORD counterPkts	= 0;
					DWORD counterLoops	= 0;
					DWORD totalWritten	= 0;

					// Now write data to board in groups of 8 packets
					while (counterLoops < numLoops && success)
					{
						int		i	= 0;

						while (i < MAX_WRITE_PKTS && counterPkts < numPkts && success)
						{
							DWORD dwWriteLength	= 0;

							if ((size - totalWritten) < MAX_PACKET_SIZE_WRITE)
							{
								dwWriteLength = size - totalWritten;
							}
							else
							{
								dwWriteLength = MAX_PACKET_SIZE_WRITE;
							}

							memset(buf, 0, dwWriteLength);
							file.Read(buf, dwWriteLength);
							dwBytesWritten = 0;

                            success = DeviceWrite(buf, dwWriteLength, &dwBytesWritten);
                            totalWritten += dwWriteLength;
							counterPkts++;
							i++;
						}

						if (success)
						{
							memset(buf, 0, MAX_PACKET_SIZE_WRITE);

							// Check for ACK packet after writing 8 pkts.
							while ((buf[0] != 0xFF) && success)
							{
								success = DeviceRead(buf, 1, &dwBytesRead);
							}
						}

						counterLoops++;
					}

					if (!success)
					{
						AfxMessageBox("Target device failure while sending file data.\nCheck file size.");
						success = FALSE;
					}
				}
				else
				{
					AfxMessageBox("Incomplete write file size message sent to device.");
					success = FALSE;
				}
			}
			else
			{
				AfxMessageBox("Target device failure while sending file size information.");
				success = FALSE;
			}

			file.Close();
		}
		else
		{
			CString err;
			err.Format("Failed opening file:\n%s", m_sTXFileName);
			AfxMessageBox(err);
			success = FALSE;
		}
	}

⌨️ 快捷键说明

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