From c2529f42816fc988f1b48650d7ecdb678b3cea77 Mon Sep 17 00:00:00 2001 From: pavel Date: Fri, 21 Nov 2014 05:48:55 +0200 Subject: [PATCH] Added ID= arg to specify the "serial number". --- commandline/README-hidusb-relay-cmd.txt | 21 ++++++-- commandline/hiddata.c | 2 +- commandline/usbrelay-cmd.c | 71 +++++++++++++++++-------- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/commandline/README-hidusb-relay-cmd.txt b/commandline/README-hidusb-relay-cmd.txt index dfe6e66..617c708 100644 --- a/commandline/README-hidusb-relay-cmd.txt +++ b/commandline/README-hidusb-relay-cmd.txt @@ -1,13 +1,25 @@ Command line utility for Chinese USB HID relays (USB 1.1 HID devices, VEN=5824 (0x16c0) DEV=1503 (0x05DF), based on the V-USB project) -Rev. 1.1 (18-Nov-2014) +Rev. 1.2 (20-Nov-2014) Usage: - usbrelay-cmd ON|OFF -- turn the relay ON or OFF - usbrelay-cmd STATE -- print state of relays and the "unique ID" + usbrelay-cmd [ID=XXXXX] ON|OFF -- turn the relay ON or OFF - where is the relay number: 1-2 or "*" for all + where is the relay number: 1-2 or "*" for all + + usbrelay-cmd STATE -- print state of one relay device with its "unique ID" + usbrelay-cmd ENUM -- print state of all devices and their "unique IDs" + +Example: + usbrelay-cmd id=ABCDE on 1 + +Note: Parameter ID=XXXXX specifies one relay device if several are connected. +The ID string is case sensitive. +The Enum command lists IDs of all connected devices. + +Without the ID= parameter, if several devices are connected, the program uses any one of them. + Each relay has two contact pairs: Normally Open and Normally Closed. The OFF state is the "normal" state, when the red LED is off, and the Normally Open contacts are disconnected. @@ -16,7 +28,6 @@ The ON state is when the red LED is on, and the Normally Open contacts are conne Note: In the original s/w readme, "Open" means ON, "Close" means OFF. Currently tested with 1 and 2-relay devices. -Finds the FIRST AVAILABLE RELAY device! Does not address by the "unique ID" yet! SOURCE based on a V-USB HID example. LICENSE: TBD! diff --git a/commandline/hiddata.c b/commandline/hiddata.c index ffa8824..9c8af52 100644 --- a/commandline/hiddata.c +++ b/commandline/hiddata.c @@ -132,7 +132,7 @@ int usbhidEnumDevices(int vendor, int product, } deviceAttributes.Size = sizeof(deviceAttributes); HidD_GetAttributes(handle, &deviceAttributes); - DEBUG_PRINT(("device attributes: vid=%d pid=%d\n", deviceAttributes.VendorID, deviceAttributes.ProductID)); + DEBUG_PRINT(("device attributes: vid=%d pid=%d ver=%4.4X\n", deviceAttributes.VendorID, deviceAttributes.ProductID, deviceAttributes.VersionNumber)); if(deviceAttributes.VendorID != vendor || deviceAttributes.ProductID != product) continue; /* skip this device */ diff --git a/commandline/usbrelay-cmd.c b/commandline/usbrelay-cmd.c index edb1be6..f166ab9 100644 --- a/commandline/usbrelay-cmd.c +++ b/commandline/usbrelay-cmd.c @@ -1,9 +1,7 @@ // Chinese USB/HID relay command line tool: // -// pa02 19-Nov-2014 supports 1,2,4 - relay devices -// Currently finds the 1st matching device by ven,dev, product name string. -// TODO: -// - Support multiple devices, select one by ID! +// pa02 20-Nov-2014 supports 1,2,4 - relay devices +// // Build for Windows: using VC++ 2008 and WDK7.1 //~~~~~~~~~~~~~~~~~~~~~~~~ @@ -13,7 +11,7 @@ * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH */ -#define A_VER_STR "r1.1y (1 device only)" +#define A_VER_STR "r1.2" #include #include @@ -30,6 +28,24 @@ static int rel_read_status_raw(USBDEVHANDLE dev, void *raw_data); /* ------------------------------------------------------------------------- */ +static void usage(char *myName) +{ + char *p = strrchr(myName, '\\'); /* windows */ + if (p) myName = p + 1; + else p = strrchr(myName, '/'); /* whatever */ + if (p) myName = p + 1; + + fprintf(stderr, "USBHID relay utility, " A_VER_STR "\n\n"); + fprintf(stderr, "Usage:\n"); + fprintf(stderr, " %s on - turn relay ON\n", myName); + fprintf(stderr, " %s off - turn relay OFF\n", myName); + fprintf(stderr, " %s state - print state of the relays\n", myName); + fprintf(stderr, " %s enum - print state of all relay devices\n", myName); + fprintf(stderr, "\nParameter ID=XXXXX selects one device if several are connected.\n"); + fprintf(stderr, "Example: ID=ABCDE on 1\n"); +} + + static const char *usbErrorMessage(int errCode) { static char buffer[80]; @@ -119,8 +135,22 @@ static int enumFunc(USBDEVHANDLE dev, void *context) } DEBUG_PRINT(("Device %s%d found: ID=[%5s]\n", productName, num, &buffer[1])); - strcpy( g_enumCtx.id, (char*)&buffer[1] ); g_max_relay_num = num; + + if ( g_enumCtx.id[0] != 0 ) + { + if ( 0 != memcmp(g_enumCtx.id, &buffer[1], 5) ) + goto next; + } +#if 0 + if ( g_enumCtx.mydev ) + { + fprintf(stderr, "ERROR: More than one relay device found. ID must be specified\n"); + usbhidCloseDevice(dev); + usbhidCloseDevice(g_enumCtx.mydev); + return 0; + } +#endif g_enumCtx.mydev = dev; return 0; @@ -172,22 +202,6 @@ FILE *fp = stdout; /* ------------------------------------------------------------------------- */ -static void usage(char *myName) -{ - char *p = strrchr(myName, '\\'); /* windows */ - if (p) myName = p + 1; - else p = strrchr(myName, '/'); /* whatever */ - if (p) myName = p + 1; - - fprintf(stderr, "USBHID relay utility, " A_VER_STR "\n"); - fprintf(stderr, "Usage:\n"); - fprintf(stderr, " %s on - turn relay ON\n", myName); - fprintf(stderr, " %s off - turn relay OFF\n", myName); - fprintf(stderr, " %s state - print state of the relays\n", myName); - fprintf(stderr, " %s enum - print state of all relay devices\n", myName); -} - - // Read state of all relays // @return bit mask of all relays (R1->bit 0, R2->bit 1 ...) or -1 on error static int rel_read_status_raw(USBDEVHANDLE dev, void *raw_data) @@ -349,6 +363,19 @@ int main(int argc, char **argv) return err; } + if ( strncasecmp(arg1, "id=", 3) == 0 ) { + if (strlen(&arg1[3]) != 5) { + fprintf(stderr, "ERROR: ID must be 5 characters (%s)\n", arg1); + return 1; + } + + strcpy( g_enumCtx.id, &arg1[3]); + + // shift following params + arg1 = arg2; + arg2 = (argc >= 4) ? argv[3] : NULL; + } + dev = openDevice(); if ( !dev ) return 1; -- 2.25.1