📄 parallel.cpp
字号:
// parallel.cpp : implementation file
//
#include "stdafx.h"
#include "ParallelPortApp.h"
#include "parallel.h"
#include "Dlportio.h"
#include "ParallelPin.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// parallel dialog
parallel::parallel(CWnd* pParent /*=NULL*/)
: CDialog(parallel::IDD, pParent)
{
//{{AFX_DATA_INIT(parallel)
m_address = _T("0x378");
m_value = _T("0x00");
//}}AFX_DATA_INIT
}
void parallel::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(parallel)
DDX_Text(pDX, IDC_EDIT_ADDR, m_address);
DDX_Text(pDX, IDC_EDIT_VALUE, m_value);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(parallel, CDialog)
//{{AFX_MSG_MAP(parallel)
ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
ON_BN_CLICKED(IDC_RADIO_HEX, OnRadioHex)
ON_BN_CLICKED(IDC_RADIO_DEC, OnRadioDec)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// parallel message handlers
void parallel::OnButtonRead()
{
// char string[20];
// UpdateData(true);
int hexStatus=10 ;
if(m_hex) hexStatus= 16;
char tmp1[100];
char tmp2[100];
memset(tmp1, 0, 100); memset(tmp2, 0, 100);
GetDlgItemText(IDC_EDIT_ADDR, tmp1, 100);
GetDlgItemText(IDC_EDIT_VALUE, tmp2, 100);
sprintf((LPSTR)(LPCTSTR)m_address, tmp1);
sprintf((LPSTR)(LPCTSTR)m_value, tmp2);
if(!m_address.IsEmpty())
{
DWORD address= GetAddress();
if(address==0) {AfxMessageBox("Get Parallel address error"); return;}
switch(GetDataType())
{
case 0:
//m_value= itoa(DlPortReadPortUchar(address), string, hexStatus);
if(((CButton*)GetDlgItem(IDC_RADIO_HEX))->GetCheck())
sprintf((LPSTR)(LPCTSTR)m_value, "0x%0000x", DlPortReadPortUchar(address));
else
sprintf((LPSTR)(LPCTSTR)m_value, "%d", DlPortReadPortUchar(address));
break;
case 1:
//m_value= itoa(DlPortReadPortUshort(address), string, hexStatus);
if(((CButton*)GetDlgItem(IDC_RADIO_HEX))->GetCheck())
sprintf((LPSTR)(LPCTSTR)m_value, "0x%0000x", DlPortReadPortUshort(address));
else
sprintf((LPSTR)(LPCTSTR)m_value, "%d", DlPortReadPortUshort(address));
break;
case 2:
//m_value= itoa(DlPortReadPortUlong(address), string, hexStatus);
if(((CButton*)GetDlgItem(IDC_RADIO_HEX))->GetCheck())
sprintf((LPSTR)(LPCTSTR)m_value, "0x%0000x", DlPortReadPortUlong(address));
else
sprintf((LPSTR)(LPCTSTR)m_value, "%d", DlPortReadPortUlong(address));
break;
default:
break;
}
}
else
{
AfxMessageBox("Input Address, first!");
}
UpdateData(false);
// SetParallelStatus();
}
void parallel::OnButtonWrite()
{
// UpdateData(true);
char tmp1[100];
char tmp2[100];
memset(tmp1, 0, 100); memset(tmp2, 0, 100);
GetDlgItemText(IDC_EDIT_ADDR, tmp1, 100);
GetDlgItemText(IDC_EDIT_VALUE, tmp2, 100);
sprintf((LPSTR)(LPCTSTR)m_address, tmp1);
sprintf((LPSTR)(LPCTSTR)m_value, tmp2);
if(!m_value.IsEmpty())
{
DWORD address= GetAddress();
if(address==0) {AfxMessageBox("Get Parallel address error"); return;}
switch(GetDataType())
{
case 0:
DlPortWritePortUchar(address, HexToDec(m_value));
break;
case 1:
DlPortWritePortUshort(address, HexToDec(m_value));
break;
case 2:
DlPortWritePortUlong(address, HexToDec(m_value));
break;
default:
break;
}
//AfxMessageBox("Write OK!");
}
else
{
AfxMessageBox("Input Value(0 ~ 255), first!");
}
// SetParallelStatus();
}
void parallel::OnRadioHex()
{
//GetDlgItem(IDC_STATIC_FORMAT)->CheckRadioButton(IDC_RADIO_HEX,IDC_RADIO_DEC,0);
// ((CButton*)GetDlgItem(IDC_RADIO_HEX))->SetCheck(1);
// ((CButton*)GetDlgItem(IDC_RADIO_DEC))->SetCheck(0);
m_hex= true;
m_dec= false;
FormatConvert();
}
void parallel::OnRadioDec()
{
// ((CButton*)GetDlgItem(IDC_RADIO_HEX))->SetCheck(0);
// ((CButton*)GetDlgItem(IDC_RADIO_DEC))->SetCheck(1);
m_hex= false;
m_dec= true;
FormatConvert();
}
int parallel::GetDataType()
{
if(((CButton*)GetDlgItem(IDC_RADIO_BYTE))->GetCheck())
return 0;
if(((CButton*)GetDlgItem(IDC_RADIO_WORD))->GetCheck())
return 1;
if(((CButton*)GetDlgItem(IDC_RADIO_DWORD))->GetCheck())
return 2;
}
DWORD parallel::GetAddress()
{
DWORD m_dwAddr;
if(!m_address.IsEmpty()) //make sure a address value was entered
{
CString Hex = m_address.Left( 2 );
if(Hex.Compare("0x" ) == 0) //Was a hex number entered
{
sscanf(m_address,"0x%x", &m_dwAddr);
}
else //a decimal number
{
sscanf(m_address,"%d", &m_dwAddr);
}
//Perform range checking on address
if (((m_dwAddr >= 256) && (m_dwAddr <=1023)) == FALSE) //Out of range
{
AfxMessageBox("Address entered out of range! Address must be >= 256 and <= 1023", MB_ICONSTOP);
return FALSE;
}
}
return m_dwAddr;
}
BOOL parallel::OnInitDialog()
{
CDialog::OnInitDialog();
((CButton*)GetDlgItem(IDC_RADIO_HEX))->SetCheck(1);
((CButton*)GetDlgItem(IDC_RADIO_BYTE))->SetCheck(1);
SetParallelStatus();
SetTimer(1, 1, NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
DWORD parallel::HexToDec(CString& str)
{
DWORD value=0;
if(str != "") //if address was entered update variable
{
CString Hex = str.Left( 2 );
if(Hex.Compare("0x" ) == 0) //Was a hex number entered
{
sscanf(str,"0x%x", &value);
}
else //a decimal number
{
sscanf(str,"%d", &value);
}
}
return value;
}
void parallel::FormatConvert()
{
if (!((CButton*)GetDlgItem(IDC_RADIO_HEX))->GetCheck())
{
m_value.Format("%d", HexToDec(m_value));
m_address.Format("%d", GetAddress());
}
else
{ //format Data into a Hexadecimal #
m_value.Format("0x%0000x", HexToDec(m_value));
m_address.Format("0x%0000x", GetAddress());
}
UpdateData(FALSE);
}
void parallel::SetParallelStatus2()
{
/*
#define LPT 0x378
#define DATA_REG_ADDR 0x378
#define STATUS_REG_SADDR 0x379
#define CTR_REG_ADDR 0x37A
#define PIN1 0x01 //CTR_ADDR= 0x37A
#define PIN2 0x01 //ADDR= 0x378
#define PIN3 0x02
#define PIN4 0x04
#define PIN5 0x08
#define PIN6 0x10
#define PIN7 0x20
#define PIN8 0x40
#define PIN9 0x80
#define PIN10 0x40 //STATUS_ADDR= 0x379
#define PIN11 0x80
#define PIN12 0x20
#define PIN13 0x10
#define PIN14 0x02 //CTR_ADDR= 0x37A
#define PIN15 0x08 //STATUS_ADDR= 0x379
#define PIN16 0x04 //CTR_ADDR= 0x37A
#define PIN17 0x08
*/
BYTE value=0;
//char s[10];
if(!((CButton*)GetDlgItem(IDC_RADIO_HEX))->GetCheck())
sscanf(m_value, "%x", &value);
else
sscanf(m_value, "%x", &value);
if(m_address== "0x378") //pin2~pin9
{
if((value&PIN2) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN2))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN2))->SetCheck(1);
if((value&PIN3) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN3))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN3))->SetCheck(1);
if((value&PIN4) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN4))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN4))->SetCheck(1);
if((value&PIN5) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN5))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN5))->SetCheck(1);
if((value&PIN6) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN6))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN6))->SetCheck(1);
if((value&PIN7) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN7))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN7))->SetCheck(1);
if((value&PIN8) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN8))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN8))->SetCheck(1);
if((value&PIN9) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN9))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN9))->SetCheck(1);
}
if(m_address== "0x379") //
{
if((value&PIN10) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN10))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN10))->SetCheck(1);
if((value&PIN11) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN11))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN11))->SetCheck(1);
if((value&PIN12) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN12))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN12))->SetCheck(1);
if((value&PIN13) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN13))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN13))->SetCheck(1);
if((value&PIN15) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN15))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN15))->SetCheck(1);
}
if(m_address== "0x37A")
{
if((value&PIN1) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN1))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN1))->SetCheck(1);
if((value&PIN14) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN14))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN14))->SetCheck(1);
if((value&PIN16) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN16))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN16))->SetCheck(1);
if((value&PIN17) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN17))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN17))->SetCheck(1);
}
}
void parallel::SetParallelStatus()
{
BYTE valueArray[3]={0};
valueArray[0]= DlPortReadPortUchar(0x378);
valueArray[1]= DlPortReadPortUchar(0x379);
valueArray[2]= DlPortReadPortUchar(0x37a);
//if(m_address== "0x378") //pin2~pin9
{
if((valueArray[0]&PIN2) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN2))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN2))->SetCheck(1);
if((valueArray[0]&PIN3) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN3))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN3))->SetCheck(1);
if((valueArray[0]&PIN4) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN4))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN4))->SetCheck(1);
if((valueArray[0]&PIN5) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN5))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN5))->SetCheck(1);
if((valueArray[0]&PIN6) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN6))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN6))->SetCheck(1);
if((valueArray[0]&PIN7) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN7))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN7))->SetCheck(1);
if((valueArray[0]&PIN8) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN8))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN8))->SetCheck(1);
if((valueArray[0]&PIN9) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN9))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN9))->SetCheck(1);
}
//if(m_address== "0x379") //pin10~pin13, pin15
{
if((valueArray[1]&PIN10) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN10))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN10))->SetCheck(1);
if((valueArray[1]&PIN11) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN11))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN11))->SetCheck(1);
if((valueArray[1]&PIN12) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN12))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN12))->SetCheck(1);
if((valueArray[1]&PIN13) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN13))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN13))->SetCheck(1);
if((valueArray[1]&PIN15) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN15))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN15))->SetCheck(1);
}
//if(m_address== "0x37A") //pin1, pin14, pin16, pin17
{
if((valueArray[2]&PIN1) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN1))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN1))->SetCheck(1);
if((valueArray[2]&PIN14) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN14))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN14))->SetCheck(1);
if((valueArray[2]&PIN16) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN16))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN16))->SetCheck(1);
if((valueArray[2]&PIN17) == 0x00)
((CButton*)GetDlgItem(IDC_RADIO_PIN17))->SetCheck(0);
else
((CButton*)GetDlgItem(IDC_RADIO_PIN17))->SetCheck(1);
}
}
void parallel::OnTimer(UINT nIDEvent)
{
//updata parallel port all pin status
SetParallelStatus();
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -