// Sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "Sample1.h" #include "Sample1Dlg.h" #include "acces32.h" int Address; bool TimerEnabled = false; CString PortPanels[3]; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define InsCR(x) x += 0x0d; x += 0x0a; ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) m_PortLabel0 = _T("0000000000000000"); m_PortLabel1 = _T("0000000000000000"); m_PortLabel2 = _T("0000000000000000"); m_BaseEdit = _T("300"); m_Instructions = _T("Sample 0"); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_BaseEdit, m_BaseEditCtrl); DDX_Control(pDX, IDOK, m_StartButtonCtrl); DDX_Control(pDX, IDCANCEL, m_ExitButtonCtrl); DDX_Text(pDX, IDC_PortLabel0, m_PortLabel0); DDX_Text(pDX, IDC_PortLabel1, m_PortLabel1); DDX_Text(pDX, IDC_PortLabel2, m_PortLabel2); DDX_Text(pDX, IDC_BaseEdit, m_BaseEdit); DDX_Text(pDX, IDC_Instructions, m_Instructions); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDCANCEL, OnExitButtonClick) ON_BN_CLICKED(IDOK, OnStartTestButtonClick) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::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 if (InPortB(0x61) == 0xAA55) { MessageBox("ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.", "Warning", MB_OK); } InsCR(m_Instructions); m_Instructions += "This sample reads each of the IDI-48's input ports' 16 input channels and displays on/off status of those channels."; InsCR(m_Instructions); InsCR(m_Instructions); m_Instructions += "Please enter the base address (in hex) in the text box below, and click the 'Start Test' button to begin."; InsCR(m_Instructions); InsCR(m_Instructions); m_Instructions += "With nothing connected to the card's I/O pin headers, each port below should indicate zeroes (0000000000000000). If so, the card is successfully installed at the base address you entered. Apply a voltage source (a 9V battery works great) to any pair of I/O pins (3 to 4, 9 to 10, etc, pins 1 and 2 are not connected on either header) and you should see the data display change to a 1 for that pair of pins. If the ports indicate all ones(1111111111111111), that probably means the card is not properly configured for that address. (On some embedded CPUs, CMOS settings for I/O space must be configured as well.)"; UpdateData(false); 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 CSample1Dlg::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 CSample1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample1Dlg::OnExitButtonClick() { exit(0); } void CSample1Dlg::OnStartTestButtonClick() { if (TimerEnabled) { TimerEnabled = false; m_StartButtonCtrl.SetWindowText("Start Test"); m_BaseEditCtrl.EnableWindow(true); KillTimer(0); } else { TimerEnabled = true; UpdateData(true); sscanf(m_BaseEdit.GetBuffer(0), "%x", &Address); m_StartButtonCtrl.SetWindowText("Abort Test"); m_BaseEditCtrl.EnableWindow(false); SetTimer(0, 100, NULL); } } void CSample1Dlg::OnTimer(UINT nIDEvent) { int port, i; unsigned data,mask; unsigned char ofs[] = { 0,1,2,4,5,6 }; // Table of offsets to input registers PortPanels[0] = ""; PortPanels[1] = ""; PortPanels[2] = ""; for (port = 0; port <= 2; port++) { // Read the 16 input channels of current port data = InPortB(Address + ofs[2*port])*256; data += InPortB(Address + ofs[(2*port)+1]); // Check the status of each bit field and display result mask = 1; for (i = 0; i < 16; i++) { if (mask & data) PortPanels[port].Insert(0,"1"); else PortPanels[port].Insert(0,"0"); mask <<= 1; } // end for i } // end for port m_PortLabel0 = PortPanels[0]; m_PortLabel1 = PortPanels[1]; m_PortLabel2 = PortPanels[2]; UpdateData(false); CDialog::OnTimer(nIDEvent); }