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

📄 parportmodedlg.cpp

📁 可以用于各种并口方式(SPP,EPP,ECP)的读写测试。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ParPortModeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ParPortMode.h"
#include "ParPortModeDlg.h"

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

typedef VOID (CALLBACK* LPFNDLLFUNC1)(INT,INT);
typedef UINT (CALLBACK* LPFNDLLFUNC2)(INT); 

LPFNDLLFUNC1 oup32; // Function pointer 
LPFNDLLFUNC2 inp32; // Function pointer 

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

/////////////////////////////////////////////////////////////////////////////
// CParPortModeDlg dialog

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

void CParPortModeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CParPortModeDlg)
	DDX_Control(pDX, IDC_STATIC_READECP, m_Static_EcpRead);
	DDX_Control(pDX, IDC_EDIT_ECP, m_Edit_EcpWrite);
	DDX_Control(pDX, IDC_STATIC_READEPP, m_Static_EppRead);
	DDX_Control(pDX, IDC_EDIT_EPP, m_Edit_EppWrite);
	DDX_Control(pDX, IDC_STATIC_READSPP, m_Static_SppRead);
	DDX_Control(pDX, IDC_EDIT_SPP, m_Edit_SppWrite);
	DDX_Control(pDX, IDC_STATIC_STATUS7, m_Static_Status7);
	DDX_Control(pDX, IDC_STATIC_STATUS6, m_Static_Status6);
	DDX_Control(pDX, IDC_STATIC_STATUS5, m_Static_Status5);
	DDX_Control(pDX, IDC_STATIC_STATUS4, m_Static_Status4);
	DDX_Control(pDX, IDC_STATIC_STATUS3, m_Static_Status3);
	DDX_Control(pDX, IDC_STATIC_STATUS2, m_Static_Status2);
	DDX_Control(pDX, IDC_STATIC_STATUS1, m_Static_Status1);
	DDX_Control(pDX, IDC_STATIC_STATUS0, m_Static_Status0);
	DDX_Control(pDX, IDC_STATIC_CTRL5, m_Static_Ctrl5);
	DDX_Control(pDX, IDC_STATIC_CTRL4, m_Static_Ctrl4);
	DDX_Control(pDX, IDC_STATIC_CTRL3, m_Static_Ctrl3);
	DDX_Control(pDX, IDC_STATIC_CTRL2, m_Static_Ctrl2);
	DDX_Control(pDX, IDC_STATIC_CTRL1, m_Static_Ctrl1);
	DDX_Control(pDX, IDC_STATIC_CTRL0, m_Static_Ctrl0);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CParPortModeDlg, CDialog)
	//{{AFX_MSG_MAP(CParPortModeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN_LIB, OnButtonOpenLib)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE_LIB, OnButtonCloseLib)
	ON_BN_CLICKED(IDC_RADIO_SPP, OnRadioSpp)
	ON_BN_CLICKED(IDC_RADIO_EPP, OnRadioEpp)
	ON_BN_CLICKED(IDC_RADIO_ECP, OnRadioEcp)
	ON_BN_CLICKED(IDC_BUTTON_CTRLPORT, OnButtonCtrlport)
	ON_BN_CLICKED(IDC_BUTTON_STATUSPORT, OnButtonStatusport)
	ON_BN_CLICKED(IDC_BUTTON_SPPWRITE, OnButtonSppwrite)
	ON_BN_CLICKED(IDC_BUTTON_NIBBLEREAD, OnButtonNibbleRead)
	ON_BN_CLICKED(IDC_BUTTON_BYTEREAD, OnButtonByteRead)
	ON_BN_CLICKED(IDC_BUTTON_EPPWRITE, OnButtonEppWrite)
	ON_BN_CLICKED(IDC_BUTTON_EPPREAD, OnButtonEppRead)
	ON_BN_CLICKED(IDC_BUTTON_ECPWRITE, OnButtonEcpWrite)
	ON_BN_CLICKED(IDC_BUTTON_ECPREAD, OnButtonEcpRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CParPortModeDlg message handlers

BOOL CParPortModeDlg::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

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

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

///////////////////////////////////////
//
//    Load/unload inpout32.dll
//
//////////////////////////////////////
void CParPortModeDlg::OnButtonOpenLib() 
{
	hLib = LoadLibrary("inpout32.dll");

     if (hLib == NULL) {
          MessageBox("LoadLibrary Failed.\n");
     }
	 	      
	 inp32 = (LPFNDLLFUNC2) GetProcAddress(hLib, "Inp32");

     if (inp32 == NULL) {
          MessageBox("GetProcAddress for Inp32 Failed.\n");
     }

	 oup32 = (LPFNDLLFUNC1) GetProcAddress(hLib, "Out32");

     if (oup32 == NULL) {
          MessageBox("GetProcAddress for Oup32 Failed.\n");
     }

	EnableBttn(TRUE);
}

void CParPortModeDlg::OnButtonCloseLib() 
{
	FreeLibrary(hLib);

	if (((CButton *)GetDlgItem(IDC_RADIO_SPP))->GetCheck()) 
        ((CButton *)GetDlgItem(IDC_RADIO_SPP))->SetCheck(FALSE);
    if (((CButton *)GetDlgItem(IDC_RADIO_EPP))->GetCheck()) 
        ((CButton *)GetDlgItem(IDC_RADIO_EPP))->SetCheck(FALSE);
	if (((CButton *)GetDlgItem(IDC_RADIO_ECP))->GetCheck()) 
        ((CButton *)GetDlgItem(IDC_RADIO_ECP))->SetCheck(FALSE);

	EnableBttn(FALSE);
	EnableModeReadWriteBttn(NOT_SELECT);
}

///////////////////////////////////////
//
//    SPP Mode 
//
//////////////////////////////////////

void CParPortModeDlg::OnRadioSpp() 
{
	short x;

	//  Change ECR Content to SPP   
	 x=0x00;  
     (oup32)(ECR_ADDR, x);

	 //  Initialize Control Port for SPP
	 //  nSelect_In,  Init,  nAutoFeed,  nStrobe
	 x=0x04;
     (oup32)(CTRL_ADDR,x);

	 EnableModeReadWriteBttn(NOT_SELECT);
	 EnableModeReadWriteBttn(SPP_MODE);
}

void CParPortModeDlg::OnButtonSppwrite() 
{
	CString    str;
    char Buffer[40];
	BYTE   bData;
	short  x;

	Set_SppCompatibleWriteMode();

	m_Edit_SppWrite.GetWindowText(str);  

	if (str == "") 
		str = "0";

	wsprintf(Buffer, "%s", str);
    bData = (byte)String_To_Hexa(Buffer);

	if ((bData < 0) || (bData > 255)) {
        AfxMessageBox(" Please input value as a range of 0 ~ 0xFF.");
		return;
	}	

	Sleep(2);

	if (!CheckStatusBusyBit()) {
		AfxMessageBox(" Busy Bit.....On !!!");
	    return;
	 }

     (oup32)(BASE_ADDR,bData);

	 x=0x09;         // 1001
     (oup32)(CTRL_ADDR,x);

	 Sleep(1);
	 
	 x=0x08;         //  1000 
     (oup32)(CTRL_ADDR,x);
}


void CParPortModeDlg::OnButtonNibbleRead() 
{
	short x;
    BYTE  bData, bLow, bHigh, bi, bj;
	// C1   1->0

	 Set_SppNibbleReadMode();

	 x=0x2A;         // 0010 1010
     (oup32)(CTRL_ADDR,x);

	 Sleep(1);

	 if (!CheckByteMode_PtrClkBit()) {
		 AfxMessageBox(" PtrClkBit.....On !!!");
	    return;
	 }
     Sleep(2);
	 x = (inp32)(Status_ADDR);
     Sleep(1);
 
	 bi = (BYTE)x;
     bi = bi >> 3;
	 bj = bi & 0x10;
	 bj = bj >> 1;
	 bj = ~bj;
	 bj &= 0x08;
	 bi &= 0x07;

	 bLow = bi + bj;

	 TRACE( "Status_ADDR........inp32:%x  bLow:%x \n", x, bLow);

	 x=0x28;         // 0010 1011
     (oup32)(CTRL_ADDR,x);
	
     /////////////////////
	 Sleep(10);

	 x=0x2A;         // 0010 1010
     (oup32)(CTRL_ADDR,x);

	 Sleep(1);

	 if (!CheckByteMode_PtrClkBit()) {
		 AfxMessageBox(" PtrClkBit.....On !!!");
	    return;
	 }
     Sleep(2);
	 x = (inp32)(Status_ADDR);
     Sleep(1);
  
	 bi = (BYTE)x;
     bi = bi >> 3;
	 bj = bi & 0x10;
	 bj = bj >> 1;
	 bj = ~bj;
	 bj &= 0x08;
	 bi &= 0x07;

	 bHigh = bi + bj;
     bData = bHigh << 4; 
     bData += bLow;
                                  
	 TRACE( "Status_ADDR........inp32:%x  bHigh:%x  bData:%x \n", x, bHigh, bData);

	 CString  str;
	 str.Format("%02X", bData);
	 m_Static_SppRead.SetWindowText(str);
     	
	 x=0x28;         // 0010 1011
     (oup32)(CTRL_ADDR,x);
}

void CParPortModeDlg::OnButtonByteRead() 
{
	short x;

	Set_SppByteReadMode();

	// C1   1->0
	 x=0x2A;         // 0010 1011
     (oup32)(CTRL_ADDR,x);

	 Sleep(1);

	 if (!CheckByteMode_PtrClkBit()) {
		 AfxMessageBox(" PtrClkBit.....On !!!");
	    return;
	 }

	 x = (inp32)(BASE_ADDR);
     Sleep(1);
 
	 TRACE( "Read on the ByteMode........%x    \n", x);
     
	 CString  str;
	 str.Format("%02X", x);
	 m_Static_SppRead.SetWindowText(str);
     	
	 x=0x28;         // 0010 1011
     (oup32)(CTRL_ADDR,x);
}

///////////////////////////////////////
//
//    EPP Mode 
//
//////////////////////////////////////

void CParPortModeDlg::OnRadioEpp() 
{
	short x;

	 //  Change ECR Content to PS2 Mode
	 x=0x20;  
     (oup32)(ECR_ADDR, x);

     //  Initialize Control Port for EPP
	 //  nSelect_In,  Init,  nAutoFeed,  nStrobe
	 x=0x04;              
     (oup32)(CTRL_ADDR, x);

	 // Change ECR Content to EPP  
	 x=0x80; 
     (oup32)(ECR_ADDR, x);

	 EnableModeReadWriteBttn(NOT_SELECT);
	 EnableModeReadWriteBttn(EPP_MODE);
}


void CParPortModeDlg::OnButtonEppWrite() 
{
	BYTE bData;
    char Buffer[40];
	CString    str;
		
    m_Edit_EppWrite.GetWindowText(str);   
	wsprintf(Buffer, "%s", str);
    bData = (byte)String_To_Hexa(Buffer);

	if ((bData < 0) || (bData > 255)) {
        AfxMessageBox(" Please input value as a range of 0 ~ 0xFF.");
		return;
	}	
	TRACE( "bData........%x    \n", bData);

	if (!CheckStatusBusyBit()) {
		AfxMessageBox(" Busy Bit.....On !!!");
	}

	// Write to Epp Address ..... 0x00
	if (!EppAddrWriteByteToPort(0)) {
        AfxMessageBox(" Failed to Write on EppAddr !!!");	
        return;
	}

	Sleep(15);
	if (!EppDataWriteByteToPort(bData)) {
        AfxMessageBox(" Failed to Write on EppData !!!");	
        return;
	}

	if(!CheckStatusTimeOutBit()) {
		AfxMessageBox(" TimeOutBit.....On !!!");	
        return;
	}
}

void CParPortModeDlg::OnButtonEppRead() 

⌨️ 快捷键说明

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