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

📄 ifsusbdlg.cpp

📁 D12和单片机通讯的电脑部分通讯程序.用于测试是否连接通畅.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//This projiect file need DDK.
//Assume your operation is win2000,and you installed NTDDK.
//Assume your NTDDK is installed at E:\, 
//and your VC++6.0 is installed at E:\Program Files\

//Before compilie this priject,some necessary set is needed:
//1. declare DDK header file directory:
//		Tools--->options...--->add dirctories: E:\NTDDK\INC
//2. add setupapi.lib
//		select "source files" item in left navigation bar, right click ,
//		select "Add Files to Folder...",find out setupapi.lib at
//		E:\NTDDK\lib\i386\free,click OK
//3. add WINMM.LIB
//		select "source files" item in left navigation bar, right click ,
//		select "Add Files to Folder...",find out WINMM.LIB at
//		E:\Program Files\Microsoft Visual Studio\VC98\Lib,click OK


// IfsUsbDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IfsUsb.h"
#include "IfsUsbDlg.h"
#include "SramDlg.h"
#include "DeviceOp.h"
#include <mmsystem.h>

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

volatile	BYTE	m_SwitchStatus;

CHAR SWITCH_PIPE[32] = "PIPE00";
CHAR LED_PIPE[32] = "PIPE01";
CHAR SRAM_PIPE_IN[32] = "PIPE02";
CHAR SRAM_PIPE_OUT[32] = "PIPE03";

/////////////////////////////////////////////////////////////////////////////
//this thread is used to implement read switch of Interface Studio USB11 Develop Board
UINT ThreadReadSwitch(LPVOID param)
{
	
	HANDLE hSwitch = INVALID_HANDLE_VALUE;
	CHAR *pinBuf = NULL;
	ULONG nBytesRead;
	BOOL success;

	hSwitch = open_file( SWITCH_PIPE);
	pinBuf = (PCHAR)malloc(1);

	if ((hSwitch == INVALID_HANDLE_VALUE) || (pinBuf == NULL))
	{
		if (hSwitch != INVALID_HANDLE_VALUE)
		{
			CloseHandle(hSwitch);	
		}
		if (pinBuf != NULL)
		{
			free(pinBuf);
		}
		return 0;
	}

	while(1)
	{
		success = ReadFile(hSwitch,
					pinBuf,
					1,
					&nBytesRead,
					NULL); 
			m_SwitchStatus = *pinBuf;
			::PostMessage((HWND)param,WM_SWITCH_CHANGE,0,0);
			Sleep(200);
	}
		
	free(pinBuf);
	CloseHandle(hSwitch);
	return 0;

}

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

/////////////////////////////////////////////////////////////////////////////
// CIfsUsbDlg dialog

CIfsUsbDlg::CIfsUsbDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIfsUsbDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIfsUsbDlg)
	m_Led1 = FALSE;
	m_Led2 = FALSE;
	m_Led3 = FALSE;
	m_Led4 = FALSE;
	m_Led5 = FALSE;
	m_Led6 = FALSE;
	m_Led7 = FALSE;
	m_Led8 = FALSE;
	m_StaticLoopTestResult = _T("");
	m_EditLoopTimes = 0;
	m_FirmWareVersion = _T("");
	m_EditSn1 = _T("");
	m_EditSn2 = _T("");
	m_EditSn3 = _T("");
	m_EditSn4 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIfsUsbDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIfsUsbDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDC_SCROLLBAR_SRAM, m_ScrollBarSram);
	DDX_Control(pDX, IDC_CHECK_SW8, m_Sw8);
	DDX_Control(pDX, IDC_CHECK_SW7, m_Sw7);
	DDX_Control(pDX, IDC_CHECK_SW6, m_Sw6);
	DDX_Control(pDX, IDC_CHECK_SW5, m_Sw5);
	DDX_Control(pDX, IDC_CHECK_SW4, m_Sw4);
	DDX_Control(pDX, IDC_CHECK_SW3, m_Sw3);
	DDX_Control(pDX, IDC_CHECK_SW2, m_Sw2);
	DDX_Control(pDX, IDC_CHECK_SW1, m_Sw1);
	DDX_Control(pDX, IDC_LIST_SRAM, m_ListSram);
	DDX_Check(pDX, IDC_CHECK_LED1, m_Led1);
	DDX_Check(pDX, IDC_CHECK_LED2, m_Led2);
	DDX_Check(pDX, IDC_CHECK_LED3, m_Led3);
	DDX_Check(pDX, IDC_CHECK_LED4, m_Led4);
	DDX_Check(pDX, IDC_CHECK_LED5, m_Led5);
	DDX_Check(pDX, IDC_CHECK_LED6, m_Led6);
	DDX_Check(pDX, IDC_CHECK_LED7, m_Led7);
	DDX_Check(pDX, IDC_CHECK_LED8, m_Led8);
	DDX_Text(pDX, IDC_STATIC_LOOP_TEST_RESULT, m_StaticLoopTestResult);
	DDX_Text(pDX, IDC_EDIT_LOOP_TIMES, m_EditLoopTimes);
	DDX_Text(pDX, IDC_STATIC_FIRMWARE_VERSION, m_FirmWareVersion);
	DDX_Text(pDX, IDC_EDIT_SN1, m_EditSn1);
	DDX_Text(pDX, IDC_EDIT_SN2, m_EditSn2);
	DDX_Text(pDX, IDC_EDIT_SN3, m_EditSn3);
	DDX_Text(pDX, IDC_EDIT_SN4, m_EditSn4);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIfsUsbDlg, CDialog)
	//{{AFX_MSG_MAP(CIfsUsbDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
	ON_BN_CLICKED(IDC_CHECK_LED1, OnCheckLed1)
	ON_BN_CLICKED(IDC_CHECK_LED2, OnCheckLed2)
	ON_BN_CLICKED(IDC_CHECK_LED3, OnCheckLed3)
	ON_BN_CLICKED(IDC_CHECK_LED4, OnCheckLed4)
	ON_BN_CLICKED(IDC_CHECK_LED5, OnCheckLed5)
	ON_BN_CLICKED(IDC_CHECK_LED6, OnCheckLed6)
	ON_BN_CLICKED(IDC_CHECK_LED7, OnCheckLed7)
	ON_BN_CLICKED(IDC_CHECK_LED8, OnCheckLed8)
	ON_WM_VSCROLL()
	ON_LBN_DBLCLK(IDC_LIST_SRAM, OnDblclkListSram)
	ON_BN_CLICKED(IDC_BUTTON_LOOP_TEST, OnButtonLoopTest)
	ON_BN_CLICKED(IDC_BUTTON_FIRMWARE_VERSION, OnButtonFirmwareVersion)
	ON_BN_CLICKED(IDC_BUTTON_SET_SERIAL_NUMBER, OnButtonSetSerialNumber)
	ON_EN_CHANGE(IDC_EDIT_SN1, OnChangeEditSn1)
	ON_EN_CHANGE(IDC_EDIT_SN2, OnChangeEditSn2)
	ON_EN_CHANGE(IDC_EDIT_SN3, OnChangeEditSn3)
	ON_EN_CHANGE(IDC_EDIT_SN4, OnChangeEditSn4)
	ON_EN_CHANGE(IDC_EDIT_LOOP_TIMES, OnChangeEditLoopTimes)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_SWITCH_CHANGE,OnSwChange)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIfsUsbDlg message handlers

BOOL CIfsUsbDlg::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
/////////////////////////////////////////////////////////////
	UINT i,j;
	CString temp;
	CString strTemp,strTemp2;
	BOOL bSuccess;

	// TODO: Add extra initialization here
	//Logo initialize
	m_LogoText = "Interface Studio";
	m_LogoFont.CreateFont(30, 0, 0, 0, FW_BOLD, true, false,0,0,0,0,0,0, "Arial");	//height is 30
	//
	m_LedStatus = 0xff;
	//
	m_FirmWareVersion = "";
	//
	m_ScrollBarSram.SetScrollRange(0,63,false);
	m_ScrollBarSram.SetScrollPos(0,false);
	m_ScrollBarSram.GetScrollInfo(&m_ScrollInfo,SIF_ALL);
	m_ScrollInfo.nPage = 16;	//line number in one page
	m_ScrollBarSram.SetScrollInfo(&m_ScrollInfo,true);
	//
	m_EditLoopTimes = 1000;
	m_StaticLoopTestResult = "Transfer Rate: 0   KB/S";
	//
	m_EditSn1 = "00";
	m_EditSn2 = "00";
	m_EditSn3 = "00";
	m_EditSn4 = "00";
	m_SerialNumber.SetSize(4);
	//
	m_SramLineData.SetSize(8);
	m_SramReadWriteAddress.SetSize(2);
	//SRAM List initialize

	for (i=0;i<64;i++)
	{
		j=i*8;
		temp="";
		temp.Format("%X",j);
		if (temp.GetLength() == 1)
			temp = "000"+temp;
		else if (temp.GetLength() == 2)
			temp = "00"+temp;
		else if (temp.GetLength() == 3)
			temp = "0"+temp;

		temp = temp+"H"+"  "+"FF FF FF FF FF FF FF FF";
		m_ListSram.AddString(temp);
	}
	m_ListTopIndex = 0x0000;
	m_SramPage.SetSize(128);


	HANDLE hDEV = open_dev();
	if (hDEV == INVALID_HANDLE_VALUE)
	{
		MessageBox("Devce has not found!","Error",MB_OK | MB_ICONWARNING);
	}
	else
	{
		CloseHandle(hDEV);

		BYTE bSw = 0x00;

		sDeviceControl.bRequest = NOTIFY_APP_START;
		sDeviceControl.wIndex = 0;
		sDeviceControl.wValue = (unsigned short)(m_LedStatus);
		sDeviceControl.wLength = 1;						//read back switch status,one byte
		sDeviceControl.pBuffer = &bSw;

		bSuccess = device_ctl(&sDeviceControl,true);
		if (!bSuccess)
		{
			MessageBox("Device initialize failed!","Error",MB_OK | MB_ICONWARNING);		
		}
		else
		{
			m_SwitchStatus = bSw;
			SetSwitchStatus();
		}

		//reinitialize ram list content
		sDeviceControl.bRequest = RAM_COMMAND_READ;	//read ram
		sDeviceControl.wIndex   = 0;				//start address
		sDeviceControl.wValue   = 128;				//read length
		sDeviceControl.wLength  = 0;				//read ram is a vendor request without data stage 
		sDeviceControl.pBuffer  = NULL;				//wLength is 0,pBuffer will always NULL
		bSuccess = ReadSram(&sDeviceControl,&m_SramPage[0]);
		if (bSuccess)
		{
			for (i=0;i<16;i++)
			{
				m_ListSram.GetText(i,temp);
				strTemp2 = temp.Mid(0,6);
				for (j=0;j<8;j++)
				{
					strTemp.Format("%X",m_SramPage[i*8+j]);
					if (strTemp.GetLength() == 1)
					{
						strTemp = "0"+strTemp;
					}
					strTemp = " "+strTemp;	//add a space
					strTemp2 += strTemp;	//get data string
				}
					//update List content
				m_ListSram.DeleteString(i);
				m_ListSram.InsertString(i,strTemp2);
			}
			UpdateData(false);
		}
		else
		{
			MessageBox("Initialize RAM list failed!","Error",MB_OK | MB_ICONWARNING);		
		}

		//start read switch status Thread
		HWND hWnd = GetSafeHwnd();
		AfxBeginThread(ThreadReadSwitch,hWnd,THREAD_PRIORITY_NORMAL);
	}

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

void CIfsUsbDlg::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 CIfsUsbDlg::OnPaint() 
{
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);

	CRect rectABOUT;
	GetDlgItem(IDC_BUTTON_ABOUT)->GetWindowRect(rectABOUT);
	ScreenToClient(rectABOUT);

	dc.SetBkMode(TRANSPARENT);

	CRect rectText;
	rectText.left = rect.left+10;
	rectText.top =rectABOUT.top;
	rectText.bottom = rect.bottom;
	rectText.right = rectABOUT.left;

	CFont * OldFont = dc.SelectObject(&m_LogoFont);

	// draw text in DC
	COLORREF OldColor = dc.SetTextColor( ::GetSysColor( COLOR_3DHILIGHT));

	dc.DrawText( m_LogoText, rectText + CPoint(1,1), DT_SINGLELINE | DT_BOTTOM  | DT_LEFT |DT_VCENTER);
	dc.SetTextColor( ::GetSysColor( COLOR_3DSHADOW));
	dc.DrawText( m_LogoText, rectText, DT_SINGLELINE | DT_BOTTOM  | DT_LEFT |DT_VCENTER);

	// restore old text color
	dc.SetTextColor( OldColor);
	// restore old font
	dc.SelectObject(OldFont);


	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 CIfsUsbDlg::OnQueryDragIcon()
{

⌨️ 快捷键说明

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