|
From: <jfp...@fa...> - 2014-01-05 05:13:19
|
Greetings, After several attempts at using the libusb functions, I am now able to communicate with the hardware I'm developing. A big 'thank you' to all the libusb developers. I'm offering my basic code as a help to other libusb beginning users, particularly to those who, like myself, may not be 'professionally fluent' in C. In the source files in the installed examples/ directory, there's quite a jump in complexity from listdevs.c to, for instance, dpfp.c. My code fits in between, and if someone would choose to include it in the examples/ directory, perhaps with some editing, that is OK with me. BTW, I did find the example files at libusb.org, but their existence could be made more obvious. Just a suggestion. So here's my code. Comments welcomed. Thanks again, John Parsons (If there's another preferred method to post code, please let me know.) // filename: hellodev.c // Minimal routine to transfer data to/from a simple USB device // using libusb-1.0.9 functions. // This example uses interrupt data transfers. // Developed and run on an Ubuntu 12.0.4 system, with libusb-1.0 // and libusb-1.0-dev packages installed via Synaptic Package // Manager. // Compile command used: // gcc hellodev.c -o hellodev $(pkg-config --cflags --libs libusb-1.0) // Run command: sudo ./hellodev // ** Note ** // Must be root in order to successfully open a device. #include <stdio.h> #include <sys/types.h> #include <libusb.h> // Adjust the following values to match the actual device in use: #define VID_MINE 0x265f #define PID_MINE 0xfcb2 #define INTERFACE_NUM 0 #define EPNUM_IN 0x81 // IN endpoint number #define EPNUM_OUT 0x01 // OUT endpoint number #define DOUT_LENGTH 1 // number of bytes of OUT data #define DIN_LENGTH 16 // number of bytes of IN data int main (void) { libusb_device **list; libusb_device_handle *devh = NULL; ssize_t dcount, i; int r, transferred; uint8_t dout[DOUT_LENGTH], din[DIN_LENGTH]; dout[0] = 0x52; // example data out (to be sent to device) r = libusb_init(NULL); // the NULL parameter causes a // default context to be created if(0 == r) { printf("Success libusb_init\n"); //Get list of connected devices and how many there are. dcount = libusb_get_device_list(NULL, &list); printf("Got device list, dcount = %d\n", dcount); if(0 < dcount) { // If 1 or more devices were found... devh = libusb_open_device_with_vid_pid(NULL, 0x265f, 0xfcb2); if (NULL != devh) { printf("Found my device\n"); r = libusb_claim_interface(devh, INTERFACE_NUM); if(0 == r) { printf("Successfully claimed interface...\n"); // Send data to device r = libusb_interrupt_transfer(devh, EPNUM_OUT, dout, DOUT_LENGTH, &transferred, 500); if(0 == r) { printf("Data sent successfully, transferred = %d\n", transferred); } else { printf("Send data failed r = %d, transferred = %d\n", r, transferred); } // Request data from device... // This example device requires that a 'send data' command // byte (0x57) be sent to it, then it will send back 16 bytes. dout[0] = 0x57; r = libusb_interrupt_transfer(devh, EPNUM_OUT, dout, DOUT_LENGTH, &transferred, 500); if(0 == r) { printf("'send data' cmd OK, transferred = %d\n", transferred); } else { printf("'send data' cmd failed r = %d, transferred = %d\n", r, transferred); } // Now actually get the data... r = libusb_interrupt_transfer(devh, EPNUM_IN, din, DIN_LENGTH, &transferred, 500); if(0 == r) { printf("Data received OK, transferred = %d\n", transferred); } else { printf("Data request failed r = %d, transferred = %d\n", r, transferred); } // Release interface when transfers are complete... r = libusb_release_interface(devh, INTERFACE_NUM); if(0 == r) { printf("OK, libusb_release_interface returned 0\n"); } else { printf("Error, libusb_release_interface returned %d\n", r); } } else { printf("Sorry, unable to claim interface, r = %d\n", r); } libusb_close(devh); // must close device before exiting } else { printf("Sorry, libusb_open_dev... returned NULL\n"); } libusb_free_device_list(list, 1); // must free devices before exiting } else { printf("Sorry, unable to get device list, dcount = %d\n", dcount); } libusb_exit(NULL); // 'de-initialize' the default context } else { // libusb_init error printf("Sorry, libusb_init returned r = %d\n", r); } // Print the data that was sent by the device... printf("Received bytes:\n"); for(i = 0; i < DIN_LENGTH; i++) { printf(" %02x", din[i]); } printf("\n Ciao.\n"); } // main |