// insample0Dlg.cpp : implementation file // #include "stdafx.h" #include "insample0.h" #include "insample0Dlg.h" #include "DetectDialog.h" #include "aiousb.h" #define READ_CLOCK 0.0 #define WRITE_CLOCK 0.0 #define NUM_SCANS 20 //Each scan is [NUM_READINGS] transfers #define NUM_READINGS 8192 CListBox * m_g_ReportList; unsigned int readings[NUM_READINGS]; unsigned long deviceIndex; UINT __cdecl threadfunc (LPVOID pParam); #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CInsample0Dlg dialog CInsample0Dlg::CInsample0Dlg(CWnd* pParent /*=NULL*/) : CDialog(CInsample0Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CInsample0Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CInsample0Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CInsample0Dlg) DDX_Control(pDX, IDC_REPORT_LIST, m_ReportList); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CInsample0Dlg, CDialog) //{{AFX_MSG_MAP(CInsample0Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CInsample0Dlg message handlers BOOL CInsample0Dlg::OnInitDialog() { CDialog::OnInitDialog(); int status; double read_clock, write_clock; // 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 unsigned long pID; DetectDialog box; deviceIndex = 0; if (QueryDeviceInfo(diOnly, &pID, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) if ( (pID == 0x800C) || (pID == 0x800D) || (pID == 0x800F) ) { deviceIndex = diOnly; } if (deviceIndex == 0) { if (box.DoModal() == IDOK) deviceIndex = box.index; else exit(1); } read_clock = READ_CLOCK; write_clock = WRITE_CLOCK; status = DIO_StreamSetClocks(deviceIndex, &read_clock, &write_clock); if (status != ERROR_SUCCESS) { MessageBox("Could not set clocks.", "ERROR"); exit(-1); } 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 CInsample0Dlg::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 CInsample0Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CInsample0Dlg::OnButton1() { m_g_ReportList = &m_ReportList; AfxBeginThread(threadfunc, NULL); } UINT __cdecl threadfunc (LPVOID pParam) { unsigned short readings2D[NUM_SCANS][NUM_READINGS + 255]; unsigned long bytes_transferred, total_readings; unsigned long status, Calls; status = DIO_StreamOpen (deviceIndex, 1); //the 1 means it is a read. Send 0 for a write if (status != ERROR_SUCCESS) //this really should never happen. { m_g_ReportList->AddString("Failed to open DIO stream."); //MessageBox("Could not open the stream.", "ERROR", MB_OK); return 1; } m_g_ReportList->AddString("Opened DIO stream..."); for ( int iScan = 0; iScan < NUM_SCANS; ++iScan ) { //m_g_ReportList->AddString("Clearing FIFO..."); unsigned long Status = AIOUSB_ClearFIFO(deviceIndex, TIME_METHOD_WAIT_INPUT_ENABLE); /* if ( Status ) { char Buf[128]; sprintf(Buf, "Error code %ld...", Status); m_g_ReportList->AddString(Buf); } else { m_g_ReportList->AddString("Success..."); } */ total_readings = 0; Calls = 0; //m_g_ReportList->AddString("Getting scan data..."); do { //If making a "remainder" request, round the remainder up to the next 256-sample packet. unsigned long MasterCt = NUM_READINGS - total_readings; unsigned long RoundCt = MasterCt & 0xFFFFFF00; if ( MasterCt & 0xFF ) RoundCt += 0x100; unsigned long Status = DIO_StreamFrame(deviceIndex, RoundCt, &(readings2D[iScan][total_readings]), &bytes_transferred); total_readings += bytes_transferred / 2UL; ++Calls; if ( Status ) { char Buf[128]; sprintf(Buf, "Error code %ld...", Status); m_g_ReportList->AddString(Buf); } else if ( bytes_transferred >= 2 ) { char Buf[128]; sprintf(Buf, "Got %ld bytes...", bytes_transferred); m_g_ReportList->AddString(Buf); } else { m_g_ReportList->AddString("Success, but no data..."); } } while(total_readings < NUM_READINGS); //once we are out of this loop the readings array will contain the requested number of readings. { char Buf[128]; sprintf(Buf, "Got scan, %ld calls...", Calls); m_g_ReportList->AddString(Buf); } } m_g_ReportList->AddString("Done getting data..."); DIO_StreamClose(deviceIndex); m_g_ReportList->AddString("Closed DIO stream..."); HANDLE hFil = CreateFile("Log.bin", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL); /* if ( hFil ) { m_g_ReportList->AddString("Opened log file..."); } else { m_g_ReportList->AddString("Failed to open log file..."); } */ unsigned long cbRet; char Buf[32]; for ( iScan = 0; iScan < NUM_SCANS; ++iScan ) { m_g_ReportList->AddString("Writing scan data..."); sprintf(Buf, "/BEGIN SCAN\\\r\n"); WriteFile(hFil, Buf, strlen(Buf), &cbRet, NULL); for ( int iReading = 0; iReading < NUM_READINGS; ++iReading ) { sprintf(Buf, "%04X\r\n", readings2D[iScan][iReading]); WriteFile(hFil, Buf, strlen(Buf), &cbRet, NULL); } sprintf(Buf, "\\END SCAN/\r\n\r\n"); WriteFile(hFil, Buf, strlen(Buf), &cbRet, NULL); } CloseHandle(hFil); m_g_ReportList->AddString("Done."); return 0; }