USB裝置開發---- 基於libusb的無驅設計 .
首先我們簡單介紹下libusb的編譯和安裝(本例OS為CentOS6.0 (核心版本2.6.32-71.el6.i686)。
官網下載libusb安裝包(本專案中下載的版本為libusb-1.0.8.tar),解壓後,進入libusb-1.0.8資料夾目錄中,依次執行如下指令:
a:執行./configure來配置安裝包。
b:執行make編譯該安裝包。
c:執行make install安裝。
libusb安裝好之後,就可以呼叫它的API來訪問我們的裝置了,先簡單介紹下幾個主要的API
1:
[cpp] view plaincopyprint?- API_EXPORTED
API_EXPORTED int libusb_init(libusb_context **context)
該函式進行libusb的初始化,必須最先呼叫。
libusb_context *ctx :可以為NULL。
2:
[cpp] view plaincopyprint?- API_EXPORTED libusb_device_handle *libusb_open_device_with_vid_pid(
- libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
API_EXPORTED libusb_device_handle *libusb_open_device_with_vid_pid(
libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
該函式可直接根據指定的USB裝置的Vendor ID 和 Product ID來找到相應的裝置並開啟它,將裝置控制代碼返回給呼叫者。
libusb_context *ctx :可以為NULL。
uint16_t vendor :生產商ID。
uint16_t product_id: 裝置ID。
3:
[cpp] view plain- int libusb_control_transfer(libusb_device_handle *dev_handle,
- uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
- unsigned char *data, uint16_t wLength, unsigned int timeout)
int libusb_control_transfer(libusb_device_handle *dev_handle,
uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
unsigned char *data, uint16_t wLength, unsigned int timeout)
控制傳輸操作
libusb_device_handle *dev_handle: 裝置控制代碼。
uint8_t bmRequestType: 對應Setup Packet包中的bmRequestType欄位。
uint8_t bRequest: 對應Setup Packet包中的bRequest欄位。
uint16_t wValue: 對應Setup Packet包中的wValue欄位。
uint16_t wIndex: 對應Setup Packet包中的wIndex欄位。
unsigned char *data: 資料。
uint16_t wLength: 資料大小。
unsigned int timeout: 超時閥值。
4:
[cpp] view plaincopyprint?- API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
- unsigned char endpoint, unsigned char *data, int length, int *transferred,
- unsigned int timeout)
API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length, int *transferred,
unsigned int timeout)
塊傳輸操作
libusb_device_handle *dev_handle: 裝置控制代碼。
unsigned char endpoint: 端點編號。
unsigned char *data: 資料。
int length: 資料塊長度。
int *transferred: 實際傳輸資料塊大小。
unsigned int timeout: 超時閥值。
5:
[cpp] view plaincopyprint?- API_EXPORTED int libusb_interrupt_transfer(
- struct libusb_device_handle *dev_handle, unsigned char endpoint,
- unsigned char *data, int length, int *transferred, unsigned int timeout)
API_EXPORTED int libusb_interrupt_transfer(
struct libusb_device_handle *dev_handle, unsigned char endpoint,
unsigned char *data, int length, int *transferred, unsigned int timeout)
中斷傳輸操作
libusb_device_handle *dev_handle: 裝置控制代碼。
unsigned char endpoint: 端點編號。
unsigned char *data: 資料。
int length: 資料塊長度。
int *transferred: 實際傳輸資料塊大小。
unsigned int timeout: 超時閥值。
下面我們看看如何開啟裝置:
- int rv = 0;
- rv = libusb_init(NULL);
- if(rv < 0) {
- printf("*** initial USB lib failed! \n");
- return -1;
- }
- //open the usb device
- g_usb_handle = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_ID, USB_PRODUCT_ID);
- if(g_usb_handle == NULL) {
- printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");
- return -1;
- }
int rv = 0;
rv = libusb_init(NULL);
if(rv < 0) {
printf("*** initial USB lib failed! \n");
return -1;
}
//open the usb device
g_usb_handle = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_ID, USB_PRODUCT_ID);
if(g_usb_handle == NULL) {
printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");
return -1;
}
libusb的使用非常方便,指定ID後,相應裝置被開啟,得到裝置控制代碼之後,即可呼叫各種傳輸操作API對我們的USB裝置進行訪問了。