【Qt】Usb IO card


【目的】

  • 使用Qt 透過廠商所提供的Dll 控制 USB IO card。

【硬體】

【程式】

  1. 使用 WinAPI方式載入Dll,並將 LED 點亮(送低電位)
    typedef DWORD (CALLBACK* OpenUSBDeviceFunc)(BYTE);
    typedef DWORD (CALLBACK* CloseDeviceFunc)(BYTE);
    typedef DWORD (CALLBACK* WriteIODataFunc)(BYTE,LPBYTE,DWORD,LPDWORD);
    
    HINSTANCE hDLL; // Handle to DLL
    OpenUSBDeviceFunc   lpfnOpenUSBDevice; // Function pointer
    CloseDeviceFunc     lpfnCloseDevice;
    WriteIODataFunc     lpfnWriteIOData;
    
    DWORD uReturnVal;
    
    hDLL = LoadLibrary(L"USBIODLL.dll");
    if (hDLL == NULL)
    {
        QMessageBox::information(this,tr("info"),tr("Load Dll Fail!"));
        goto exit;
    }
    lpfnOpenUSBDevice = (OpenUSBDeviceFunc)GetProcAddress(hDLL,"OpenUSBDevice");
    lpfnCloseDevice = (CloseDeviceFunc)GetProcAddress(hDLL,"CloseDevice");
    lpfnWriteIOData = (WriteIODataFunc)GetProcAddress(hDLL,"WriteIOData");
    if ((!lpfnOpenUSBDevice) || (!lpfnCloseDevice) || (!lpfnWriteIOData))
    {
      // handle the error
      QMessageBox::information(this,tr("info"),tr("GetProcAddress Fail!"));
      goto free;
    }
    
    // call the function
    uReturnVal = lpfnOpenUSBDevice(0);
    if (uReturnVal) {
        QMessageBox::information(this,tr("info"),tr("Open Fail!"));
        goto free;
    }
    {
    BYTE  pSrc[4];
    DWORD dwRet;
    WORD  wData = 0x0;
    pSrc[0] = (BYTE)wData & 0x00FF;
    pSrc[1] = (BYTE)((wData & 0xFF00) >>8);
    uReturnVal = lpfnWriteIOData(0, pSrc, 2, &dwRet);
    if (uReturnVal) {
      QMessageBox::information(this,tr("info"),tr("Write Fail!"));
      goto free;
    }
    }
    uReturnVal = lpfnCloseDevice(0);
    if (uReturnVal) {
      QMessageBox::information(this,tr("info"),tr("Close Fail!"));
      goto free;
    }
    free:
    FreeLibrary(hDLL);
    exit:
    if (uReturnVal) {
        // TODO:
        //ShowErrorCode();
    }
  2. 透過QLibrary方式(省略)

【研究】

  • 只看到提供Windows 系列的 Driver,不確定 Mac/Linux 是否能用。
  • AVR 系列好像有可以直接走 USB Protocol 和 PC 對連,可能會比目前所用的架構簡單。
  • 硬體多加一棵 USB2UART 走 RS232 方式可能會比較能達到跨平台要求。

【參考】

 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com