alankam58 Posted March 22, 2014 Share Posted March 22, 2014 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 More sharing options...
LarsJ Posted March 23, 2014 Share Posted March 23, 2014 Try this:DllCall( $dll, "int", "usb_relay_device_open_with_serial_number", "str", "JC8EQ", "uint", 5 ) Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
alankam58 Posted March 23, 2014 Author Share Posted March 23, 2014 Hi LarsJ, thank for the help. but it not working.. it generate below: error --> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop !>21:41:17 AutoIt3.exe ended.rc:-1073741795 >Exit code: -1073741795 Time: 7.546 Link to comment Share on other sites More sharing options...
alankam58 Posted March 23, 2014 Author Share Posted March 23, 2014 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 More sharing options...
LarsJ Posted March 23, 2014 Share Posted March 23, 2014 Does usb_relay_init work? Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
alankam58 Posted March 24, 2014 Author Share Posted March 24, 2014 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 More sharing options...
LarsJ Posted March 24, 2014 Share Posted March 24, 2014 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 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
alankam58 Posted March 24, 2014 Author Share Posted March 24, 2014 Hi LarsJ, Thank you very much for the help. it working now. Link to comment Share on other sites More sharing options...
alankam58 Posted March 24, 2014 Author Share Posted March 24, 2014 Hi LarsJ, btw what is this mean "int:cdecl"? Link to comment Share on other sites More sharing options...
LarsJ Posted March 24, 2014 Share Posted March 24, 2014 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. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
alankam58 Posted March 25, 2014 Author Share Posted March 25, 2014 Hi Lars, Thank you very much for the help and info on the dll. i learn some new thing about DllCall here. Thank Alan Link to comment Share on other sites More sharing options...
ynbIpb Posted March 28, 2014 Share Posted March 28, 2014 alankam58, please show your results? interested in the code on and off switch. Link to comment Share on other sites More sharing options...
cramaboule Posted April 16, 2014 Share Posted April 16, 2014 @alankam58 I am interested too do you have a piece of code to show ? C. My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website Link to comment Share on other sites More sharing options...
cramaboule Posted April 17, 2014 Share Posted April 17, 2014 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 My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website Link to comment Share on other sites More sharing options...
cramaboule Posted April 17, 2014 Share Posted April 17, 2014 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 My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website Link to comment Share on other sites More sharing options...
erickirkland Posted April 19, 2014 Share Posted April 19, 2014 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 More sharing options...
ColdFox Posted May 23, 2014 Share Posted May 23, 2014 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 More sharing options...
alankam58 Posted July 30, 2014 Author Share Posted July 30, 2014 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 More sharing options...
alankam58 Posted July 30, 2014 Author Share Posted July 30, 2014 additional info. for the $RelaySN. please use the software provide by USB device the check it. click find device and u will get the Serial Number. Link to comment Share on other sites More sharing options...
cramaboule Posted August 11, 2014 Share Posted August 11, 2014 @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. My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now