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

📄 usb320dlg.cpp

📁 USB device的PC机测试程序
💻 CPP
字号:
// usb320Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "usb320.h"
#include "usb320Dlg.h"
#include "SiF32xUSB.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUsb320Dlg dialog

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

void CUsb320Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsb320Dlg)
	DDX_Control(pDX, IDC_OPENDEVICE, m_openbutton);
	DDX_Control(pDX, IDC_COMBO_PKTSIZE, m_pksize);
	DDX_Text(pDX, IDC_STATIC_TIMER, m_timer);
	DDX_Text(pDX, IDC_STATIC_TURNS, m_turns);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUsb320Dlg, CDialog)
//{{AFX_MSG_MAP(CUsb320Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_END, OnEnd)
	ON_CBN_SELCHANGE(IDC_COMBO_PKTSIZE, OnSelchangeComboPktsize)
	ON_BN_CLICKED(IDC_OPENDEVICE, OnOpendevice)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsb320Dlg message handlers

BOOL CUsb320Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// 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
	
	SetTimer(1,100,NULL);
    
	// Initialization the variable;

	m_pksize.SetCurSel (0);
	m_TransPackSize = 4096;
    h_mUsbDevice = INVALID_HANDLE_VALUE;
	m_turns = "";
	m_timer = "";

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

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

void CUsb320Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (1 == nIDEvent)
	{
		UpdateData(FALSE);
	}
	//CDialog::OnTimer(nIDEvent);
}

void CUsb320Dlg::OnStart() 
{  
	// TODO: Add your control notification handler code here
 	BYTE		buf[3];
	DWORD       NumberWritten;
	DWORD       NumberRead;
    MSG message;
	
	m_stopread = FALSE;
	BlockRead  = 0;

	buf[0] = 0;
	buf[1] = (char)(m_TransPackSize & 0x000000FF);
	buf[2] = (char)((m_TransPackSize & 0x0000FF00) >> 8);
    
	m_timer = _T("");
	UpdateData(FALSE);

	BeginWaitCursor();
    
	if ( h_mUsbDevice == INVALID_HANDLE_VALUE)
	{
	   AfxMessageBox (" No Device Detected! Fatal Error",MB_OK);
	   return;
	}

  //	WriteFileData();
	F32x_SetTimeouts(100,100);

	F32x_Write(h_mUsbDevice, buf, 3, &NumberWritten);

	if ( 3 != NumberWritten) 
	{
		AfxMessageBox("Error");
        return;
	}
    //DeviceWrite(buf, 3 , 0, 100);
//	oldtime = mytime.GetCurrentTime ();
    CtOldTime = CTime::GetCurrentTime ();

    while(1)
	{   
        if (::PeekMessage (&message,NULL,0,0,PM_REMOVE))
		{
			::TranslateMessage (&message);
			::DispatchMessage (&message);
		}

		
        if ( TRUE == m_stopread)
		{
			break;
		}
        
		F32x_Read(h_mUsbDevice,ReadBuffer,4096,&NumberRead);
		if ((UINT)m_TransPackSize == NumberRead) 
		{
			BlockRead ++;
		}
        
		//BlockRead = NumberRead;
		m_turns.Format ("%d",BlockRead);
		UpdateData(FALSE);

	}

	//DeviceRead(ReadBuffer,4096,0,100);
	

	EndWaitCursor();	
	
}

void CUsb320Dlg::OnEnd() 
{
	// TODO: Add your control notification handler code here
	CString temp;
	long    lnTime;
    UINT  dlspeed;

	m_stopread = TRUE;
//	spantime = mytime.GetCurrentTime () - oldtime;
    CtNewTime = CTime::GetCurrentTime ();
    CSpanTime = CtNewTime - CtOldTime;
	lnTime = CSpanTime.GetTotalSeconds ();
	temp.Format ("%d",lnTime);
	AfxMessageBox(temp);


	if ( lnTime > 0)
	{
        if ( 0 == BlockRead)
		{
			AfxMessageBox("Zero Package Has Red");
			return;
		}
	dlspeed = (UINT)(BlockRead * m_TransPackSize / lnTime) >> 7;
    }
	else
	{
	dlspeed = 0;
	}
    
	temp.Format ("%d Kbps",dlspeed);
	
	m_timer = temp;
	UpdateData(FALSE);
}



void CUsb320Dlg::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);
	}
}



void CUsb320Dlg::OnOK() 
{
	// TODO: Add extra validation here
	F32x_Close(h_mUsbDevice);
	CDialog::OnOK();
}



void CUsb320Dlg::OnSelchangeComboPktsize() 
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("hello");

	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	//m_TransPackSize = ::atoi(m_pksize.GetItemData ()); 
    m_TransPackSize = m_pksize.GetCurSel();
    m_TransPackSize = (UINT)4096>>(m_TransPackSize);
	
	if ( 8 > m_TransPackSize)
	{   
		CString Err;
		Err.Format (" Package Size %d is invalid",m_TransPackSize);
		AfxMessageBox(Err);
	}

	//CString temp;
	//temp.Format ("the value is %d",m_TransPackSize);
	//AfxMessageBox(temp);
}

void CUsb320Dlg::OnOpendevice() 
{
	// TODO: Add your control notification handler code here
	CComboBox*	pDevList	= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	BeginWaitCursor();

	if (pDevList)
	{
		// Open selected device.
		if (pDevList->GetCurSel() >= 0)
		{
			F32x_STATUS status = F32x_Open(pDevList->GetCurSel(), &h_mUsbDevice);

			// Open selected file.
			if (status != F32x_SUCCESS)
			{
				h_mUsbDevice = INVALID_HANDLE_VALUE;
			}
			else
			{
				m_openbutton.EnableWindow (FALSE);
			}
		}
	}

	EndWaitCursor();
}

⌨️ 快捷键说明

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