www.pudn.com > parallelportcontrol.rar > ParallelPortControlDlg.cpp, change:2004-11-29,size:11250b


// ParallelPortControlDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ParallelPortControl.h" 
#include "ParallelPortControlDlg.h" 
 
#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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CParallelPortControlDlg dialog 
 
CParallelPortControlDlg::CParallelPortControlDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CParallelPortControlDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CParallelPortControlDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CParallelPortControlDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CParallelPortControlDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CParallelPortControlDlg, CDialog) 
	//{{AFX_MSG_MAP(CParallelPortControlDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_WRITEDATAPORT, OnButtonWritedataport) 
	ON_BN_CLICKED(IDC_BUTTON_WRITECONTORLPORT, OnButtonWritecontorlport) 
	ON_BN_CLICKED(IDC_BUTTON_READSTATEPORT, OnButtonReadstateport) 
	ON_BN_CLICKED(IDC_BUTTON_READCONTROLPORT, OnButtonReadcontrolport) 
	ON_BN_CLICKED(IDC_BUTTON_WRITEDATA, OnButtonWritedata) 
	ON_BN_CLICKED(IDC_BUTTON_READDATA, OnButtonReaddata) 
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CParallelPortControlDlg message handlers 
 
BOOL CParallelPortControlDlg::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 
	// 初始化I/O并口 
	if(!InitializeWinIo()) 
	{ 
		MessageBox("Init fail!"); 
	} 
	//设置I/O口address 
	SetDlgItemText(IDC_EDIT1,"378"); 
	SetDlgItemText(IDC_EDIT3,"37A"); 
	SetDlgItemText(IDC_EDIT5,"379"); 
	SetDlgItemText(IDC_EDIT7,"37A"); 
	SetDlgItemText(IDC_EDIT9,"378"); 
	SetDlgItemText(IDC_EDIT10,"37A"); 
	SetDlgItemText(IDC_EDIT12,"379"); 
	SetDlgItemText(IDC_EDIT13,"37A"); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CParallelPortControlDlg::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 CParallelPortControlDlg::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 CParallelPortControlDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
BOOL CParallelPortControlDlg::DestroyWindow()  
{ 
	ShutdownWinIo(); 
	return CDialog::DestroyWindow(); 
} 
 
void CParallelPortControlDlg::OnButtonWritedataport()  
{ 
	char s[10]; 
    unsigned int  WriValue=0; 
 
    GetDlgItem(IDC_EDIT1)->GetWindowText(s, 10); 
    sscanf(s, "%x", &WriValue); 
    WORD  m_nport=(WORD)WriValue; 
	 
	GetDlgItem(IDC_EDIT2)->GetWindowText(s, 10); 
    sscanf(s, "%d", &WriValue); 
	DWORD   m_nValue=(DWORD)WriValue; 
	 
	SetPortVal(m_nport, m_nValue, 1);//write a BYTE value to an I/O port 
} 
 
void CParallelPortControlDlg::OnButtonWritecontorlport()  
{ 
	char s[10]; 
    unsigned int  WriValue=0; 
	 
    GetDlgItem(IDC_EDIT3)->GetWindowText(s, 10); 
    sscanf(s, "%x", &WriValue); 
    WORD   m_nport=(WORD)WriValue;  //获取端口地址 
	 
	DWORD  temp_dwPortVal; 
	unsigned int    temp_aa; 
	GetPortVal(m_nport, &temp_dwPortVal, 1);  //reads a BYTE value from an I/O port 
	temp_aa=(unsigned int)temp_dwPortVal; 
	temp_aa=temp_aa&0x0F0;  //取低8位值,将低4位置为0;高4位不变; 
	temp_aa=temp_aa^0x0B;   //将低4位中C0、C1、C3置为1,C2置为0;高4位不变; 
	 
	GetDlgItem(IDC_EDIT4)->GetWindowText(s, 10); 
    sscanf(s, "%d", &WriValue);//获取要写入端口的值 
	WriValue=WriValue&0x0F; //取低4位; 
	temp_aa=temp_aa^WriValue; //将写入值的低4位中的C0、C1、C3取反,C2位不变,高4位保持端口值不变 
	 
	SetPortVal(m_nport, (DWORD)temp_aa, 1); //写出的值中,高4位保持端口原来的值不变, 
	                                        //低4位是写入什么电平,连接器上既是什么电平 
} 
 
void CParallelPortControlDlg::OnButtonReadstateport()  
{ 
	char ss[10]; 
	DWORD            dwPortVal; 
    unsigned int     ValueGet=0; 
    GetDlgItem(IDC_EDIT5)->GetWindowText(ss, 10); 
    sscanf(ss, "%x", &ValueGet); 
    WORD   m_nport=(WORD)ValueGet; 
 
	GetPortVal(m_nport, &dwPortVal, 1); 
 
	ValueGet=dwPortVal; 
	 
	ValueGet=ValueGet^0x80; //保持得到的State值与连接器处的值一直; 
	ValueGet=ValueGet&0xF8; //去掉S0 ~S2位; 
	ValueGet=ValueGet>>3;   //右移3位,将S7~S3变为低5位 
	SetDlgItemInt(IDC_EDIT6,ValueGet,true); 
} 
 
void CParallelPortControlDlg::OnButtonReadcontrolport()  
{ 
	char ss[10]; 
    unsigned int  ValueGet=0; 
 
    GetDlgItem(IDC_EDIT7)->GetWindowText(ss, 10); 
    sscanf(ss, "%x", &ValueGet); 
    WORD   m_nport=(WORD)ValueGet; 
 
	//========== 将C0~C3位置1,即使连接器上是高电平 ,使控制端口为输入端口===== 
	DWORD           temp_dwPortVal; 
	unsigned int    temp_aa; 
	GetPortVal(m_nport, &temp_dwPortVal, 1);  //获取端口的当前值 
	temp_aa=(unsigned int)temp_dwPortVal; 
	temp_aa=temp_aa&0x0F0;  //取低8位值,将低4位置为0;高4位不变; 
	temp_aa=temp_aa^0x4;   //将低4位中C0、C1、C3置为0,C2置为1;高4位不变; 
	SetPortVal(m_nport, (DWORD)temp_aa, 1); //写出的值中,高4位保持端口原来的值不变, 
	                                        //低4位是写高电平,即使连接器上是高电平 
	//============================================================= 
	DWORD     dwPortVal; 
	GetPortVal(m_nport, &dwPortVal, 1); 
	ValueGet=dwPortVal;																								 
	 
	ValueGet=ValueGet^0x0B; //保持C0,C1,C3位的值与连接器处的值一至; 
	ValueGet=ValueGet&0x0F; //去掉高4位值 
	SetDlgItemInt(IDC_EDIT8,ValueGet,true); 
} 
 
void CParallelPortControlDlg::OnButtonWritedata()  
{ 
	char s[10]; 
    unsigned int  WriValue=0; 
	 
    GetDlgItem(IDC_EDIT9)->GetWindowText(s, 10); 
    sscanf(s, "%x", &WriValue); 
    WORD  m_nportData=(WORD)WriValue; 
	GetDlgItem(IDC_EDIT10)->GetWindowText(s, 10); 
    sscanf(s, "%x", &WriValue); 
    WORD  m_nportControl=(WORD)WriValue; 
	 
	GetDlgItem(IDC_EDIT11)->GetWindowText(s, 10); 
    sscanf(s, "%d", &WriValue); 
 
	DWORD   m_nValue=(DWORD)(WriValue&0x0FF);//取低8位值 
	SetPortVal(m_nportData, m_nValue, 1);//write a BYTE value to Data port 
 
	DWORD  temp_dwPortVal; 
	unsigned int    temp_aa; 
	GetPortVal(m_nportControl, &temp_dwPortVal, 1);  //reads a BYTE value from an I/O port 
	temp_aa=(unsigned int)temp_dwPortVal; 
	temp_aa=temp_aa&0x0F0;  //取低8位值,将低4位置为0;高4位不变; 
	temp_aa=temp_aa^0x0B;   //将低4位中C0、C1、C3置为1,C2置为0;高4位不变; 
 
	WriValue=WriValue&0x0F00; 
	WriValue=WriValue>>8; 
	temp_aa=temp_aa^WriValue; //将写入值的低4位中的C0、C1、C3取反,C2位不变,高4位保持端口值不变 
	SetPortVal(m_nportControl, (DWORD)temp_aa, 1); //写出的值中,高4位保持端口原来的值不变, 
	                                               //低4位是写入什么电平,连接器上既是什么电平 
} 
 
void CParallelPortControlDlg::OnButtonReaddata()  
{ 
	char ss[10]; 
    unsigned int     ValueGet=0; 
    GetDlgItem(IDC_EDIT12)->GetWindowText(ss, 10); 
    sscanf(ss, "%x", &ValueGet); 
    WORD   m_nportState=(WORD)ValueGet; 
 
	GetDlgItem(IDC_EDIT13)->GetWindowText(ss, 10); 
    sscanf(ss, "%x", &ValueGet); 
    WORD   m_nportControl=(WORD)ValueGet; 
	//Read State Port 
	DWORD            dwPortVal; 
	unsigned int     ValueState=0; 
	GetPortVal(m_nportState, &dwPortVal, 1); 
	ValueState=dwPortVal; 
	ValueState=ValueState^0x80; //保持得到的State值与连接器处的值一直; 
	ValueState=ValueState&0xF8; //去掉S0 ~S2位; 
	ValueState=ValueState<<1;   //左移1位,将S7~S3变为高5位 
	//Read control Port 
	//========== 将C0~C3位置1,即使连接器上是高电平 ,使控制端口为输入端口===== 
 
	GetPortVal(m_nportControl, &dwPortVal, 1);  //获取端口的当前值 
	ValueGet=(unsigned int)dwPortVal; 
	ValueGet=ValueGet&0x0F0;  //取低8位值,将低4位置为0;高4位不变; 
	ValueGet=ValueGet^0x4;   //将低4位中C0、C1、C3置为0,C2置为1;高4位不变; 
	SetPortVal(m_nportControl, (DWORD)ValueGet, 1); //写出的值中,高4位保持端口原来的值不变, 
	                                        //低4位是写高电平,即使连接器上是高电平 
	//============================================================= 
	unsigned int     ValueControl=0; 
	GetPortVal(m_nportControl, &dwPortVal, 1); 
	ValueControl=(unsigned int)dwPortVal;																								 
	ValueControl=ValueControl^0x0B; //保持C0,C1,C3位的值与连接器处的值一至; 
	ValueControl=ValueControl&0x0F; //去掉高4位值 
	//get 9bit value 
	ValueGet=ValueState^ValueControl; 
	SetDlgItemInt(IDC_EDIT14,ValueGet,true); 
} 
 
void CParallelPortControlDlg::OnButtonAbout()  
{ 
	CAboutDlg  dlg; 
	dlg.DoModal(); 
} 
 
void CParallelPortControlDlg::OnOK()  
{ 
	 
}