Skip to content

Commit

Permalink
Added capability to change the serial of the device. Uses relay 0 as …
Browse files Browse the repository at this point in the history
…the flag to change the serial.
  • Loading branch information
Darryl Bond committed Mar 17, 2018
1 parent 7a6b941 commit 75954bf
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,53 @@ If for some reason the USB id changes, (ie other than 16c0:05df) set the USBID e
$sudo USBID=16c0:05df ./usbrelay
```

Change the serial permanently

Use the fictitious relay 0 to set the serial permanently. If you have duplicate serials, make sure only one is plugged in when you change it.
Maximum of 5 character serial probably sensible to change one at a time.

```
$ sudo ./usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw4
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
Number of Relays = 2
ZXCV_1=0
ZXCV_2=0
$ sudo ./usbrelay ZXCV_0=ZAQ12
Orig: ZXCV, Serial: ZXCV, Relay: 0 State: 0
Device Found
type: 16c0 05df
path: /dev/hidraw4
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
Number of Relays = 2
Serial: ZXCV, Relay: 0 State: 0
1 HID Serial: ZXCV
Serial: ZXCV, Relay: 0 State: 0 --- Not Found
$ sudo ./usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw4
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
Number of Relays = 2
ZAQ12_1=0
ZAQ12_2=0
```


Enjoy
52 changes: 42 additions & 10 deletions usbrelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main( int argc, char *argv[]) {
char *vendor, *product;
int exit_code = 0;

/* allocate the memeory for all the relays */
/* allocate the memory for all the relays */
if (argc > 1) {
relays = malloc(size*(argc+1)); /* Yeah, I know. Not using the first member */
relays[0].this_serial[0] = '\0';
Expand All @@ -64,11 +64,17 @@ int main( int argc, char *argv[]) {
relays[i].relay_num = atoi(token);
}
token = strtok(NULL, delimiters);
if (token != NULL) {
if (atoi(token)) {
relays[i].state = ON;
} else {
relays[i].state = OFF;
if (relays[i].relay_num == 0) { /* command to change the serial - remaining token is the new serial */
if (token != NULL) {
strcpy(relays[i].new_serial,token);
}
} else {
if (token != NULL) {
if (atoi(token)) {
relays[i].state = ON;
} else {
relays[i].state = OFF;
}
}
}
fprintf(stderr,"Orig: %s, Serial: %s, Relay: %d State: %x\n",arg_t,relays[i].this_serial,relays[i].relay_num,relays[i].state);
Expand Down Expand Up @@ -128,10 +134,14 @@ int main( int argc, char *argv[]) {
fprintf(stderr,"Serial: %s, Relay: %d State: %x \n",relays[i].this_serial,relays[i].relay_num,relays[i].state);
if (!strcmp(relays[i].this_serial, (const char *) buf)) {
fprintf(stderr,"%d HID Serial: %s ", i, buf);
fprintf(stderr,"Serial: %s, Relay: %d State: %x\n",relays[i].this_serial,relays[i].relay_num,relays[i].state);
if (operate_relay(handle,relays[i].relay_num,relays[i].state) < 0 )
exit_code++;
relays[i].found = 1;
if (relays[i].relay_num == 0 ) {
set_serial(handle,relays[i].new_serial);
} else {
fprintf(stderr,"Serial: %s, Relay: %d State: %x\n",relays[i].this_serial,relays[i].relay_num,relays[i].state);
if (operate_relay(handle,relays[i].relay_num,relays[i].state) < 0 )
exit_code++;
relays[i].found = 1;
}
}
}
hid_close(handle);
Expand Down Expand Up @@ -178,3 +188,25 @@ int operate_relay(hid_device *handle,unsigned char relay, unsigned char state){
}
return(res);
}

int set_serial(hid_device *handle,char *newserial){
unsigned char buf[9];// 1 extra byte for the report ID
int res;

buf[0] = 0x0; //report number
buf[1] = CMD_SET_SERIAL;
buf[2] = newserial[0];
buf[3] = newserial[1];
buf[4] = newserial[2];
buf[5] = newserial[3];
buf[6] = newserial[4];
buf[7] = 0x00;
buf[8] = 0x00;
res = hid_write(handle, buf, sizeof(buf));
if (res < 0) {
fprintf(stderr,"Unable to write()\n");
fprintf(stderr,"Error: %ls\n", hid_error(handle));
}
return(res);
}

3 changes: 3 additions & 0 deletions usbrelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#define ON 0xff
#define OFF 0xfd
#define CMD_SET_SERIAL 0xfa

int operate_relay(hid_device *handle,unsigned char relay, unsigned char state);
int set_serial(hid_device *handle,char *newserial);

struct relay {
char this_serial[10];
unsigned char relay_num;
unsigned char state;
int found;
char new_serial[10];
};

0 comments on commit 75954bf

Please sign in to comment.