Page 1 of 1

USB Controlled Relay library

Posted: 13 Mar 2018, 07:53
by x32
A function library for USB Relay boards. See this post for capturing USB input.
https://www.banggood.com/5V-2-Channel-USB-Relay-Module-Programmable-Computer-Control-p-910097.html
Image

It only has one function, turn the relays on or off. There must be a second function, for finding the board serial number, but there are no instructions for it. A test app in the file downloads can be used to find it.

Requires the following files in the script directory.
1. CommandApp_USBRelay.exe
2. usb_relay_device.dll
3. GuiApp_English.exe ;;;;;; These last two are only for
4. XCGUI.dll ;;;;;; finding the device serial number.

File download links.
Original file, https://www.dropbox.com/sh/7clljb4h1aeb0m1/AAA-oUOCE2a15PC9BoMWY-Cha

Backup on my website, http://www.starshipgrissom.com/ahk/USB_Relay_Control.7z
This one includes copies of the library and a test script.

USBRelay(serial,oc,chan)
"serial" is your device's serial number.
"oc" is open or close. This is opposite of what would be expected, "open" activates the relay and "close" disconnects it.
"chan" is the relay chan, 1, 2, 3, ect. 255 controls all relays on the board.
Example - USBRelay(tk421,open,1)

I've only tested with a two channel board but I imagine it would work with any number of channels.

Code: Select all

;;  USB controled relay board. By X32.
;;  https://www.banggood.com/5V-2-Channel-USB-Relay-Module-Programmable-Computer-Control-p-910097.html

;;  Requires the following files in the script directory.
;;  1. CommandApp_USBRelay.exe
;;  2. usb_relay_device.dll
;;  3. GuiApp_English.exe      ;;;;;;  These last two are only for 
;;  4. XCGUI.dll               ;;;;;;  finding the device serial number.
;;
;;  File download links.
;;  Original file, https://www.dropbox.com/sh/7clljb4h1aeb0m1/AAA-oUOCE2a15PC9BoMWY-Cha
;;
;;  Backup on my website, http://www.starshipgrissom.com/ahk/USB_Relay_Control.7z


;; Usage, 
;; Ther are no instructions to locate the device serial number so you must use the test app for this purpose.
;; Run GuiApp_English.exe, click "Find Device", then find your device serial number in the drop down list.
;;
;; "serial" is your device's serial number.
;; "oc" is open or close.
;; "chan" is the relay chan, 1, 2, 3, ect. 
;; Example - USBRelay(tk421,open,1) 


USBRelay(serial,oc,chan)
	{
	Run, CommandApp_USBRelay %serial% %oc% %chan%, , Hide
	}
	
	
/*   ;Test Application

; #Include, USBRelay.ahk

Gui, Add, Edit, x5 y5 w100 h25 vsn, xxxxx
Gui, Add, Text, x+5 y5 w100, <== Enter device`n serial number here.
Gui, Add, Button, x5 y55 w100 h25 goneoff, Relay 1 Off 
Gui, Add, Button, x+5 y55 w100 h25 goneon, Relay 1 On
Gui, Add, Button, x5 y105 w100 h25 gtwooff, Relay 2 Off 
Gui, Add, Button, x+5 y105 w100 h25 gtwoon, Relay 2 On 
Gui, Add, Button, x5 y155 w100 h25 galloff, All Relays Off 
Gui, Add, Button, x+5 y155 w100 h25 gallon, All Relays On 

Gui, Show, w215 h200 , USB Relay Control 
Return 

oneoff:
Gui, Submit, NoHide
serial = %sn%
oc = close
chan = 1
USBRelay(serial,oc,chan)
Return

oneon:
Gui, Submit, NoHide
serial = %sn%
oc = open
chan = 1
USBRelay(serial,oc,chan)
Return

twooff:
Gui, Submit, NoHide
serial = %sn%
oc = close
chan = 2
USBRelay(serial,oc,chan)
Return

twoon:
Gui, Submit, NoHide
serial = %sn%
oc = open
chan = 2
USBRelay(serial,oc,chan)
Return

alloff:
Gui, Submit, NoHide
serial = %sn%
oc = close
chan = 255
USBRelay(serial,oc,chan)
Return

allon:
Gui, Submit, NoHide
serial = %sn%
oc = open
chan = 255
USBRelay(serial,oc,chan)
Return


Exit:
GuiEscape:
GuiClose:
Gui, Destroy
ExitApp
*/
	
	
	

Re: USB Controlled Relay library

Posted: 15 Sep 2022, 08:16
by jannik
While I don't like to bump old threads, I just recently had to implement a relay like this with AutoHotkey and stumbled across this thread. I suspect others might do the same so here is my solution.
I didn't implement all the functions, but I looked at usb_relay_device.h and used DllCall to call the functions in the DLL directly.
Here is my little class:

Code: Select all

class Relay
{
	hRelay := ""
	hDevice := ""
	
	__New()
	{
		this.hRelay := DllCall( "LoadLibrary", "Str", "usb_relay_device.dll" )
		dll_return := DllCall("usb_relay_device.dll\usb_relay_init")
	}
	
	__Delete()
	{
		dll_return := DllCall("usb_relay_device.dll\usb_relay_exit")
		DllCall("FreeLibrary", "Ptr", this.hRelay)
	}
	
	lib_version()
	{
		dll_return := DllCall("usb_relay_device.dll\usb_relay_device_lib_version")
		return dll_return
	}
	
	device_open_with_serial_number(serial_number)
	{
		this.hDevice := DllCall("usb_relay_device.dll\usb_relay_device_open_with_serial_number", "AStr", serial_number, "Int", StrLen(serial_number))
		return this.hDevice
	}
	
	device_close()
	{
		dll_return := DllCall("usb_relay_device.dll\usb_relay_device_close", "Ptr", this.hDevice)
		return dll_return
	}
	
	device_open_all_relay_channel()
	{
		dll_return := DllCall("usb_relay_device.dll\usb_relay_device_open_all_relay_channel", "Ptr", this.hDevice)
		return dll_return
	}
	
	device_close_all_relay_channel()
	{
		dll_return := DllCall("usb_relay_device.dll\usb_relay_device_close_all_relay_channel", "Ptr", this.hDevice)
		return dll_return
	}
}

Re: USB Controlled Relay library

Posted: 26 Feb 2023, 19:25
by x32
jannik wrote:
15 Sep 2022, 08:16
While I don't like to bump old threads, I just recently had to implement a relay like this with AutoHotkey and stumbled across this thread. I suspect others might do the same so here is my solution.
I didn't implement all the functions, but I looked at usb_relay_device.h and used DllCall to call the functions in the DLL directly.
I didn't know that anyone had posted in this topic but this is great, can't wait to try it out. One quick question though, I purchased a second one of these relays but the app I used to get the serial number returned the exact same serial number for both. So I could use only one on any pc. Do you know if your serial number call returns actual serial numbers and can you use multiple relay boards on the same computer?

Re: USB Controlled Relay library

Posted: 04 Mar 2023, 07:07
by LegzRwheelz
Would I be able to use your code with any relay board or is this limited to the one pictured? Today was the first time ever hearing about these boards as I posted over at r/Arduino on Reddit with an idea to use an Arduino and a few relay modules to be turn on/off the switches on a USB hub with switches. I was suggested to look into these boards and they seem promising but I'm looking for something I can control via command or AutoHotKey that doesn't require learning a new scripting language as I intend to incorporate control of the relay board into my gaming setup to keep a multitude of wireless and wired controllers connected at all times without being shown as connected until a game is launched that uses the respective controller.

Re: USB Controlled Relay library

Posted: 06 Mar 2023, 09:58
by x32
ctind wrote:
04 Mar 2023, 07:07
Would I be able to use your code with any relay board or is this limited to the one pictured? Today was the first time ever hearing about these boards as I posted over at r/Arduino on Reddit with an idea to use an Arduino and a few relay modules to be turn on/off the switches on a USB hub with switches. I was suggested to look into these boards and they seem promising but I'm looking for something I can control via command or AutoHotKey that doesn't require learning a new scripting language as I intend to incorporate control of the relay board into my gaming setup to keep a multitude of wireless and wired controllers connected at all times without being shown as connected until a game is launched that uses the respective controller.
I have only been able to use the type/brand of board pictured. I've tried with other relay boards with no luck. However, the library works with any size of this board, I believe they come up to 8 channels.

Re: USB Controlled Relay library

Posted: 02 May 2023, 01:56
by payaAHK
hello
could you post an exemple of global script using your class
IE activate relay 01 10 sec after boot then relay 02 30 seconds behind
i try to do this, but the relay lost their ID on reboot
this first bat work well untill reboot:

Code: Select all

@echo off
REM 2023-05-01 en connectant
REM com1 avec no1 === commun1 & Normalement Ouvert 1
REM l action ferme > contact
REM errorlevel 0 si la communication s est bien faite
REM PAS SI L ORDRE est VALIDE
REM & 1 si la communication ne s est pas bien deroulee

REM l identification du relais ici HURTM
REM est a recuperer avec GuiApp_English.exe

set relay=HURTM
Title RELAY double 01 on
Mode con cols=50 lines=12
cls & color 0A & echo.
timeout /t 10
echo   ******************************************
echo    RELAY double 01 on   %relay%
echo   ******************************************

CommandApp_USBRelay  %relay% open 01
if %errorlevel% ==0 (
echo Pas d erreur de communication
) else (
echo ERREUR de communication

)

timeout /t 30
set relay=HURTM
Title RELAY double 02 on
Mode con cols=50 lines=12
cls & color 0A & echo.
echo   ******************************************
echo    RELAY double 02 on   %relay%
echo   ******************************************

CommandApp_USBRelay  %relay% open 02
if %errorlevel% ==0 (
echo Pas d erreur de communication
) else (
echo ERREUR de communication

)
timeout /t 10