⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9112cdma.c

📁 凌华工控控制卡PCI-9112使用例子
💻 C
📖 第 1 页 / 共 2 页
字号:
                      WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | BS_LEFTTEXT | WS_GROUP,
                      output_pos[4].x+100, io_pos[0].y+49+20,25,15, hWnd, IDC_CH+7, hInst, NULL);

        //default channel 0
        for (i=1;i<8;i++) bCheck[i]=FALSE;
        bCheck[0] = TRUE;
        channel = 0;
        SendMessage(GetDlgItem(hWnd,IDC_CH), BM_SETCHECK, 1, 0);

        hBlackBrush= CreateSolidBrush(RGB(0,0,0));
        hRedPen= CreatePen(PS_SOLID,1,RGB(255,0,0));
        hBlackPen= CreatePen(PS_SOLID,1,RGB(0,0,0));
        break;

    case WM_PAINT:
        hDC = BeginPaint(hWnd, &ps);

        // DMA analog input box
        SelectObject(hDC, GetStockObject(GRAY_BRUSH));
        Rectangle(hDC, output_pos[15].x-10, io_pos[15].y-24, output_pos[0].x+26, io_pos[0].y+195);
        SetTextColor(hDC,RGB(255,255,0));
        SetBkColor(hDC, RGB(0, 0, 255));
        TextOut(hDC, io_pos[15].x, io_pos[15].y-35, "DMA Input", 9);
        SelectObject(hDC, hBlackBrush);
        Rectangle(hDC, output_pos[15].x+35, io_pos[15].y-2, output_pos[4].x+41, io_pos[0].y+172);

        SetBkMode(hDC,TRANSPARENT);
        TextOut(hDC,output_pos[13].x-60, input_pos[10].y,"Samping Rate",12);
        TextOut(hDC,output_pos[13].x+30, input_pos[15].y+20,"KHZ",3);
        TextOut(hDC,output_pos[13].x+70, input_pos[10].y,"Buffer Size",11);
        TextOut(hDC,input_pos[2].x-10, io_pos[15].y+118,"Range: +-5V",11);
        TextOut(hDC,input_pos[15].x, io_pos[15].y-19,"Volt",4);
        TextOut(hDC,input_pos[15].x, io_pos[15].y-5,"5.00",4);
        TextOut(hDC,input_pos[15].x, io_pos[15].y+36,"2.50",4);
        TextOut(hDC,input_pos[15].x, io_pos[15].y+77,"0.00",4);
        TextOut(hDC,input_pos[15].x-3, io_pos[15].y+118,"-2.50",5);
        TextOut(hDC,input_pos[15].x-3, io_pos[15].y+159,"-5.00",5);
        TextOut(hDC,input_pos[10].x+10, io_pos[10].y-19,"Signals",7);
        TextOut(hDC,output_pos[4].x+60, io_pos[0].y-15,"Channel",7);
        EndPaint(hWnd, &ps);
        break;

        // Sample Rate Spin Control
    case WM_VSCROLL:
        switch (LOWORD(wParam)) {
          case SB_LINEUP :
              sample_rate+=5000;
              if (sample_rate > 50000 ) sample_rate=50000;
              break;
          case SB_LINEDOWN:
              sample_rate-=5000;
              if (sample_rate < 5000 ) sample_rate=5000;
              break;
        }
        wsprintf(n,"%d",sample_rate/1000);
        SetWindowText(hEdit,n);
        break;
    case WM_COMMAND:
        switch(LOWORD(wParam)) {
           case IDM_EXIT:
                SendMessage(hWnd, WM_CLOSE, 0, 0L);
                break;
           case IDC_CH:
           case IDC_CH+1:
           case IDC_CH+2:
           case IDC_CH+3:
           case IDC_CH+4:
           case IDC_CH+5:
           case IDC_CH+6:
           case IDC_CH+7:
               if (!bCheck[wParam-IDC_CH]) {
                  SendMessage(GetDlgItem(hWnd,wParam), BM_SETCHECK, 1, 0);
                  SendMessage(GetDlgItem(hWnd,IDC_CH+channel), BM_SETCHECK, 0, 0);
                  bCheck[channel]=FALSE;
               }
               channel=wParam-IDC_CH;
               bCheck[channel]=TRUE;
               break;

           case IDC_SHOW :
                DialogBox(hInst, "DMABox", hWnd, ShowDMA);
                break;
           case IDC_START:
                if (hMem != NULL) {
                   GlobalUnfix(hMem);
                   GlobalUnlock(hMem);
                   GlobalFree(hMem);
                }
                // allocate a memory for DMA
                data_size=GetDlgItemInt(hWnd,IDC_EDIT1,NULL ,FALSE);
                mem_size=data_size * 2;
                hMem = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,mem_size);

                ai_buf = (U16 *)GlobalLock(hMem);
                if (ai_buf == NULL ) {
                   MessageBox(hWnd,"DMA", "No Memory", MB_OK);
                   SendMessage(hWnd, WM_CLOSE, 0, 0L);
                   break;
                }

                GlobalFix(hMem);

                if ((err=AI_9112_Config(card, TRIG_INT_PACER)) != NoError) {
                      wsprintf(s, "AI_Config Error = %d\n", err);
                      MessageBox(hWnd, s, "Error", MB_OK);
                      SendMessage(hWnd, WM_CLOSE, 0, 0L);
                      break;
                }
                if ((err=AI_AsyncDblBufferMode (card, 1)) != NoError) {
                      wsprintf(s, "AI_AsyncDblBufferMode Error = %d\n", err);
                      MessageBox(hWnd, s, "Error", MB_OK);
                      SendMessage(hWnd, WM_CLOSE, 0, 0L);
                      break;
                }
				if((err= AI_EventCallBack (card, 1, DBEvent, (U32) AI_DBCallBack)) != NoError) {
                      wsprintf(s, "AI_EventCallBack Error = %d\n", err);
                      MessageBox(hWnd, s, "Error", MB_OK);
                      SendMessage(hWnd, WM_CLOSE, 0, 0L);
                      break;
                }
				if((err= AI_EventCallBack (card, 1, AIEnd, (U32) AI_CallBack)) != NoError) {
                      wsprintf(s, "AI_EventCallBack Error = %d\n", err);
                      MessageBox(hWnd, s, "Error", MB_OK);
                      SendMessage(hWnd, WM_CLOSE, 0, 0L);
                      break;
                }
                if ((err=AI_ContReadChannel(card, channel, AD_B_5_V, ai_buf, data_size, (F64)sample_rate, ASYNCH_OP)) != NoError) {
                      wsprintf(s, "AI_ContReadPort Error = %d\n", err);
                      MessageBox(hWnd, s, "Error", MB_OK);
                      SendMessage(hWnd, WM_CLOSE, 0, 0L);
                      break;
                }
                EnableWindow(hStart, FALSE);
                EnableWindow(hStop, TRUE);

                hMainWnd = hWnd;
                clear_op = FALSE;
                //hThread = (HANDLE)_beginthread(ProcessThread, 0, 0);
                MessageBeep(0);
                break;
            case IDC_STOP:
                clear_op = TRUE;
				AI_AsyncClear(card, &count);
                EnableWindow(hStart, TRUE);
                EnableWindow(hStop, FALSE);
                break;
            default:
                return(DefWindowProc(hWnd, message, wParam, lParam));
        }
        break;
    case WM_DESTROY:
        if (card >= 0) Release_Card(card);
        DeleteObject(hBlackBrush);
        DeleteObject(hRedPen);
        GlobalUnfix(hMem);
        GlobalUnlock(hMem);
        GlobalFree(hMem);
        PostQuitMessage(0);
        break;
    default:
        return(DefWindowProc(hWnd, message, wParam, lParam));
  }
  return(NULL);
}

BOOL  PASCAL ShowDMA(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
    int nTabs[2] = {32, 92};
    int i;
    char cBuf[100];
    char string[10]="      ";
    char data_str[3]="  ";
    U16 *pp;
    switch (message) {
        case WM_INITDIALOG:
            hListBox = CreateWindow("LISTBOX", "Index                       Data (Hex)",
                           WS_CHILD | WS_VISIBLE | LBS_HASSTRINGS | LBS_STANDARD | LBS_USETABSTOPS |WS_CAPTION,
                           50, 40, 240, 180, hDlg, 100, hInst, NULL);
            SendMessage(hListBox, LB_RESETCONTENT, 0, 0L);
            SendMessage(hListBox, LB_SETTABSTOPS, 2, (DWORD)(LPINT) nTabs);
            SetDlgItemText(hDlg, DMA_DESCP, "This ListBox can show at most 4000 data");
            itoa( data_size, string, 10);
            SetDlgItemText(hDlg, DMA_SPACE, string);

            pp = (U16 *)ai_buf;
            if (data_size > 4000) data_size = 4000;
            for(i=0;i<data_size; i++) {
                 wsprintf(cBuf, "%5d\t                %04x", i, pp[i]);
                 SendMessage(hListBox, LB_INSERTSTRING, -1, (DWORD)(LPSTR)cBuf);
            }
            ShowWindow(hListBox, SW_SHOW);
            return (TRUE);
        case WM_COMMAND:
            if (LOWORD(wParam) == ID_STOP) {
                DestroyWindow(hListBox);
                EndDialog(hDlg, TRUE);
                return (TRUE);
            }
            break;
    }
    return (FALSE);
}


//void ProcessThread(void* pArg)
void AI_DBCallBack()
{
  HDC hDC;
  U16 adinput;
  U32 i, oldx, oldy, newx, newy;
  U32 tfer_size;

  hDC = GetDC(hMainWnd);
  tfer_size = data_size/2;  //Every time transfer half size of buffer
  AI_AsyncDblBufferTransfer(card, ai_buf);
  SelectObject(hDC, hBlackBrush);
  SelectObject(hDC, hBlackPen);
  Rectangle(hDC, output_pos[15].x+35, io_pos[15].y-2, output_pos[4].x+41, io_pos[0].y+172);
  SelectObject(hDC, hRedPen);
  for (i=0; i<tfer_size; i++) {
         adinput = ai_buf[i]>>4;
         newy = io_pos[0].y+171-(172*(U32)adinput)/4095;
         newx = i*336/tfer_size+output_pos[15].x+35;
         if (i==0)
            MoveToEx(hDC, newx, newy, NULL);
         else
            MoveToEx(hDC, oldx, oldy, NULL);
         LineTo(hDC, newx, newy);
         oldx = newx;
         oldy = newy;
  }
  ReleaseDC(hMainWnd, hDC);
}

void AI_CallBack()
{
  HDC hDC;
  U16 adinput;
  U32 i, oldx, oldy, newx, newy;
  U32 tfer_size;
  hDC = GetDC(hMainWnd);
  tfer_size = data_size/2;  //Every time transfer half size of buffer
  AI_AsyncDblBufferTransfer(card, ai_buf);
  if (!count) count = tfer_size;
  SelectObject(hDC, hBlackBrush);
  SelectObject(hDC, hBlackPen);
  Rectangle(hDC, output_pos[15].x+35, io_pos[15].y-2, output_pos[4].x+41, io_pos[0].y+172);
  SelectObject(hDC, hRedPen);
  for (i=0; i<count; i++) {
         adinput = ai_buf[i]>>4;
         newy = io_pos[0].y+171-(172*(U32)adinput)/4095;
         newx = i*336/tfer_size+output_pos[15].x+35;
         if (i==0)
            MoveToEx(hDC, newx, newy, NULL);
         else
            MoveToEx(hDC, oldx, oldy, NULL);
         LineTo(hDC, newx, newy);
         oldx = newx;
         oldy = newy;
 }
 ReleaseDC(hMainWnd, hDC);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -