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

📄 filetransferdlg.cpp

📁 上位机与下位机的USB通讯
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FileTransferDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileTransfer.h"
#include "FileTransferDlg.h"
#include "SiUSBXp.h"
#include "dbt.h"
#include "initguid.h"

DEFINE_GUID(GUID_DEVINTERFACE_USBXPRESS, 0x3C5E1462L, 0x5695, 0x4E18, \
			0x87, 0x6B, 0xF3, 0xF3, 0xD0, 0x8A, 0xAF, 0x18);

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

/////////////////////////////////////////////////////////////////////////////
// CFileTransferDlg dialog

CFileTransferDlg::CFileTransferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileTransferDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileTransferDlg)
	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 CFileTransferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileTransferDlg)
	DDX_Text(pDX, IDC_TX_FILE_NAME, m_sTXFileName);
	DDX_Text(pDX, IDC_RX_FILE_NAME, m_sRXFileName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileTransferDlg, CDialog)
	//{{AFX_MSG_MAP(CFileTransferDlg)
	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)
	ON_WM_DEVICECHANGE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileTransferDlg message handlers

BOOL CFileTransferDlg::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
	
	// Register for device notification
	RegisterNotification();

	// Init. member variables
	m_hUSBDevice = INVALID_HANDLE_VALUE;

	// Get devices, init. combo box with their names
	FillDeviceList();

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

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

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

	BeginWaitCursor();

	// Make sure that the device list is valid
	if (pDevList)
	{
		// Open selected device and transfer the file if the selection is valid
		if (pDevList->GetCurSel() >= 0)
		{
			SI_STATUS status = SI_Open(pDevList->GetCurSel(), &m_hUSBDevice);

			if (status == SI_SUCCESS)
			{
				// Write file to device in MAX_PACKET_SIZE_WRITE-byte chunks.
				WriteFileData();

				// Close device.
				SI_Close(m_hUSBDevice);
				m_hUSBDevice = INVALID_HANDLE_VALUE;
			}
		}
	}

	EndWaitCursor();
}

void CFileTransferDlg::RegisterNotification()
{
	// Register device notification
	DEV_BROADCAST_DEVICEINTERFACE devIF = {0};

	devIF.dbcc_size = sizeof(devIF);    
	devIF.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;    
	devIF.dbcc_classguid  = GUID_DEVINTERFACE_USBXPRESS;    

	m_hNotifyDevNode = RegisterDeviceNotification(GetSafeHwnd(), &devIF, DEVICE_NOTIFY_WINDOW_HANDLE);
}

void CFileTransferDlg::UnregisterNotification()
{
	// Unegister device notification
	if(NULL != m_hNotifyDevNode)
	{
		UnregisterDeviceNotification(m_hNotifyDevNode);

		m_hNotifyDevNode = NULL;
	}
}

BOOL CFileTransferDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
	switch(nEventType)
	{
	case DBT_DEVICEARRIVAL:        
		// A device has been inserted and is now available.
	
	case DBT_DEVICEREMOVECOMPLETE:        
		// Device has been removed.        
		// Confirm this notify derives from the target device        
		if (DBT_DEVTYP_DEVICEINTERFACE != ((DEV_BROADCAST_HDR *)dwData)->dbch_devicetype)           
			break;        

		if (GUID_DEVINTERFACE_USBXPRESS != ((DEV_BROADCAST_DEVICEINTERFACE *)dwData)->dbcc_classguid)
			break;
		
		if (DBT_DEVICEARRIVAL == nEventType)            
		{
			FillDeviceList();
		}
		else
		{
			SI_Close(m_hUSBDevice);
			m_hUSBDevice = INVALID_HANDLE_VALUE;
			FillDeviceList();
		}
		break;    
	
	default:        
		break;    
	}    
	
	return TRUE;
}

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

	BeginWaitCursor();

	// Make sure that the device list is valid
	if (pDevList)
	{
		// Open selected device and read the file if the selection is valid
		if (pDevList->GetCurSel() >= 0)
		{
			SI_STATUS status = SI_Open(pDevList->GetCurSel(), &m_hUSBDevice);

			if (status == SI_SUCCESS)
			{
				// Read file data in MAX_PACKET_SIZE_READ-byte chunks and write to temp file.
				// Compare temp. file with original
				ReadFileData();

				// Close device.
				SI_Close(m_hUSBDevice);
				m_hUSBDevice = INVALID_HANDLE_VALUE;
			}
		}
	}

	EndWaitCursor();	
}

void CFileTransferDlg::OnBrowseTxFile() 
{
	CFileDialog fileDlg(TRUE);
	
	// Browse for the TX File
	if (fileDlg.DoModal() == IDOK)
	{
		m_sTXFileName = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CFileTransferDlg::OnBrowseRxFile() 
{
	CFileDialog fileDlg(TRUE);
	
	// Browse for the RX File
	if (fileDlg.DoModal() == IDOK)
	{
		m_sRXFileName = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CFileTransferDlg::OnOK() 
{
	// Close handle if open

⌨️ 快捷键说明

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