Jump to content

USB Interface Relay Dllcall help


Recommended Posts

Hi Guys, i needed some advice on how to convert or using a C++ dll in autoit. i just purchase an USB interface relay and i like to write a simple script using autoit to control the on off of the relay boards. the supplier of the interface relay supply me with a dll file and intruction in C++ but problem is i do not know how to convert or how to use the function from the dll. Some help from professional like your guys would be very appreciated. thank in advance.

do not understand how to fill in: i cannot find which type to use in autoit for (const char *serial_number, unsigned len)

instruction for c++ is:-

/*open device that serial number is serial_number*/
/*@return: This funcation returns a valid handle to the device on success or NULL on failure.*/
/*e.g: usb_relay_device_open_with_serial_number("abcde", 5")*/
int EXPORT_API usb_relay_device_open_with_serial_number(const char *serial_number, unsigned len);
 
autoit script i wrote so far.
Local $dll = DllOpen(@ScriptDir&"usb_relay_device.dll")
If $dll = -1 Then MsgBox(48,"","Dll Open error")
Local $result = DllCall($dll, "int", "usb_relay_init")
If @error Then MsgBox(48,"","init error")

DllCall($dll, "WORD", "usb_relay_device_open_with_serial_number", ")

the serial number of the device is "JC8EQ"

an example would be very helpful thank.

usb_relay_device.h

Link to comment
Share on other sites

Link to comment
Share on other sites

i have a instruction for C++ they give me as below. but since i am dummy for c++ i dun really understand it.

1st, How to use in Visual Studio.
1. Creat a new C++ Porject
2. In stdfax.h file, add this code:
   #include "usb_relay_device.h"
   #pragma comment(lib, "usb_relay_device.lib")
 
3. Copy the usb_relay_device.dll file into the dir that generate your applicaiton. Like the Release or Debug dir
 
2nd, The way to use function:
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 function:
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
Link to comment
Share on other sites

the usb_relay_init work it did not generate any error. 

i try run it without calling usb_relay_device_open_with_serial_number and run usb_relay_device_enumerate() instead it have return and array when i put return type as int

Link to comment
Share on other sites

Try these:

 

$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", "JC8EQ", "uint", 5 )
If @error Then
  ConsoleWrite( "Errorcode: " & @error & @CRLF )
Else
  ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
EndIf

$tSerial = DllStructCreate( "char[16]" )
DllStructSetData( $tSerial, 1, "JC8EQ" )
DllStructSetData( $tSerial, 1, 0, 6 )
$aRet = DllCall( $dll, "int", "usb_relay_device_open_with_serial_number", "ptr", DllStructGetPtr( $tSerial ), "uint", 5 )
If @error Then
  ConsoleWrite( "Errorcode: " & @error & @CRLF )
Else
  ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
EndIf

$tSerial = DllStructCreate( "char[16]" )
DllStructSetData( $tSerial, 1, "JC8EQ" )
DllStructSetData( $tSerial, 1, 0, 6 )
$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "ptr", DllStructGetPtr( $tSerial ), "uint", 5 )
If @error Then
  ConsoleWrite( "Errorcode: " & @error & @CRLF )
Else
  ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
EndIf
Link to comment
Share on other sites

DllCall supports two calling conventions (rules for calling functions in a DLL): stdcall and cdecl. stdcall is the default way to call functions in the Windows API. cdecl is the old method from C programming.

stdcall is also the default for DllCall. You don't have to specify anything to use this method. To use the cdecl method, you must add "cdecl" after the return type separated by a ":".

Which method you must use depends of the implementation of the DLL. As you have seen, you must use the proper method.

Regards Lars.

Link to comment
Share on other sites

  • 3 weeks later...

@alankam58 I am interested too do you have a piece of code to show ?

C.

Link to comment
Share on other sites

Hello,

$dll = DllOpen("usb_relay_device.dll")

$aRet1 = DllCall( $dll, "int", "usb_relay_init" )
ConsoleWrite($aRet1[0] & @LF)

$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", "TGND1", "uint", 5 )
If @error Then
ConsoleWrite( "Errorcode: " & @error & @CRLF )
Else
ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
EndIf
$para1 = "int"
$para2 = 1

$aRet2 = DllCall($aRet[0], "int", "usb_relay_device_open_one_relay_channel", $para1, $para2)
ConsoleWrite($aRet2 & @LF)

Sleep(1000)


$aRet2 = DllCall($aRet[0], "int", "usb_relay_device_close_one_relay_channel", $para1, $para2)
ConsoleWrite($aRet2 & @LF)

Sleep(1000)

DllClose($dll)

I have this piece of code, but it is NOT working.

I got this as a result:

0
Handle: 6565168
0
0 

I am noob with DLL. Can you help me please?
I would like also to get the serial number with 
 

usb_relay_device_enumerate

 
Thanks for your help
 
Cramaboule

Link to comment
Share on other sites

Hello again,

I found a part of my problem: I know how to open and close the relay:

$dll = DllOpen("usb_relay_device.dll")

$aRet1 = DllCall( $dll, "int", "usb_relay_init" )
ConsoleWrite($aRet1[0] & @LF)

$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", "TGND1", "uint", 5 )
If @error Then
  ConsoleWrite( "Errorcode: " & @error & @CRLF )
Else
  ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
EndIf
$para1 = "int"
$para2 = $aRet[0]
$para3 = "int"
$para4 = 1

$aRet2 = DllCall($dll, "int:cdecl", "usb_relay_device_open_one_relay_channel", $para1, $para2, $para3, $para4)
ConsoleWrite($aRet2[0] & @LF)

Sleep(1000)


$aRet2 = DllCall($dll, "int:cdecl", "usb_relay_device_close_one_relay_channel", $para1, $para2, $para3, $para4)
ConsoleWrite($aRet2[0] & @LF)


DllClose($dll)

need help to retrieve the serial number.

Thanks for your help

Link to comment
Share on other sites

Hello AlanKam,

I think I have purchased the same device as you.

For my application I want to open and close the relays via Excel VBA.

It appears I am calling the usb_relay_init() correctly and getting a "0" result

I know the serial number I need to call: BL1M2

When I used the usb_relay_device_open("BL1M2") I get a bad DLL call error.  I also cannot use the "enumerate" function, and any of the open/close relays functions.

I am wondering if I am sending or receiving the wrong types from the DLL.

Do you know what arguments are expected by each DLL function you listed above, and what are returned?

If it isn't obvious, this is well above my pay grade!

Link to comment
Share on other sites

  • 1 month later...

Hi guys, I got here because I'm also having some troubles with an usb relay that seems to be like yours. The problem is that it doesn't even work using the available software. What operative system do you use?

Link to comment
Share on other sites

  • 2 months later...

Sorry guys for late reply, be busy with work lately.

hope below code still help.

Func _RelayOn($Port="01")
 
Local $dll = DllOpen(@ScriptDir&"usb_relay_device.dll")
If $dll = -1 Then MsgBox(48,"","Dll Open error")
Local $result = DllCall($dll, "int", "usb_relay_init")
If @error Then MsgBox(48,"","init error")
;_ArrayDisplay($result)
 
$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", $RelaySN, "uint", 5 )
If @error Then
ConsoleWrite( "Errorcode: " & @error & @CRLF )
SetError(@error)
Else
ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
$return = DllCall($dll, "int:cdecl", "usb_relay_device_open_one_relay_channel","int",$aRet[0],"int",$Port)
If @error Then ConsoleWrite( "Errorcode Open: " & @error & @CRLF )
DllCall($dll, "int:cdecl", "usb_relay_device_close","int",$aRet[0])
EndIf
 
DllClose($dll)
 
EndFunc
 
Func _RelayOff($Port="01")
 
Local $dll = DllOpen(@ScriptDir&"usb_relay_device.dll")
If $dll = -1 Then MsgBox(48,"","Dll Open error")
Local $result = DllCall($dll, "int", "usb_relay_init")
If @error Then MsgBox(48,"","init error")
;_ArrayDisplay($result)
 
$aRet = DllCall( $dll, "int:cdecl", "usb_relay_device_open_with_serial_number", "str", $RelaySN, "uint", 5 )
If @error Then
ConsoleWrite( "Errorcode: " & @error & @CRLF )
SetError(@error)
Else
ConsoleWrite( "Handle: " & $aRet[0] & @CRLF )
$return = DllCall($dll, "int:cdecl", "usb_relay_device_close_one_relay_channel","int",$aRet[0],"int",$Port)
If @error Then ConsoleWrite( "Errorcode Close: " & @error & @CRLF )
;_ArrayDisplay($return)
DllCall($dll, "int:cdecl", "usb_relay_device_close","int",$aRet[0])
EndIf
 
DllClose($dll)
 
EndFunc
Link to comment
Share on other sites

  • 2 weeks later...

@LarsJ

Do you know how I would get the serial number?

There is the file at the first post and I found this as well

http://ideone.com/tI8mWo

I really need this and hope someone or lary can help me.

Cramaboule.

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...