// DIODlg.cpp : implementation file // #include "stdafx.h" #include "DIO.h" #include "DIODlg.h" #include "afxdialogex.h" #include "AIOWDM.h" //WDM Driver #ifdef _DEBUG #define new DEBUG_NEW #endif #define InsCR(x) x += 0x0d; x += 0x0a; // CAboutDlg dialog used for App About class CAboutDlg : public CDialogEx { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() public: // afx_msg void OnTimer(UINT_PTR nIDEvent); }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) // ON_WM_TIMER() END_MESSAGE_MAP() // CDIODlg dialog CDIODlg::CDIODlg(CWnd* pParent /*=NULL*/) : CDialogEx(CDIODlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_AddressSelection = _T(""); m_InputA = _T(""); m_InputB = _T(""); m_OutputA = _T(""); m_StatusEdit = _T(""); m_CardName = _T(""); } void CDIODlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_COMBO1, m_AddressCombo); DDX_CBString(pDX, IDC_COMBO1, m_AddressSelection); DDX_Text(pDX, IDC_EDIT_INPUTA, m_InputA); DDX_Text(pDX, IDC_EDIT_INPUTB, m_InputB); DDX_Text(pDX, IDC_EDIT_OUTPUTA, m_OutputA); DDX_Text(pDX, IDC_EDIT_STATUS, m_StatusEdit); // DDX_Control(pDX, IDC_STATIC_CARD_NAME, m_StatusControl); DDX_Control(pDX, IDC_BUTTON_PERFORMIO, m_TimerButton); DDX_Control(pDX, IDC_EDIT_STATUS, m_StatusControl); DDX_Text(pDX, IDC_STATIC_CARD_NAME, m_CardName); } BEGIN_MESSAGE_MAP(CDIODlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_PERFORMIO, &CDIODlg::OnBnClickedPerformIO) ON_WM_TIMER() ON_CBN_SELCHANGE(IDC_COMBO1, &CDIODlg::OnCbnSelchangeCombo1) END_MESSAGE_MAP() // CDIODlg message handlers BOOL CDIODlg::OnInitDialog() { CDialogEx::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) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); 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 // TODO: Add extra initialization here // Ping the driver AIOWDM.SYS: if (InPortB(0x61) == 0xAA55) { AfxMessageBox(_T("AIOWDM.SYS not detected.\n Please copy AIOWDM.SYS into [Windows]/system32/drivers and re-run this sample.\n Ensure that a board is installed properly.")); } FindCardsWDM(); m_StatusEdit = _T("This sample outputs a walking bit on Port A of the selected group. It then reads back the value from Port A. This should be identical to the output value due to on-chip readback. It also displays the Input from Port B. If a wrap plug is installed, Port B should read the same value. Otherwise, ground individual bits on Port B to see them show zeroes on the display."); return TRUE; // return TRUE unless you set the focus to a control } void CDIODlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::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 CDIODlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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 { CDialogEx::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDIODlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CDIODlg::OnBnClickedPerformIO() { // TODO: Add your control notification handler code here static bool TimerEnabled = false; if (TimerEnabled) { TimerEnabled = false; KillTimer(0); m_TimerButton.SetWindowText(_T("Perform I/O")); } else { TimerEnabled = true; OutPortB(Address + 3, 0x82); // Driver call SetTimer(0, 1000, NULL); m_TimerButton.SetWindowText(_T("Stop I/O")); } } void CDIODlg::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default static int value[3] = {0,0,0}, i = 0; int j; char msg[9] = "00000000"; value[0] = 1 << i++; OutPortB(Address, value[0]); // Driver Calls value[1] = InPortB(Address); value[2] = InPortB(Address+1); i %= 8; for (j=0; j<8; j++) msg[7-j] = (((value[0] & (1 << j)) > 0) + 0x30); m_OutputA = msg; for (j=0; j<8; j++) msg[7-j] = (((value[1] & (1 << j)) > 0) + 0x30); m_InputA = msg; for (j=0; j<8; j++) msg[7-j] = (((value[2] & (1 << j)) > 0) + 0x30); m_InputB = msg; UpdateData(false); CDialogEx::OnTimer(nIDEvent); } void CDIODlg::OnCbnSelchangeCombo1() { // TODO: Add your control notification handler code here CString SelStr; UpdateData(true); m_AddressCombo.GetLBText(m_AddressCombo.GetCurSel(), SelStr); // Select and set new address data: if(CardData[m_AddressCombo.GetCurSel()].IsValid == true) { Address = CardData[m_AddressCombo.GetCurSel()].Base; // Deselect all data and set new selected data: for(int count = 0; count < MAX_CARDS; count++) { CardData[count].IsSelected = false; } CardData[m_AddressCombo.GetCurSel()].IsSelected = true; } //InsCR(m_StatusEdit); m_StatusEdit += "New Port Selected: "; m_StatusEdit += SelStr; m_AddressSelection = SelStr; UpdateData(false); //m_StatusControl.LineScroll(999); } void CDIODlg::FindCardsWDM(void) { bool found = false; char AddStr[256]; // Vars for QueryCardInfo() : signed int CardNum; unsigned long DeviceID, Base; unsigned long NameSize; UCHAR Name[256]; CString strname; // Init: RunFlag = true; NumCards = 0; int DataIndex = 0; // Set CardData to 0 as needed max 10: for(int count = 0; count < MAX_CARDS; count++) { CardData[count].IsValid = false; CardData[count].IsSelected = false; CardData[count].DeviceID = 0; CardData[count].Base = 0; }; // Get the total number of cards installed: NumCards = GetNumCards(); if (NumCards == 0) // no cards present { m_CardName = "No Card Found"; m_StatusEdit = "No cards were found!"; m_StatusEdit += "This may mean the card is not installed. "; m_StatusEdit += "Check Device Manager for a card and its status"; m_StatusEdit += "You may consider rebooting your system."; m_AddressCombo.EnableWindow(false); m_StatusControl.LineScroll(999); RunFlag = false; } else { // Loop through the cards and validate and store card data: for (CardNum = 0; CardNum != NumCards; CardNum++) { NameSize = 256; QueryCardInfo(CardNum, &DeviceID, &Base, &NameSize, Name); // Populate list box with addresses found set flags: switch (DeviceID) { // Will only cover PCIe-DIO-24 family in this sample: case 0x0C52: m_CardName = "PCIe-DIO-24 Parallel Digital I/O Card"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E53: m_CardName = "PCIe-DIO-24S Parallel Digital I/O Card"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0C53: m_CardName = "PCIe-DIO-24D Parallel Digital I/O Card"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E55: m_CardName = "PCIe-DIO-24DC Parallel Digital I/O Card w/Counter"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E54: m_CardName = "PCIe-DIO-24DS Parallel Digital I/O Card"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E56: m_CardName = "PCIe-DIO-24DCS Parallel Digital I/O Card w/Counter"; sprintf(AddStr, "Port 0: %4X", Base & 0xFFF8); strname = AddStr; m_AddressCombo.AddString(strname); if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; // default: break; } }// end loop on validate present cards }//end else card present // If card(s) are present but no valid cards were found: if ((NumCards != 0) && (found == false) ) { m_CardName = "No Valid Card Found."; m_StatusEdit = "No valid card was found!"; m_StatusEdit += "This may mean the card is not installed. "; m_StatusEdit += "Check Device Manager for a card and its status"; m_StatusEdit += "You may consider rebooting your system."; m_AddressCombo.EnableWindow(false); m_StatusControl.LineScroll(999); RunFlag = false; } UpdateData(false); m_AddressCombo.SetCurSel(0); if (RunFlag) { UpdateData(true); // This is the result setting the active address to be used: Address = CardData[0].Base; //default to fist valid CardData[0].IsSelected = true; } }