// IIRO16Dlg.cpp : Sample 0 program for 104-IDIO16 // #include "stdafx.h" #include "IIRO16.h" #include "IIRO16Dlg.h" #include "ACCES32.h" unsigned short Address; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CIDIO16Dlg dialog CIIRO16Dlg::CIIRO16Dlg(CWnd* pParent /*=NULL*/) : CDialog(CIIRO16Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CIIRO16Dlg) m_EditBox = _T("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 how to use the readback function of the board."); m_Opto = _T("0000000000000000"); m_Relay = _T("0000000000000000"); m_BaseEdit = _T("300"); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CIIRO16Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CIIRO16Dlg) DDX_Control(pDX, IDC_START, m_TimerButton); DDX_Text(pDX, IDC_EDIT1, m_EditBox); DDX_Text(pDX, IDC_OPTO, m_Opto); DDX_Text(pDX, IDC_RELAY, m_Relay); DDX_Text(pDX, IDC_BASEEDIT, m_BaseEdit); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CIIRO16Dlg, CDialog) //{{AFX_MSG_MAP(CIIRO16Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_START, OnStart) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CIIRO16Dlg message handlers BOOL CIIRO16Dlg::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); } return TRUE; // return TRUE unless you set the focus to a control } void CIIRO16Dlg::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(); } } HCURSOR CIIRO16Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CIIRO16Dlg::OnStart() { static int TimerEnable = 0; //Set Timer flag to 0 sscanf(m_BaseEdit.GetBuffer(0), "%x", &Address); //Get the BaseAddress from the dialog box if (TimerEnable) //Check to see if Timer is active { TimerEnable = 0; //Disable Timer flag m_TimerButton.SetWindowText("Perform I/O"); KillTimer(0); //Disable Timer } else { TimerEnable = 1; //Enable Timer Flag m_TimerButton.SetWindowText("Stop I/O"); SetTimer(0, 1000, NULL); //Enable Timer } } void CIIRO16Dlg::OnTimer(UINT nIDEvent) { static int value[4] = {0,0,0,0}; //Set of Arrays for storing Bit info from card static int i=0; int j; unsigned y; char msg[17] = "0000000000000000"; //Set bit holders if (i < 8) { //Test if program is checking upper or lower bit ranges value[1] = 0; value[0] = 1 << i; } else { value[0] = 0; value[1] = 1 << (i % 8); } if (i < 8) OutPortB(Address,value[0]); else OutPortB(Address+4,value[1]); //Outport bit value to correct base address, Base+0 for Bits 1-8, Base+4 for Bits 9-16 y = GetTickCount() + 10; //Code pauses program for 10 Milliseconds allowing card plenty of time to process data while (GetTickCount() < y); if (i < 8) { //Test if program is checking upper or lower bit ranges value[3] = 0; value[2] = InPortB(Address+1); //Read Base+1 for lower range, 1-8 } else { value[2] = 0; value[3] = InPortB(Address+5); //Read Base+5 for upper range 9-16 } i++; i %= 16; UpdateData(true); //Begin storing Data for (j = 0; j < 8; j++) msg[15-j] = (((value[0] & (1 << j))>0) + 0x30); //Create bit series for output to show which is set high. for (j = 0; j < 8; j++) msg[7-j] = (((value[1] & (1 << j))>0) + 0x30); m_Relay = msg; for (j = 0; j < 8; j++) msg[15-j] = (((value[2] & (1 << j))>0) + 0x30); for (j = 0; j < 8; j++) msg[7-j] = (((value[3] & (1 << j))>0) + 0x30); m_Opto = msg; UpdateData(false); //Take stored data and output to screen CDialog::OnTimer(nIDEvent); }