// sample0Dlg.cpp : implementation file // #include "stdafx.h" #include "sample0.h" #include "sample0Dlg.h" #include "ACCES32.h" #define InsCR(x) x += 0x0d; x += 0x0a; unsigned short Address; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static \char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg dialog CSample0Dlg::CSample0Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample0Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample0Dlg) m_BaseAddress = _T("300"); m_RelayOutput = _T("00000000"); m_RelayReadback = _T("00000000"); m_OptoOutput = _T("00000000"); m_StatusEdit = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample0Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample0Dlg) DDX_Control(pDX, IDC_TIMER, m_TimerButton); DDX_Control(pDX, IDC_STATUS, m_StatusControl); DDX_Control(pDX, IDC_BASEADDRESS, m_BaseAddress_Ctrl); DDX_Text(pDX, IDC_BASEADDRESS, m_BaseAddress); DDX_Text(pDX, IDC_OUTPUTA, m_RelayOutput); DDX_Text(pDX, IDC_INPUTA, m_RelayReadback); DDX_Text(pDX, IDC_INPUTB, m_OptoOutput); DDX_Text(pDX, IDC_STATUS, m_StatusEdit); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample0Dlg, CDialog) //{{AFX_MSG_MAP(CSample0Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnExitButton) ON_BN_CLICKED(IDC_TIMER, OnTimerClick) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg message handlers BOOL CSample0Dlg::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 CONSTRUCT(); 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); } 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 CSample0Dlg::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 CSample0Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample0Dlg::OnExitButton() { exit(0); } void CSample0Dlg::CONSTRUCT(void) { UpdateData(true); InsCR(m_StatusEdit); m_StatusControl.LineScroll(999); m_StatusEdit += "This sample program will sequentially turn on all bits of the relay input and sequentially turn them off. Each time it sets a new bit, both the relay output and the relay input are read and the data is displayed. This demonstrates how to read and write to the relays, and to use the readback function of the board."; UpdateData(false); } void CSample0Dlg::OnTimerClick() { static int TimerEnable = 0; sscanf(m_BaseAddress.GetBuffer(0), "%x", &Address); if (TimerEnable) { TimerEnable = 0; m_TimerButton.SetWindowText("Perform I/O"); KillTimer(0); } else { TimerEnable = 1; m_TimerButton.SetWindowText("Stop I/O"); SetTimer(0, 1000, NULL); OutPortB(Address+3, 0x01); } } void CSample0Dlg::OnTimer(UINT nIDEvent) { static int value[3] = {0,0,0}, i = 0; int j; char msg[9] = "00000000"; static char msgRO[9] = "00000000"; UpdateData(true); value[0] = 1 << i++; value[1] = InPortB(Address+3); value[2] = InPortB(Address+1); i %= 8; m_RelayOutput = msgRO; for (j=0; j<8; j++) msg[7-j] = (((value[1] & (1 << j))>0) + 0x30); m_RelayReadback = msg; for (j=0; j<8; j++) msg[7-j] = (((value[2] & (1 << j))>0) + 0x30); m_OptoOutput = msg; UpdateData(false); OutPortB(Address, value[0]); for (j=0; j<8; j++) msgRO[7-j] = (((value[0] & (1 << j))>0) + 0x30); CDialog::OnTimer(nIDEvent); }