Jump to content

Recommended Posts

Posted

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:

#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
    }
  }
}

 

Posted (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 by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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

 

#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 by Danyfirex

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
  • Recently Browsing   0 members

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