// Sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "Sample1.h" #include "Sample1Dlg.h" #include "afxdialogex.h" #include "cstringt.h" #include "string.h" #include "aiousb.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // Data array: unsigned short ADData[128*1024]; //8K scans of 16 channels, or 16K scans of 8 differential channels. // 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() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialogEx(CSample1Dlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_BUTTON_GO, m_BtnGO); DDX_Control(pDX, IDC_CHECK_DIFF, m_CheckDiff); DDX_Control(pDX, IDC_COMBO_CAL, m_ComboCal); DDX_Control(pDX, IDC_COMBO_RANGE, m_ComboRange); DDX_Control(pDX, IDC_PROGRESS_00, m_progress0); DDX_Control(pDX, IDC_PROGRESS_01, m_progress1); DDX_Control(pDX, IDC_PROGRESS_02, m_progress2); DDX_Control(pDX, IDC_PROGRESS_03, m_progress3); DDX_Control(pDX, IDC_PROGRESS_04, m_progress4); DDX_Control(pDX, IDC_PROGRESS_05, m_progress5); DDX_Control(pDX, IDC_PROGRESS_06, m_progress6); DDX_Control(pDX, IDC_PROGRESS_07, m_progress7); DDX_Control(pDX, IDC_PROGRESS_08, m_progress8); DDX_Control(pDX, IDC_PROGRESS_09, m_progress9); DDX_Control(pDX, IDC_PROGRESS_10, m_progress10); DDX_Control(pDX, IDC_PROGRESS_11, m_progress11); DDX_Control(pDX, IDC_PROGRESS_12, m_progress12); DDX_Control(pDX, IDC_PROGRESS_13, m_progress13); DDX_Control(pDX, IDC_PROGRESS_14, m_progress14); DDX_Control(pDX, IDC_PROGRESS_15, m_progress15); DDX_Control(pDX, IDC_STATIC_STATUS, m_StaticStatus); DDX_Control(pDX, IDC_STATIC_CAL, m_staticCal); DDX_Control(pDX, IDC_STATIC_RANGE, m_staticRange); DDX_Control(pDX, IDC_EDIT_STATUS, m_editStatus); DDX_Control(pDX, IDC_STATIC00, m_staticVolt00); DDX_Control(pDX, IDC_STATIC01, m_staticVolt01); DDX_Control(pDX, IDC_STATIC02, m_staticVolt02); DDX_Control(pDX, IDC_STATIC03, m_staticVolt03); DDX_Control(pDX, IDC_STATIC04, m_staticVolt04); DDX_Control(pDX, IDC_STATIC05, m_staticVolt05); DDX_Control(pDX, IDC_STATIC06, m_staticVolt06); DDX_Control(pDX, IDC_STATIC07, m_staticVolt07); DDX_Control(pDX, IDC_STATIC08, m_staticVolt08); DDX_Control(pDX, IDC_STATIC09, m_staticVolt09); DDX_Control(pDX, IDC_STATIC10, m_staticVolt10); DDX_Control(pDX, IDC_STATIC11, m_staticVolt11); DDX_Control(pDX, IDC_STATIC12, m_staticVolt12); DDX_Control(pDX, IDC_STATIC13, m_staticVolt13); DDX_Control(pDX, IDC_STATIC14, m_staticVolt14); DDX_Control(pDX, IDC_STATIC15, m_staticVolt15); } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_GO, &CSample1Dlg::OnBnClickedButtonGo) ON_WM_TIMER() END_MESSAGE_MAP() // CSample1Dlg message handlers BOOL CSample1Dlg::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 bAcquiring = false; // Default selections: m_ComboCal.SetCurSel(0); m_ComboRange.SetCurSel(0); unsigned long Status, PID, ConfigBufSize; double Hz; int count; DeviceIndex = diOnly; Status = QueryDeviceInfo(DeviceIndex, &PID, NULL, NULL, NULL, NULL); VoltProgress[0] = &m_progress0; VoltProgress[1] = &m_progress1; VoltProgress[2] = &m_progress2; VoltProgress[3] = &m_progress3; VoltProgress[4] = &m_progress4; VoltProgress[5] = &m_progress5; VoltProgress[6] = &m_progress6; VoltProgress[7] = &m_progress7; VoltProgress[8] = &m_progress8; VoltProgress[9] = &m_progress9; VoltProgress[10] = &m_progress10; VoltProgress[11] = &m_progress11; VoltProgress[12] = &m_progress12; VoltProgress[13] = &m_progress13; VoltProgress[14] = &m_progress14; VoltProgress[15] = &m_progress15; VoltStatic[0] = &m_staticVolt00; VoltStatic[1] = &m_staticVolt01; VoltStatic[2] = &m_staticVolt02; VoltStatic[3] = &m_staticVolt03; VoltStatic[4] = &m_staticVolt04; VoltStatic[5] = &m_staticVolt05; VoltStatic[6] = &m_staticVolt06; VoltStatic[7] = &m_staticVolt07; VoltStatic[8] = &m_staticVolt08; VoltStatic[9] = &m_staticVolt09; VoltStatic[10] = &m_staticVolt10; VoltStatic[11] = &m_staticVolt11; VoltStatic[12] = &m_staticVolt12; VoltStatic[13] = &m_staticVolt13; VoltStatic[14] = &m_staticVolt14; VoltStatic[15] = &m_staticVolt15; if ( ADC_QueryCal(DeviceIndex) != ERROR_SUCCESS ) { //this board doesn't have calibration m_staticCal. ShowWindow(false); m_ComboCal.ShowWindow(false); } //Stop the counter, in case it was running. Hz = 0; CTR_StartOutputFreq(DeviceIndex, 0, &Hz); ZeroMemory(&Config, sizeof(Config)); Config.CalibMode = 0; //Take actual data, not internal calibration sources. Config.TrigMode = 5; //Scan selected channels each counter rising edge. Config.Oversample = 0;//No oversample. ConfigBufSize = sizeof(Config); ADC_SetConfig (DeviceIndex, &Config, &ConfigBufSize); NextChannel = 0; for (count = 0; count <= 15; count++) { //VoltProgress[count] -> SetRange32(0, 0xFFFF); VoltProgress[count] -> SetRange32( -10000, 10000); VoltProgress[count] -> SetPos(0); } return TRUE; // return TRUE unless you set the focus to a control } void CSample1Dlg::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 CSample1Dlg::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 CSample1Dlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CSample1Dlg::OnBnClickedButtonGo() { // vars: unsigned long Status = 0; unsigned long ConfigBufSize; byte RangeCode = 0; double Hz = 0; int i = 0; CString strStatus; int calIndex; char cAuto[7]; char cNone[7]; strcpy_s(cAuto,":AUTO:"); strcpy_s(cNone,":1TO1:"); // Test and Set Calibration: if(m_ComboCal.IsWindowVisible()) { calIndex = m_ComboCal.GetCurSel(); if(calIndex == 0) { Status = ADC_SetCal(DeviceIndex, cAuto); } else { Status = ADC_SetCal(DeviceIndex, cNone); } } if( Status != ERROR_SUCCESS) { strStatus.Format(_T("Error setting calibration. Status: %d"), Status); m_editStatus.SetWindowTextW(strStatus); } //Stop the counter, in case it was running. Hz = 0; CTR_StartOutputFreq(DeviceIndex, 0, &Hz); //Set range: //The ranges in the combo box are listed in order, so that the index is equal to the range code. RangeCode = m_ComboRange.GetCurSel(); if ( m_CheckDiff.GetCheck() ) { RangeCode |= 0x08; } for (i = 0; i <= 15; i++) Config.ChannelRange[i] = RangeCode; // Set start stop channels: if ( m_CheckDiff.GetCheck() != false ) { Config.StartStopCH = 0x70; //Select all differential channels, 0 to 7. } else { Config.StartStopCH = 0xF0; //Select all channels, 0 to 15. } // Setup config: Config.CalibMode = 0; //Take actual data, not internal calibration sources. Config.TrigMode = 0x05; //Scan selected channels each counter rising edge. Config.Oversample = 0; //No oversample. ConfigBufSize = sizeof(Config); ADC_SetConfig(DeviceIndex, &Config, &ConfigBufSize); // Set Channel Displays if ( m_CheckDiff.GetCheck() ) { for (i = 8; i <= 15; i++) { VoltProgress[i]->EnableWindow(false); VoltProgress[i]->ShowWindow(false); VoltStatic[i]->ShowWindow(false); } } else { for (i = 8; i <= 15; i++) { VoltProgress[i]->EnableWindow(true); VoltProgress[i]->ShowWindow(true); VoltStatic[i]->ShowWindow(true); } } //Since we've put it in scan mode above, this is 1kHz per channel, or 16kHz total. Hz = 1000; CTR_StartOutputFreq(DeviceIndex, 0, &Hz); //Set the streaming block size fairly small, to make the UI more responsive. //The default (large) streaming block size is necessary for a slow computer, //or when acquiring close to the max for a fast board. AIOUSB_SetStreamingBlockSize(DeviceIndex, 512); // Set up Bulk Acquire: KnownBytesLeft = 128 * 1024 * sizeof(ADData[0]); // RBF Fix Status = ADC_BulkAcquire(DeviceIndex, KnownBytesLeft, ADData); // Post messages to GUI: if(Status != 0) { //Error: strStatus = "Error Starting acquisition!"; m_editStatus.SetWindowTextW(strStatus); } else { //Acquiring: strStatus = "Acquiring Data..."; m_editStatus.SetWindowTextW(strStatus); } NextSample = 0; NextChannel = 0; // Set Gui enable states: m_BtnGO.EnableWindow(false); m_CheckDiff.EnableWindow(false); m_ComboCal.EnableWindow(false); m_ComboRange.EnableWindow(false); // loop flag bAcquiring = true; //Start the timer. SetTimer(1, 1, NULL); } void CSample1Dlg::OnTimer(UINT_PTR nIDEvent) { unsigned long Status = 0; unsigned long BytesLeft = 0; double Volts = 0; byte RangeCode = 0; int i = 0; unsigned short Counts = 0; CString strStatus; Status = ADC_BulkPoll(DeviceIndex, &BytesLeft); // Post messages to GUI: if(Status != 0) { //Error: strStatus.Format(_T("Error from ADC_BulkPoll. Status: %d"), Status); m_editStatus.SetWindowTextW(strStatus); // Set Gui enable states: m_BtnGO.EnableWindow(true); m_CheckDiff.EnableWindow(true); m_ComboCal.EnableWindow(true); m_ComboRange.EnableWindow(true); // loop flag bAcquiring = false; // Exit: return; } else { //Acquiring: //strStatus = "Acquiring!"; //m_editStatus.SetWindowTextW(strStatus); } if (BytesLeft >= KnownBytesLeft) return; //If we get here, some data has been taken. if (BytesLeft == 0) { // loop flag bAcquiring = false; // Message: strStatus = "Acquisition Complete!"; m_editStatus.SetWindowTextW(strStatus); // Set Gui enable states: m_BtnGO.EnableWindow(true); m_CheckDiff.EnableWindow(true); m_ComboCal.EnableWindow(true); m_ComboRange.EnableWindow(true); } do { //Volts = double(Counts) / double(0xFFFF); //"Volts" now holds a value from 0 to 1. Volts = double( ADData[NextSample]) / double(0xFFFF); //"Volts" now holds a value from 0 to 1. //We're going to use the range code to convert to volts. RangeCode = Config.ChannelRange[NextChannel]; if ((RangeCode & 0x01) != 0) //Bit 0(mask 01 hex) indicates bipolar. { Volts = Volts * 2 - 1; //"Volts" now holds a value from -1 to +1. } if ((RangeCode & 0x02) == 0) //Bit 1(mask 02 hex) indicates x2 gain. { Volts *= 2; } if ((RangeCode & 0x04) == 0) //Bit 2(mask 04 hex) indicates x5 gain. { Volts *= 5; } //"Volts" now holds volts. //Because the display can be slow, we just display the last reading for each channel. ChannelVolts[NextChannel] = Volts; KnownBytesLeft -= 2; NextSample++; NextChannel++; if(NextChannel > 15) NextChannel = 0; }while (KnownBytesLeft != BytesLeft); for (i = 0; i <= 15; i++) { // Update GUI ooutside the previous loop: CString strVolt; strVolt.Format(_T("%0.3f V"), ChannelVolts[i]); VoltStatic[i]->SetWindowText(strVolt); Counts = ADData[i]; VoltProgress[i] -> SetPos((int)(1000 * ChannelVolts[i])); // Message: if(bAcquiring) { strStatus = "Acquiring Data."; m_editStatus.SetWindowTextW(strStatus); } } CDialogEx::OnTimer(nIDEvent); }