TestingA Posted May 21, 2020 Posted May 21, 2020 Hi again,😇 i was trying to send a multiple variables($ValueA,$ValueB) to Arduino with AutoIT but i can't find any solotion how to do it. Autoit Code: expandcollapse popup#include <CommMG.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <WinAPI.au3> #include <Misc.au3> $vDLL = DllOpen("user32.dll") ;Conncet to Arduino Global $CMPort = 3 Global $CmBoBaud = 9600 Global $sportSetError = '' Global $CmboDataBits = 8 Global $CmBoParity = "none" Global $CmBoStop = 1 Global $setflow = 2 _CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow) If @error Then MsgBox(16,"Error!","Can't connect to Arduino on port - "&$CMPort) Exit EndIf _CommSetRTS(0) _CommSetDTR(0) Local $ValueA = Random(0, 1270, 1) Local $ValueB = Random(0, 1909, 1) While 1 If _IsPressed("1B") Then Exit ;ESC key If _IsPressed("10", $vDLL) Then ;SHIFT key Send_Value_To_Arduino() EndIf WEnd Func _ExitFunction() Exit EndFunc Func Send_Value_To_Arduino() _CommSendData($ValueA & $ValueB) ; Here is my Problem... EndFunc  Arduino Code: void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { if (Serial.available()) // send data only when receive { int data = Serial.read(); // Read received data if (data) { Serial.println(data); //output $ValueA & $ValueB } } } Â
argumentum Posted May 21, 2020 Posted May 21, 2020 (edited) I have never used Arduino but for what I see, you use a serial port to communicate. Whatever you send are bytes. You can use the bites in a byte as a trigger or a bunch of bytes as a string. Nonetheless, you are sending via a comm port.Watch some of these videos. and use a RS232 Terminal to get the Arduino part working. Then try with AutoIt to do more elaborate stuff  In the above code, I'd change "Local $Value = Random(0, 1909, 1)" to "Global $Value = Random(32, 126, 1)". Do that for both , $ValueA and $ValueB. Again, learning Arduino and AutoIt at the same time, can get difficult. Have fun learning stuff     Edited May 21, 2020 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Danyfirex Posted May 21, 2020 Posted May 21, 2020 (edited) You can use _CommSendByteArray to send x number of bytes so you can then read each byte from your arduino code. If you want to send 2 ints (4 bytes each one) You could do something like this.  expandcollapse popup#include <CommMG.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <WinAPI.au3> #include <Misc.au3> Global $vDLL = DllOpen("user32.dll") ;Conncet to Arduino Global $CMPort = 3 Global $CmBoBaud = 9600 Global $sportSetError = '' Global $CmboDataBits = 8 Global $CmBoParity = "none" Global $CmBoStop = 1 Global $setflow = 2 _CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow) If @error Then MsgBox(16, "Error!", "Can't connect to Arduino on port - " & $CMPort) Exit EndIf _CommSetRTS(0) _CommSetDTR(0) Local $ValueA = Random(0, 1270, 1) Local $ValueB = Random(0, 1909, 1) While 1 If _IsPressed("1B") Then Exit ;ESC key If _IsPressed("10", $vDLL) Then ;SHIFT key Send_Value_To_Arduino() Sleep(500) EndIf WEnd Func _ExitFunction() DllClose($vDLL) _CommClosePort() Exit EndFunc ;==>_ExitFunction Func Send_Value_To_Arduino() Local $taInts = DllStructCreate("int Value1;int Value2") $taInts.Value1 = Random(999999, 9999999, 1) $taInts.Value2 = Random(999999, 9999999, 1) ConsoleWrite(">Send Value1: " & $taInts.Value1 & " Value2: " & $taInts.Value2) _CommSendByteArray(DllStructGetPtr($taInts), 4 * 2, 1) ;4*2=8 bytes to be sent Sleep(30) ConsoleWrite(@CRLF & ">Read" & @CRLF) If _CommGetInputCount() Then For $i = 1 To 2 ;read the two lines Local $sLine = _CommGetLine() ConsoleWrite($sLine) ;Print to Console. Next EndIf EndFunc ;==>Send_Value_To_Arduino  //structure to cast from 4 bytes to long because arudino int is 16-bit (2-byte) union tLong { unsigned long value; byte bytes[4]; }; tLong g_Value1; tLong g_Value2; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: while (Serial.available() < 8) {} // Wait 'till there are 8 Bytes waiting g_Value1.bytes[0] = Serial.read(); g_Value1.bytes[1] = Serial.read(); g_Value1.bytes[2] = Serial.read(); g_Value1.bytes[3] = Serial.read(); g_Value2.bytes[0] = Serial.read(); g_Value2.bytes[1] = Serial.read(); g_Value2.bytes[2] = Serial.read(); g_Value2.bytes[3] = Serial.read(); Serial.println("Value1: " + String(g_Value1.value)); Serial.println("Value2: " + String(g_Value2.value)); }  Alternative way without using struct union part. long g_Value1; long g_Value2; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: while (Serial.available() < 8) {} // Wait 'till there are 8 Bytes waiting Serial.readBytes((char*)&g_Value1,4); Serial.readBytes((char*)&g_Value2,4); Serial.println("Value1: " + String(g_Value1)); Serial.println("Value2: " + String(g_Value2)); } Saludos Edited May 21, 2020 by Danyfirex  Danysys.com     AutoIt...  UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection  PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut  Â
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