/* Interface - prog. */ static int usbasp_open(PROGRAMMER * pgm, char * port) { /* usb_init will be done in usbOpenDevice */ if (usbOpenDevice(&PDATA(pgm)->usbhandle, pgm->usbvid, pgm->usbvendor, pgm->usbpid, pgm->usbproduct) != 0) { /* try alternatives */ if(strcasecmp(ldata(lfirst(pgm->id)), "usbasp") == 0) { /* for id usbasp autodetect some variants */ if(strcasecmp(port, "nibobee") == 0) { fprintf(stderr, "%s: warning: Using \"-C usbasp -P nibobee\" is deprecated," "use \"-C nibobee\" instead.\n", progname); if (usbOpenDevice(&PDATA(pgm)->usbhandle, USBASP_NIBOBEE_VID, "www.nicai-systems.com", USBASP_NIBOBEE_PID, "NIBObee") != 0) { fprintf(stderr, "%s: error: could not find USB device " "\"NIBObee\" with vid=0x%x pid=0x%x\n", progname, USBASP_NIBOBEE_VID, USBASP_NIBOBEE_PID); return -1; } return 0; } /* check if device with old VID/PID is available */ if (usbOpenDevice(&PDATA(pgm)->usbhandle, USBASP_OLD_VID, "www.fischl.de", USBASP_OLD_PID, "USBasp") == 0) { /* found USBasp with old IDs */ fprintf(stderr, "%s: Warning: Found USB device \"USBasp\" with " "old VID/PID! Please update firmware of USBasp!\n", progname); return 0; } /* original USBasp is specified in config file, so no need to check it again here */ /* no alternative found => fall through to generic error message */ } fprintf(stderr, "%s: error: could not find USB device with vid=0x%x pid=0x%x", progname, pgm->usbvid, pgm->usbpid); if (pgm->usbvendor[0] != 0) { fprintf(stderr, " vendor='%s'", pgm->usbvendor); } if (pgm->usbproduct[0] != 0) { fprintf(stderr, " product='%s'", pgm->usbproduct); } fprintf(stderr,"\n"); return -1; } return 0; }
// Load the standard FPGALink firmware into the FX2 at currentVid/currentPid. DLLEXPORT(FLStatus) flLoadStandardFirmware( const char *curVidPid, const char *newVidPid, const char **error) { FLStatus flStatus, retVal = FL_SUCCESS; struct Buffer ramBuf = {0,}; BufferStatus bStatus; FX2Status fxStatus; struct USBDevice *device = NULL; USBStatus uStatus; uint16 newVid, newPid, newDid; CHECK_STATUS( !usbValidateVidPid(newVidPid), FL_USB_ERR, cleanup, "flLoadStandardFirmware(): The supplied VID:PID:DID \"%s\" is invalid; it should look like 1D50:602B or 1D50:602B:0001", newVidPid); newVid = (uint16)strtoul(newVidPid, NULL, 16); newPid = (uint16)strtoul(newVidPid+5, NULL, 16); newDid = (uint16)((strlen(newVidPid) == 14) ? strtoul(newVidPid+10, NULL, 16) : 0x0000); uStatus = usbOpenDevice(curVidPid, 1, 0, 0, &device, error); CHECK_STATUS(uStatus, FL_USB_ERR, cleanup, "flLoadStandardFirmware()"); bStatus = bufInitialise(&ramBuf, 0x4000, 0x00, error); CHECK_STATUS(bStatus, FL_ALLOC_ERR, cleanup, "flLoadStandardFirmware()"); flStatus = copyFirmwareAndRewriteIDs( &ramFirmware, newVid, newPid, newDid, &ramBuf, error); CHECK_STATUS(flStatus, flStatus, cleanup, "flLoadStandardFirmware()"); fxStatus = fx2WriteRAM(device, ramBuf.data, (uint32)ramBuf.length, error); CHECK_STATUS(fxStatus, FL_FX2_ERR, cleanup, "flLoadStandardFirmware()"); cleanup: bufDestroy(&ramBuf); if ( device ) { usbCloseDevice(device, 0); } return retVal; }
// Load custom firmware (.hex) into the FX2's RAM DLLEXPORT(FLStatus) flLoadCustomFirmware( const char *curVidPid, const char *fwFile, const char **error) { FLStatus retVal = FL_SUCCESS; struct Buffer fwBuf = {0,}; BufferStatus bStatus; FX2Status fxStatus; struct USBDevice *device = NULL; USBStatus uStatus; const char *const ext = fwFile + strlen(fwFile) - 4; const bool isHex = (strcmp(".hex", ext) == 0) || (strcmp(".ihx", ext) == 0); CHECK_STATUS( !isHex, FL_FILE_ERR, cleanup, "flLoadCustomFirmware(): Filename should have .hex or .ihx extension"); uStatus = usbOpenDevice(curVidPid, 1, 0, 0, &device, error); CHECK_STATUS(uStatus, FL_USB_ERR, cleanup, "flLoadCustomFirmware()"); bStatus = bufInitialise(&fwBuf, 8192, 0x00, error); CHECK_STATUS(bStatus, FL_ALLOC_ERR, cleanup, "flLoadCustomFirmware()"); bStatus = bufReadFromIntelHexFile(&fwBuf, NULL, fwFile, error); CHECK_STATUS(bStatus, FL_FILE_ERR, cleanup, "flLoadCustomFirmware()"); fxStatus = fx2WriteRAM(device, fwBuf.data, (uint32)fwBuf.length, error); CHECK_STATUS(fxStatus, FL_FX2_ERR, cleanup, "flLoadCustomFirmware()"); cleanup: bufDestroy(&fwBuf); if ( device ) { usbCloseDevice(device, 0); } return retVal; }
void RF24ComVUsb::initialize(void) { const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; int vid, pid; int ret; uint8_t buffer[128]; usb_init(); vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0) fatal(-1, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); // exmptiy thei interrupt queue DPRINT("Emptying usb interrupt queue..."); ret = usb_interrupt_read(handle, USB_ENDPOINT_IN | 1, (char *)buffer, sizeof(buffer), 100); DPRINT("done, returned %d.\n", ret); DPRINT("Verifying protocol version\n"); buffer[0] = 4; buffer[1] = 1; buffer[2] = RF24_getProtocolVersion; buffer[3] = 0; sendRequest(buffer); getResponse(buffer); if (buffer[4] != RF24REMOTE_PROTOCOL_VERSION) fatal(-1, "usb client returned wrong protocol version. Expected:%d, received:%d\n", RF24REMOTE_PROTOCOL_VERSION, buffer[4]); }
int main(int argc, const char *argv[]) { int retVal = 0; struct USBDevice *device = NULL; const char *error = NULL; const char *vp; USBStatus uStatus; if ( argc != 2 ) { fprintf(stderr, "Synopsis: %s <VID:PID>\n", argv[0]); FAIL(1, cleanup); } vp = argv[1]; uStatus = usbInitialise(0, &error); CHECK_STATUS(uStatus, 2, cleanup); uStatus = usbOpenDevice(vp, 1, 0, 0, &device, &error); CHECK_STATUS(uStatus, 3, cleanup); uStatus = usbPrintConfiguration(device, stdout, &error); CHECK_STATUS(uStatus, 4, cleanup); cleanup: if ( device ) { usbCloseDevice(device, 0); } if ( error ) { fprintf(stderr, "%s: %s\n", argv[0], error); errFree(error); } return retVal; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; char buffer[50] = ""; unsigned char request; int vid, pid, nBytes; usb_init(); if(argc < 2){ /* we need at least one argument */ printf("Arguments Missing"); exit(1); } request = atoi(argv[1]); /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } /* Since we use only control endpoint 0, we don't need to choose a * configuration and interface. Reading device descriptor and setting a * configuration and interface is done through endpoint 0 after all. * However, newer versions of Linux require that we claim an interface * even for endpoint 0. Enable the following code if your operating system * needs it: */ #if 0 int retries = 1, usbConfiguration = 1, usbInterface = 0; if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } /* now try to claim the interface and detach the kernel HID driver on * Linux and other operating systems which support the call. */ while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); } #endif } #endif nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, request, 0, 0, (char *)buffer, sizeof(buffer), 5000); if(nBytes < 0) printf("error in USB control transfer: %s\n", usb_strerror()); usb_close(handle); return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; unsigned char buffer[8]; int nBytes; if(argc < 2) { usage(argv[0]); exit(1); } usb_init(); if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, iMANUFACTURER, USBDEV_SHARED_PRODUCT, iPRODUCT) != 0) { fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", iPRODUCT, USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); exit(1); } /* We have searched all devices on all busses for our USB device above. Now * try to open it and perform the vendor specific control operations for the * function requested by the user. */ if(strcmp(argv[1], "in") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, FUNC_READ_BUFFER, 0, 0, (char *)buffer, sizeof(buffer), 5000); if(nBytes < 1) { if(nBytes < 0) fprintf(stderr, "USB error: %s\n", usb_strerror()); fprintf(stderr, "only %d bytes status received\n", nBytes); exit(1); } int in = buffer[0]; printf("in = 0x%x\n", in); } else { int outValue; if(argc < 3) { usage(argv[0]); exit(1); } outValue = atoi(argv[2]); if (outValue >= (1<<NUMBER_OF_BITS) || outValue < 0) { fprintf(stderr, "Sorry, there are only %d Bits\n", NUMBER_OF_BITS); exit(1); } if(argc > 3) { usage(argv[0]); exit(1); } if(strcmp(argv[1], "out") == 0) { //outValue = (outValue << 8); nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, FUNC_WRITE_BUFFER, outValue, 0, (char *)buffer, sizeof(buffer), 5000); } else { nBytes = 0; usage(argv[0]); exit(1); } if(nBytes < 0) fprintf(stderr, "USB error: %s\n", usb_strerror()); } usb_close(handle); return 0; }
int initUsbLib() { if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, "www.obdev.at", USBDEV_SHARED_PRODUCT, "PowerSwitch") != 0) { fprintf(stderr, "Could not find USB device \"metaboard\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); exit(1); } }
int usbrelay_init() { usb_init(); if(usbOpenDevice(&usb_handle, USBDEV_SHARED_VENDOR, "johannes.krude.de", USBDEV_SHARED_PRODUCT, "usb-Relay") != 0){ snprintf(str_error, sizeof(str_error), "Could not find USB device \"usb-Relay\" with vid=0x%x pid=0x%x", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); return(1); } }
bool CUsbIo::open() { int res = usbOpenDevice( &(pd->handle), VENDOR_ID, NULL, PRODUCT_ID, NULL, NULL, NULL, NULL ); return (res == 0); }
littleWire* littleWire_connect() { littleWire *tempHandle = NULL; usb_init(); usbOpenDevice(&tempHandle, VENDOR_ID, "*", PRODUCT_ID, "*", "*", NULL, NULL ); return tempHandle; }
/* Interface - prog. */ static int usbasp_open(PROGRAMMER * pgm, char * port) { #ifdef USE_LIBUSB_1_0 libusb_init(&ctx); #else usb_init(); #endif if(strcasecmp(port, "nibobee") == 0) { if (usbOpenDevice(&PDATA(pgm)->usbhandle, USBASP_NIBOBEE_VID, "www.nicai-systems.com", USBASP_NIBOBEE_PID, "NIBObee") != 0) { fprintf(stderr, "%s: error: could not find USB device " "\"NIBObee\" with vid=0x%x pid=0x%x\n", progname, USBASP_NIBOBEE_VID, USBASP_NIBOBEE_PID); return -1; } } else if (usbOpenDevice(&PDATA(pgm)->usbhandle, USBASP_SHARED_VID, "www.fischl.de", USBASP_SHARED_PID, "USBasp") != 0) { /* check if device with old VID/PID is available */ if (usbOpenDevice(&PDATA(pgm)->usbhandle, USBASP_OLD_VID, "www.fischl.de", USBASP_OLD_PID, "USBasp") != 0) { /* no USBasp found */ fprintf(stderr, "%s: error: could not find USB device " "\"USBasp\" with vid=0x%x pid=0x%x\n", progname, USBASP_SHARED_VID, USBASP_SHARED_PID); return -1; } else { /* found USBasp with old IDs */ fprintf(stderr, "%s: Warning: Found USB device \"USBasp\" with " "old VID/PID! Please update firmware of USBasp!\n", progname); } } return 0; }
// Open a connection, get device status & sanity-check it. // DLLEXPORT(FLStatus) flOpen(const char *vp, struct FLContext **handle, const char **error) { FLStatus retVal = FL_SUCCESS, fStatus; USBStatus uStatus; uint8 statusBuffer[16]; struct FLContext *newCxt = (struct FLContext *)calloc(sizeof(struct FLContext), 1); uint8 progEndpoints, commEndpoints; CHECK_STATUS(!newCxt, FL_ALLOC_ERR, cleanup, "flOpen()"); uStatus = usbOpenDevice(vp, 1, 0, 0, &newCxt->device, error); CHECK_STATUS(uStatus, FL_USB_ERR, cleanup, "flOpen()"); fStatus = getStatus(newCxt, statusBuffer, error); CHECK_STATUS(fStatus, fStatus, cleanup, "flOpen()"); CHECK_STATUS( statusBuffer[0] != 'N' || statusBuffer[1] != 'E' || statusBuffer[2] != 'M' || statusBuffer[3] != 'I', FL_PROTOCOL_ERR, cleanup, "flOpen(): Device at %s not recognised", vp); CHECK_STATUS( !statusBuffer[6] && !statusBuffer[7], FL_PROTOCOL_ERR, cleanup, "flOpen(): Device at %s supports neither NeroProg nor CommFPGA", vp); progEndpoints = statusBuffer[6]; commEndpoints = statusBuffer[7]; if ( progEndpoints ) { newCxt->isNeroCapable = true; newCxt->progOutEP = (progEndpoints >> 4); newCxt->progInEP = (progEndpoints & 0x0F); } if ( commEndpoints ) { newCxt->isCommCapable = true; newCxt->commOutEP = (commEndpoints >> 4); newCxt->commInEP = (commEndpoints & 0x0F); } newCxt->firmwareID = (uint16)( (statusBuffer[8] << 8) | statusBuffer[9] ); newCxt->firmwareVersion = (uint32)( (statusBuffer[10] << 24) | (statusBuffer[11] << 16) | (statusBuffer[12] << 8) | statusBuffer[13] ); newCxt->chunkSize = 0x10000; // default maximum libusbwrap chunk size *handle = newCxt; return retVal; cleanup: if ( newCxt ) { if ( newCxt->device ) { usbCloseDevice(newCxt->device, 0); } free((void*)newCxt); } *handle = NULL; return retVal; }
int8_t CUsbDevice::connect(){ if (usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf( stderr, "USB device \"%s\" with vid=0x%x pid=0x%x\n NOT FOUND\n", product, vid, pid); connected = 0; }else connected = USBRETRY; return connected; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; int nBytes = 0; char buffer[256]; if(argc < 2) { printf("Usage:\n"); printf("usbtext.exe on\n"); printf("usbtext.exe off\n"); printf("usbtext.exe out\n"); printf("usbtext.exe write\n"); printf("usbtext.exe sendstr\n"); exit(1); } handle = usbOpenDevice(0x16C0, "obdev.at", 0x05DC, "USBMANOJ"); if(handle == NULL) { fprintf(stderr, "Could not find USB device!n"); exit(1); } if(strcmp(argv[1], "on") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000); } else if(strcmp(argv[1], "off") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000); }else if(strcmp(argv[1], "out") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000); printf("Got %d bytes: %sn", nBytes, buffer); } else if(strcmp(argv[1], "write") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_WRITE, 'T' + ('E' << 8), 'S' + ('T' << 8), (char *)buffer, sizeof(buffer), 5000); } else if(strcmp(argv[1], "sendstr") == 0 && argc > 2) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USB_DATA_IN, 0, 0, argv[2], strlen(argv[2])+1, 5000); } if(nBytes < 0) fprintf(stderr, "USB error: %sn", usb_strerror()); usb_close(handle); return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}; const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}; char product[] = {USB_CFG_DEVICE_NAME, 0}; char buffer[4]; int cnt, vid, pid; usb_init(); if(argc < 2){ /* we need at least one argument */ usage(argv[0]); exit(1); } /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } /*if(strcasecmp(argv[1], "status") == 0){ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000); if(cnt < 1){ if(cnt < 0){ fprintf(stderr, "USB error: %s\n", usb_strerror()); }else{ fprintf(stderr, "only %d bytes received.\n", cnt); } }else{ printf("LED is %s\n", buffer[0] ? "on" : "off"); } } else*/ if(strcasecmp(argv[1], "rgb") == 0) { int r = atoi(argv[2]); int g = atoi(argv[3]); int b = atoi(argv[4]); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_RED, r, 0, buffer, 0, 5000); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_GREEN, g, 0, buffer, 0, 5000); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_BLUE, b, 0, buffer, 0, 5000); } else{ usage(argv[0]); exit(1); } usb_close(handle); return 0; }
static void avrdoper_open(char *port, long baud, union filedescriptor *fdp) { int rval; char *vname = "obdev.at"; char *devname = "AVR-Doper"; rval = usbOpenDevice(fdp, USB_VENDOR_ID, vname, USB_PRODUCT_ID, devname, 1); if(rval != 0){ fprintf(stderr, "%s: avrdoper_open(): %s\n", progname, usbErrorText(rval)); exit(1); } }
bool UsbIo::open() { QMutexLocker lock( &m_openMutex ); pd->handle = 0; int res = usbOpenDevice( &(pd->handle), PD::VENDOR_ID, NULL, PD::PRODUCT_ID, NULL, NULL, NULL, NULL ); return (res == 0); }
bool init_led_controler() { usb_init(); /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); return false; } return true; }
static int avrdoper_open(char *port, union pinfo pinfo, union filedescriptor *fdp) { int rval; char *vname = "obdev.at"; char *devname = "AVR-Doper"; rval = usbOpenDevice(fdp, USB_VENDOR_ID, vname, USB_PRODUCT_ID, devname, 1); if(rval != 0){ avrdude_message(MSG_INFO, "%s: avrdoper_open(): %s\n", progname, usbErrorText(rval)); return -1; } return 0; }
int UsbWidget::findDevice() { if ( handle ) return 1; int vid = rawVid[1] * 256 + rawVid[0]; int pid = rawPid[1] * 256 + rawPid[0]; if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0) { qDebug("[usb] could not find USB device \"%s\" with vid=0x%x pid=0x%x", product, vid, pid); handle = NULL; return 0; } qDebug("[usb] found device"); return 1; }
usb_dev_handle *device_open(void) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}; const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}; char product[] = {USB_CFG_DEVICE_NAME, 0}; int vid, pid; /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = (rawVid[1] << 8) | rawVid[0]; pid = (rawPid[1] << 8) | rawPid[0]; if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) == 0){ return handle; } fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); return NULL; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}; const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}; char product[] = {USB_CFG_DEVICE_NAME, 0}; char buffer[4]; int cnt, vid, pid; usb_init(); if(argc < 2){ /* we need at least one argument */ usage(argv[0]); exit(1); } /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } if(argc == 4) { int r = atoi(argv[1]); int g = atoi(argv[2]); int b = atoi(argv[3]); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_RED, r, 0, buffer, 0, 5000); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_GREEN, g, 0, buffer, 0, 5000); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_PULSE, b, 0, buffer, 0, 5000); } else{ usage(argv[0]); exit(1); } usb_close(handle); return 0; }
MyDriver::MyDriver() { const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; int vid, pid; handle=NULL; usb_init(); // compute VID/PID from usbconfig.h so that there is a central source of information vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; // The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } // Since we use only control endpoint 0, we don't need to choose a // configuration and interface. Reading device descriptor and setting a // configuration and interface is done through endpoint 0 after all. // However, newer versions of Linux require that we claim an interface // even for endpoint 0. Enable the following code if your operating system // needs it #if 0 int retries = 1, usbConfiguration = 1, usbInterface = 0; if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } // now try to claim the interface and detach the kernel HID driver on // Linux and other operating systems which support the call. while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); } #endif } #endif }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; unsigned char buffer[8]; int nBytes; usb_init(); if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, 0, USBDEV_SHARED_PRODUCT, 0) != 0){ fprintf(stderr, "Could not find USB device \"PowerSwitch\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); exit(1); } /* We have searched all devices on all busses for our USB device above. Now * try to open it and perform the vendor specific control operations for the * function requested by the user. */ DWORD time = GetTickCount(); int count = 1000; for(int i=0;i<count;i++) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 0, 0, 0, (char *)buffer, sizeof(buffer), 5000); if(nBytes < 2){ if(nBytes < 0) fprintf(stderr, "USB error: %s\n", usb_strerror()); fprintf(stderr, "only %d bytes received in iteration %d\n", nBytes, i); continue; } std::cout << (char *)buffer << std::endl; } time = GetTickCount()-time; std::cout << (float)count*8 / time * 1000.f << std::endl; std::ofstream file("123.txt"); file << time; printf("test succeeded\n"); usb_close(handle); system("pause"); return 0; }
static int usbdevice_open (usb_dev_handle **device) #endif { #ifdef USE_LIBUSB_1_0 libusb_init(NULL); #else usb_init(); #endif if (usbOpenDevice(device, USBDEVICE_SHARED_VID, "Embedded Creations", USBDEVICE_SHARED_PID, "libusbtest") != 0) { /* no USBasp found */ fprintf(stderr, "error: could not find USB device " "\"libusbtest\" with vid=0x%x pid=0x%x\n", USBDEVICE_SHARED_VID, USBDEVICE_SHARED_PID); return -1; } return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; int nBytes = 0; char buffer[256]; if(argc < 2) { printf("Usage:\n"); printf("somethingswrong_controller.exe on\n"); printf("somethingswrong_controller.exe off\n"); exit(1); } handle = usbOpenDevice(0x16C0, "Krzysztof Walach", 0x05DC, "Something's Wrong!"); if(handle == NULL) { fprintf(stderr, "Could not find 'Something's Wrong!' device!\n"); exit(1); } if(strcmp(argv[1], "on") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_PB0_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000); } else if(strcmp(argv[1], "off") == 0) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_PB0_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000); } if(nBytes < 0) fprintf(stderr, "USB error: %s\n", usb_strerror()); usb_close(handle); return 0; }
/** * Main function. Initializes the USB-device, parses commandline-parameters and * calls the functions that communicate with the device. * \param argc Number of arguments. * \param argv Arguments. * \return Error code. */ int main(int argc, char **argv) { usb_dev_handle *handle = NULL; if (argc < 2) { usage(argv[0]); exit(1); } usb_init(); if (usbOpenDevice (&handle, USBDEV_SHARED_VENDOR, "www.schatenseite.de", USBDEV_SHARED_PRODUCT, "USB-LED-Fader") != 0) { fprintf(stderr, "Could not find USB device \"USB-LED-Fader\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); exit(1); } /* We have searched all devices on all busses for our USB device above. Now * try to open it and perform the vendor specific control operations for the * function requested by the user. */ if (strcmp(argv[1], "test") == 0) { dev_test(handle, argc, argv); } else if (strcmp(argv[1], "set") == 0) { dev_set(handle, argc, argv); } else if (strcmp(argv[1], "clear") == 0) { dev_clear(handle, argc, argv); } else if (strcmp(argv[1], "status") == 0) { dev_status(handle, argc, argv); } else if (strcmp(argv[1], "reset") == 0) { dev_reset(handle, argc, argv); } else if (strcmp(argv[1], "show") == 0) { dev_show(handle, argc, argv); } else { usage(argv[0]); exit(1); } usb_close(handle); return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = "Atmel", product[] = "JTAGICE mkII"; char buffer[1000]; int cnt, vid, pid; usb_init(); /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0) { fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); system("pause"); exit(1); } try{ usb_set_configuration(handle, 1); usb_claim_interface(handle, 0); int i, count=0, total=0; DWORD time = GetTickCount(); char send[] = "1234567890abcdef"; char recv[256]; //ComPort com; int res; for(i = 0; i < 1000; i++) { //if(!com.WriteBuffer(send, 16)) //{ // std::cout << "Com write error" << std::endl; //} std::cout << i << std::endl; res = usb_bulk_read(handle, 0x82, recv, 128, 500); if(res < 0) { std::cout << "bulk = " << res << std::endl << usb_strerror() << std::endl; } else { recv[res]=0; std::cout << "res = " << res << " \t" << recv << std::endl; total+=res; } //Sleep(100); } time = GetTickCount() - time; std::cout << "transfer compleate:\n"; std::cout << total << " bytes in " << (float)time/1000.f << "seconds" << std::endl; std::cout << (float)total/(float)time*1000.f << "bytes per second" << std::endl; } catch(std::exception &e) { std::cout << e.what()<< std::endl; } usb_close(handle); system("pause"); return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}; char product[] = {USB_CFG_DEVICE_NAME, 0}; int cnt, vid, pid; usb_init(); if(argc < 1){ /* we need at least one argument */ usage("set-lcd"); exit(1); } else if (argc >= 2 && (strcmp(argv[1], "-h") == 0)) { usage(argv[0]); exit(0); } /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } /* Since we use only control endpoint 0, we don't need to choose a * configuration and interface. Reading device descriptor and setting a * configuration and interface is done through endpoint 0 after all. * However, newer versions of Linux require that we claim an interface * even for endpoint 0. Enable the following code if your operating system * needs it: */ #if 0 int retries = 1, usbConfiguration = 1, usbInterface = 0; if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } /* now try to claim the interface and detach the kernel HID driver on * Linux and other operating systems which support the call. */ while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); } #endif } #endif if(argc > 1) { int size = getSizeOfDisplayArgs(argc, argv); char* allargs = malloc(size); buildConcatenatedDisplayArgString(allargs, argc, argv); fprintf(stderr, "Setting LCD text to: %s\n", allargs); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SHOW_MSG, size - 1, 0, allargs, size - 1, 5000); free(allargs); if(cnt < 0){ fprintf(stderr, "USB error: %s\n", usb_strerror()); } }else{ fprintf(stderr, "Clearing LCD\n"); cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_CLEAR, 0, 0, NULL, 0, 5000); if(cnt < 0){ fprintf(stderr, "USB error: %s\n", usb_strerror()); } } usb_close(handle); return 0; }