USB HID 2 Channel Relay Needs Help

Aquí puedes preguntar o compartir tus dudas y conocimientos acerca del programa
Hi everyone how are you this days? Hoping you are good and fine away from covid..
Anyways i have found a 2 dll shared by @Pabloko on this thread: https://www.amsspecialist.com/viewtopic.php?f=15&t=7311

and first i tried the serial port using esp8266 however i have not luck to make it work in my test so i tried HID usb relay.
but still i cannot make send data to the device. Also wondering why the VID and PID is different form actual id of the device. i hope pabloko can help me this.. but if anyone have and idea i really appreciate the help.

some info of my hid is here:
https://github.com/pavel-a/usb-relay-hid
https://github.com/darrylb123/usbrelay

here are the things:

using the provide link of pabloko for the HID i tried to enumerate and display all hid devices..
Code on show:
table = HID.Enumerate(0) --use 0 for wildcard
_hid = Table.Count(table);
for i=1, _hid do
result = ComboBox.AddItem("ComboBox1", table.manufacturer_string.." "..table.product_string.." - (VID:"..table.vid.. ") (PID:"..table.pid..")", i);
end

Imagen

Imagen

as you can see the vid from table.vid is different form the vid in the path..

on ComboBox1 code:

dev = ComboBox.GetSelected(this);
index = String.ToNumber(ComboBox.GetItemData(this, dev));

vid = table[index].vid
pid = table[index].pid
path = table[index].path
serial = table[index].serial
release_number = table[index].release_number
manufacturer_string = table[index].manufacturer_string
product_string = table[index].product_string
interface_number = table[index].interface_number

--device = HID.Open(vid,pid,serial)
device = HID.OpenPath(path)
--result = Dialog.Message("Notice", tostring(device), MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
Input.SetText("Input1", path)
Input.SetText("Input2", tostring(device))

xButton:
device = HID.Open(vid, pid, 0)
device = HID.OpenPath(path)
written=HID.Write(device, "ON1") --data is a literal string

i cannot send data to my hid because device = HID.Open(vid, pid, 0) will result nil value...

hope anyone can help.. thank you.
hello my friend, been some days off on the beach...

the numbers in path are the same, 0x16C0 hex = 5824 dec and 0x05df hex = 1503 dec.

You can use decimal numbers or its hex representation, lua accept both as the same number
Imagen

As for the protocol for this usb relay, found a repo that us using the same library for hid as my plugin: https://github.com/darrylb123/usbrelay

They have dumped the usb descriptor, wich is something you need, the protocol seems to be quite simple:
Imagen

so basically all request are 8 bytes long, as you have to include the usage page (0) it will be 9 bytes what you must send.
byte 1 means state (0xFD/0xFF off/on) and byte 2 tells the relay number (1/2/3... depending on your board, it seems even you can read the number of realays and change the serial number of the board)

some samples
HID.Write(hid, string.char(0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn on relay 1
HID.Write(hid, string.char(0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn off relay 1
as you see, only second (state) and third (relay) bytes are meaningful, the other are just zerofill but needed in that exact order as usb descriptor.

Hope you get it
also reaiding your messages on the other thread

only use 1 of HID.Open or HID.OpenPath, not both. generally more safe if you use only OpenPath as you have it from the enumeration.

HID.Open is more mean for the situation where you want to open a device without enumerating, just knowing vid/pid and possibly the serial if theres many devices of the same kind.

The path is totally unique per device.

If you continue to get hid==nil, try using admin permission or less restrictive user.
Hi Pabloko, thank you for your time replying my questions:
Also it made me more clear but still not get the relay turn on.. i have tried the given example but still the relay/board does not react

here is the code on my combobox
dev = ComboBox.GetSelected(this);
index = String.ToNumber(ComboBox.GetItemData(this, dev));

vid = table[index].vid
pid = table[index].pid
path = table[index].path
serial = table[index].serial
release_number = table[index].release_number
manufacturer_string = table[index].manufacturer_string
product_string = table[index].product_string
interface_number = table[index].interface_number

hid = HID.OpenPath(path)
Input.SetText("Input1", path)
Input.SetText("Input2", tostring(hid))

xButton1:
written=HID.Write(hid, string.char(0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00))

xButton2:
written=HID.Write(hid, string.char(0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00))

but still does not react,
also i have found this tool in the internet "RelayCtl.exe" with this picture
Imagen

Link: https://sourceforge.net/projects/usb-relay-hid/

in that Usb Relay Controller i have found i can managed to turn on and off the relay1 and relay2..

i even try this attempt
written=HID.Write(hid, string.char(0x00, 0xFF, 0x01))
but still no luck..

PS: I have select the Require As Admin in my project also i can share to you the apz.

Thank you Pabloko..

Regards
Sendai
check if hid==nil that could mean a library issue.
also check if using admin rights fix the issue.

If you want to make sure of the protocol, download WIRESHARK tool, it includes usb hid capturing, and capture your usb talking with the valid app (connecting, and turning relays on/off). Upload here the .pcap file and ill give you more info.
On usb the lenght of packet is always fixed to what descriptor says plus 1 byte for the page code.

going to try check with some hid device later, but im already tested this plugin with medical equipment
Hi pabloko, the hid is not nil anymore i managed to get the userdata already, and when i try to HID.write also it return a value.. see screenshot
Imagen

i have tried wireshark but no idea how it works.. i cant find the USBRelays in there

Imagen


Sendai
hey, with wireshark try usbpcap, plug your usb device and start sending it command.- You will be able to view all the usb traffic. You can compare to see differences in data
hi pabloko.. check this data when i try to use the HID Usb controller app..
Imagen

Imagen

when i used my ams app to send upon clicking the button there is no data coming in the wireshark..

Thanks
Hi pabloko.. i send you the apz of what i have done.. thanks for all your help..

Sendai
could you send the pcap from wireshark too?

i dont have the device to look
yes i already send it, also my problem using the
written=HID.Write(hid, string.char(0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00))

has nothing to go wireshark, maybe there is a problem with my ams sample.. so i used only the HID Control app to send data., as you can see in video data is receive using the hid controll and also turn on the relays, while in my ams only the my mouse click receives by wireshark.

thanks

Sendai
small question, the result of hid.write is 9?
the result of hid.write from my ams is 4294967295
Imagen
Take a look, for this example i've used a USB HID Buzz controller from PS3 that seems manufactured by namco/logitech. Its far complex than the relays as it send reports of 1+4 bytes for buttons and recive 1+5 bytes with the keystates that you can watch on binary representation. i did not have another 2 extra AA batteries for the 4th controller so i only display 3, but you get the idea...

Ive set up a timer to read the device and parse the output to binary so you can watch bits of each button toggling. for more precise implementation id use the lua bit library.



the lib seems to be ok, found a couple of problems and recompiled the lib just in case, find attached my apz for ps3 wireless buzz controller, you can obtain new dll in the apz

HIDE: ON
Hidebb Message Hidden Description
I have download and tested the new usbhid and it seems the problem is on my side.. when i try to send data it returns -1 and give incorrect function message.

i am using windows 10 AMS v8.5.1.0

Imagen

also the page time has error with me after i select the device in combobox the application terminated by it self..
i have send you the video link, today i will try on windows 7 computer for my friend and see it this works..

Thanks

Edit: tried i also in AMS v8.5.2.0 the same result of incorrect function and the HID.Write returns -1
Friend you have to edit the HID.Write according to your device specs, try the lines that ive posted you the other day.

the function SendHid is sending now 1+4 bytes, and you should be sending 1+8, you just cannot use my example for buzz on your relay board...
HID.Write(hid, string.char(0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn on relay 1
HID.Write(hid, string.char(0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn off relay 1
Once you send the correct data, it will work
Pabloko escribió:
11 Ago 2021 00:32
Friend you have to edit the HID.Write according to your device specs, try the lines that ive posted you the other day.

the function SendHid is sending now 1+4 bytes, and you should be sending 1+8, you just cannot use my example for buzz on your relay board...
HID.Write(hid, string.char(0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn on relay 1
HID.Write(hid, string.char(0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) --turn off relay 1
Once you send the correct data, it will work
hi my friend.. thanks for your help on this i really dont know what the error, yes i tried it also that line but still no luck..
Imagen

the old lib will return a 4294967295 after hid.write, while the new one will return -1, however all is return incorrect function usind hid.error

thanks.
Hi friend.. i have found this in the internet but im curios if this help about my hid device..

Imagen

Thanks
i have downloaded another tool called SimpleHIDwrite and i found out that my device have no write feature
Imagen

i really confused because using other tool i can managed to turn on/off both 2 relays, or maybe the device i have is not programmable i dont know..

but still i do some research about my device..

Thanks
Try HID.SendFeatureReport intead HID.Write

apart from that look on wireshack for differences

regards