【目的】
- 使用Qt 透過廠商所提供的Dll 控制 USB IO card。
【硬體】
- Usb IO card
http://www.icci.com.tw/web/MdFront?mdId=MD0000002307001371 - Devices Mananger
- Connect to LED on Bread board
【程式】
- 使用 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(); }
- 透過QLibrary方式(省略)
【研究】
- 只看到提供Windows 系列的 Driver,不確定 Mac/Linux 是否能用。
- AVR 系列好像有可以直接走 USB Protocol 和 PC 對連,可能會比目前所用的架構簡單。
- 硬體多加一棵 USB2UART 走 RS232 方式可能會比較能達到跨平台要求。
【參考】
- USB 8051/AVR programmer
http://www.8051projects.info/blogs/post/USB-8051AVR-programmer.aspx - USB DIY 講座 (六) --- USB I/O
http://chamberplus.myweb.hinet.net/usb.htm