Jump to content

USB Interface Relay Dllcall help


Recommended Posts

Digging into that *.dll

I found how the usb_relay_device_get_status works:  

just need to get the serial number from it and would be good!

Hope this help someone!

Cramaboule.

by the way this is the product we are talking about

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=relay.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Array.au3>

#cs
1. call usb_relay_init() to init the lib.
2. call usb_relay_device_enumerate() to get all the device pluged into pc
3. call usb_relay_device_open() open the device you need
4. other operation funcation:
call usb_relay_device_open_one_relay_channel() to open one way relay
call usb_relay_device_open_all_relay_channel() to open all relays
call usb_relay_device_close_one_relay_channel()to close one way relay
call usb_relay_device_close_all_relay_channel()to close all relays
#ce
$Serial = "abcde"
Global $hDllRelay = DllOpen("usb_relay_device.dll")

$aRet = DllCall( $hDllRelay, "int", "usb_relay_init" )
If @error Then
    MsgBox (16, "error", "The Divice cannot initialize", 3)
    Exit
EndIf

Global $hHandleRelay = DllCall( $hDllRelay, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", $Serial, "uint", 5 )
If @error Then
    MsgBox (16, "error", "The Divice cannot open", 3)
    Exit
EndIf

_ArrayDisplay($hHandleRelay)


;------------------------------

_OpenRelay(1)


$aStat = DllCall($hDllRelay, "int:cdecl", "usb_relay_device_get_status","Int" , $hHandleRelay[0], "uint*", 0)
If @error Then
    MsgBox (16, "error", "The Relay cannot read status", 3)
    Exit
EndIf

_ArrayDisplay($aStat)
_CloseRelay(1)

$aStat = DllCall($hDllRelay, "int:cdecl", "usb_relay_device_get_status","Int" , $hHandleRelay[0], "uint*", 0)
If @error Then
    MsgBox (16, "error", "The Relay cannot read status", 3)
    Exit
EndIf

_ArrayDisplay($aStat)


_Exit()

Func _OpenRelay($sChannel)
    $aRet = DllCall($hDllRelay, "int:cdecl", "usb_relay_device_open_one_relay_channel", "int", $hHandleRelay[0], "int", $sChannel)
    If @error Then
        MsgBox (16, "error", "The Relay cannot open", 3)
        Exit
    EndIf
    Return $aRet[0]
EndFunc


Func _CloseRelay($sChannel)
    $aRet = DllCall($hDllRelay, "int:cdecl", "usb_relay_device_close_one_relay_channel", "int", $hHandleRelay[0], "int", $sChannel)
    If @error Then
        MsgBox (16, "error", "The Relay cannot close", 3)
        Exit
    EndIf
    Return $aRet[0]
EndFunc

Func _Exit()
    _CloseRelay(1)
    DllClose($hDllRelay)
    Exit
EndFunc
Link to comment
Share on other sites

From your link in post 20 it seems to be possible to get the serial number with usb_relay_device_enumerate function:

$aRet = DllCall( $hDllRelay, "ptr:cdecl", "usb_relay_device_enumerate" )
If @error Then
  MsgBox(16, "error", "usb_relay_device_enumerate", 3)
  Exit
EndIf
$ptr = $aRet[0]

usb_relay_device_enumerate returns a pointer to a structure, which in C-code is defined in this way:

/*usb relay board info structure*/
struct usb_relay_device_info
{
  unsigned char *serial_number;
  char *device_path;
  usb_relay_device_type type;
  usb_relay_device_info* next;
};

It's not possible for me to test anything. I have no such USB Relay. So you have to do some tests to translate this structure to something which is working in AutoIt.

For a start you can try this:

$tag_usb_relay_device_info = "char* serial_number;char* device_path;uint relay_type;ptr relay_next"
$t_usb_relay_device_info = DllStructCreate( $tag_usb_relay_device_info, $ptr )
$ptr_serial_number = DllStructGetData( $t_usb_relay_device_info, "serial_number" )
ConsoleWrite( "$ptr_serial_number = " & $ptr_serial_number & @CRLF )
$ptr_device_path = DllStructGetData( $t_usb_relay_device_info, "device_path" )
ConsoleWrite( "$ptr_device_path = " & $ptr_device_path & @CRLF )
$relay_type = DllStructGetData( $t_usb_relay_device_info, "relay_type" )
ConsoleWrite( "$relay_type = " & $relay_type & @CRLF )
$relay_next = DllStructGetData( $t_usb_relay_device_info, "relay_next" )
ConsoleWrite( "$relay_next = " & $relay_next & @CRLF )

If this doesn't work you can try to replace the structure with this structure:

$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;ptr relay_next"
Link to comment
Share on other sites

From your link in post 20 it seems to be possible to get the serial number ...

 

Hi,

Thanks a lot @LarsJ We made a good step we got ALMOST  IT !

This is the code I get so far!

$aRet = DllCall( $hDllRelay, "ptr:cdecl", "usb_relay_device_enumerate" )
If @error Then
  MsgBox(16, "error", "usb_relay_device_enumerate", 3)
  Exit
EndIf
;_ArrayDisplay($aRet, "$aRet")
$ptr = $aRet[0]

$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;int relay_next"
;$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;ptr relay_next"

$t_usb_relay_device_info = DllStructCreate( $tag_usb_relay_device_info, $ptr )
If @error Then
    MsgBox(0, "", "Error in DllStructCreate " & @error);
    Exit
EndIf
$ptr_serial_number = DllStructGetData( $t_usb_relay_device_info, "serial_number" )
ConsoleWrite( "$ptr_serial_number = " & $ptr_serial_number & @CRLF )
$ptr_device_path = DllStructGetData( $t_usb_relay_device_info, "device_path" )
ConsoleWrite( "$ptr_device_path = " & $ptr_device_path & @CRLF )
$relay_type = DllStructGetData( $t_usb_relay_device_info, "relay_type" )
ConsoleWrite( "$relay_type = " & $relay_type & @CRLF )
$relay_next = DllStructGetData( $t_usb_relay_device_info, "relay_next" )
ConsoleWrite( "$relay_next = " & $relay_next & @CRLF )

Here is the results I get!

$ptr_serial_number = 0x02DE2EF8
$ptr_device_path = 0x02DE1218
$relay_type = 1
$relay_next = 0
$ptr_serial_number = 0x03582CD8
$ptr_device_path = 0x03581438
$relay_type = 1
$relay_next = 0

I got different results every time I run the script.

$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;int relay_next"

I tried different type but was not able to get something relevent

Any Hint?

C.

Link to comment
Share on other sites

This is looking fine. That the pointers (memory addresses) are different everytime you run the script is as expected.

Now you just have to read the serial number from the memory address given by the pointer. Add this to your code:

; Buffer for serial number for max. 32 characters
$tag_serial_number = "char[32]"

; Fill buffer with data from the memory location given by the pointer
$t_serial_number = DllStructCreate( $tag_serial_number, $ptr_serial_number )

; Get serial number as a string
$s_serial_number = DllStructGetData( $t_serial_number, 1 )

; Print serial number
ConsoleWrite( "$s_serial_number = " & $s_serial_number & @CRLF )
Link to comment
Share on other sites

:thumbsup:  :thumbsup:  :thumbsup:

WELL DONE !!!

IT WORKS !!!

all the code:

$aRet = DllCall( $hDllRelay, "ptr:cdecl", "usb_relay_device_enumerate" )
If @error Then
  MsgBox(16, "error", "usb_relay_device_enumerate", 3)
  Exit
EndIf
;_ArrayDisplay($aRet, "$aRet")
$ptr = $aRet[0]

$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;int relay_next"
;$tag_usb_relay_device_info = "ptr serial_number;ptr device_path;uint relay_type;ptr relay_next"

$t_usb_relay_device_info = DllStructCreate( $tag_usb_relay_device_info, $ptr )
If @error Then
    MsgBox(0, "", "Error in DllStructCreate " & @error);
    Exit
EndIf
$ptr_serial_number = DllStructGetData( $t_usb_relay_device_info, "serial_number" )
ConsoleWrite( "$ptr_serial_number = " & $ptr_serial_number & @CRLF )
$ptr_device_path = DllStructGetData( $t_usb_relay_device_info, "device_path" )
ConsoleWrite( "$ptr_device_path = " & $ptr_device_path & @CRLF )
$relay_type = DllStructGetData( $t_usb_relay_device_info, "relay_type" )
ConsoleWrite( "$relay_type = " & $relay_type & @CRLF )
$relay_next = DllStructGetData( $t_usb_relay_device_info, "relay_next" )
ConsoleWrite( "$relay_next = " & $relay_next & @CRLF )

; Buffer for serial number for max. 32 characters
$tag_serial_number = "char[32]"

; Fill buffer with data from the memory location given by the pointer
$t_serial_number = DllStructCreate( $tag_serial_number, $ptr_serial_number )

; Get serial number as a string
$s_serial_number = DllStructGetData( $t_serial_number, 1 )

; Print serial number
ConsoleWrite( "$s_serial_number = " & $s_serial_number & @CRLF )

and the result:

$ptr_serial_number = 0x026D2DD0
$ptr_device_path = 0x026D2DE0
$relay_type = 1
$relay_next = 0
$s_serial_number = 2UZXF

Fantastic

Thanks a lot !!!

Link to comment
Share on other sites

  • 3 years later...

Thanks for sharing your code, works great.

Was able to control the 4 relay version of the same product. This is a current ebay listing of the product as of Aug 2017.

At around $10 for a 4 channel relay it is hard to beat the price, although I would be hesitant to switch any line voltage loads with it. The PCB doesn't look to laid out in a way that adequately isolates the switched load from the low voltage circuitry.

This listing does not provide links to the software so I've attached the software including the required DLL. I didn't see any copyright notice in any of the software so I think that is ok.

RelayControl-All Vendor Info.zip

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...