Jump to content

CommMG _CommGetString


JBAU
 Share

Recommended Posts

I have this on my UNO R3;

#define led1 13
 
// Variable that will contain the character received via the serial port

int Received = ' ';
 
void setup() {

// Open the serial port at 9600, Define the pin led1 as OUTPUT

   Serial.begin(9600);
   pinMode(led1, OUTPUT);
}
 
void loop() {
   if (Serial.available() > 0) {        // If the serial buffer contains data
      Received = Serial.read();         // Read the received data
      switch (Received){                // Interpret the data received and act accordingly
         case '0':
            digitalWrite(led1,LOW);     // If I get 0 turn off led1
            Serial.print("1OFF");
            break;
         case '1':
            digitalWrite(led1,HIGH);    // If I get 1 turn on led1
            Serial.print("1ON");
            break;
       }}}

And Using;

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include 'CommMG.au3'

;~ Declare variable $Red & $Green for easier explanation

Global $Red = 0xFF0000
Global $Green = 0x00FF00

#Region ### START Koda GUI section ### Form=

; ~ Creation of the main Gui Position 402px_Right , 120px_Down , 389px_Wide & 341px_High

$Form1 = GUICreate ( "Arduino GUI" , 402 , 120 , 389 , 341 )

;~ $Led1 30px in size & Assigned $Red

$Led1 = GUICtrlCreateGraphic ( 10 , 10 , 30 , 30 )
GUICtrlSetGraphic ( - 1 , $GUI_GR_COLOR , $Red , $Red )
GUICtrlSetGraphic ( - 1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 )
;~ Creation of ~ 3 buttons

$Button1 = GUICtrlCreateButton ( "Pin 13" , 50 , 10 , 55 , 30 )

GUISetState (@SW_SHOW)

#EndRegion ### END Koda GUI section ###

;~ Com Serial Settings

Global $Com_Port = 3
Global $BitPerSecond = 9600
Global $BitDati = 8
Global $Parity = 0
Global $BitStop = 1
Global $FlowControl = 2
Global $sErr
_CommSetPort ( $Com_Port , $sErr , $BitPerSecond , $BitDati , $Parity , $BitStop , $FlowControl )
Global $Button1_press = True

;~ $BS_PUSHBUTTON variable declaration , management of the buttons

Global $BS_PUSHBUTTON
While 1
     $ReceivedFromCom = _CommGetString ( )
     Sleep ( 20 )

;~ ConsoleWrite ("Received =" & @ CRLF & $ ReceivedFromCom)

     Switch $ReceivedFromCom
       Case "1ON"
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_COLOR , $Green , $Green )
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 )
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_REFRESH )
       Case "1OFF"
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_COLOR , $Red , $Red )
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 )
         GUICtrlSetGraphic ( $Led1 , $GUI_GR_REFRESH )
     EndSwitch
     $nMsg = GUIGetMsg ( )
     Switch $nMsg
         Case $GUI_EVENT_CLOSE
             Exit
         Case $Button1
             If ( $Button1_press = False ) Then
                 _CommSendString ( "0" ) ;Turn OFF led1
                 $Button1_press = True
             Else
                 _CommSendString ( "1" ) ;Turn ON led1
                 $Button1_press = False
             EndIf
     EndSwitch
WEnd

So i would like to draw the $led1 state using _CommGetString to set the LED green if Pin13 is active.

So when the gui is launched, i know exactly what state Pin13 is at and nor just rewriting the current state.

Salute to Martin :ILA2:

Link to comment
Share on other sites

Hi,

Welcome to the autoit forum :)

Try the Serial.println function instead of the Serial.print one.

Also, add a ConsoleWrite debug of the $ReceivedFromCom variable to check where the problem comes from ;)

Br, FireFox.

Link to comment
Share on other sites

Your code works fine, check that your arduino is not on the COM Port 1.

However, I rewrote the scripts in a cleaner way :

#define led1 13

// Variable that will contain the character received via the serial port

char Received = '0';

void setup() {
  // Open the serial port at 9600, Define the pin led1 as OUTPUT

  Serial.begin(9600);
  pinMode(led1, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {        // If the serial buffer contains data
    Received = Serial.read();         // Read the received data

    switch (Received){                // Interpret the data received and act accordingly
      case '0':
        digitalWrite(led1, LOW);     // If I get 0 turn off led1
        Serial.print("1OFF");
        break;
      case '1':
        digitalWrite(led1, HIGH);    // If I get 1 turn on led1
        Serial.print("1ON");
        break;
    }
  }
}
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include "CommMG.au3"

;~ Com Serial Settings
Global $Com_Port = 3, $BitPerSecond = 9600, $BitDati = 8, $Parity = 0, $BitStop = 1, $FlowControl = 2, $sErr = ""
_CommSetPort($Com_Port, $sErr, $BitPerSecond, $BitDati, $Parity, $BitStop, $FlowControl)

Global $blLEDON = True

#region GUI
Global $hForm = 0, $iGrLed1 = 0, $iButton1 = 0

$hForm = GUICreate("Arduino GUI")

$iGrLed1 = GUICtrlCreateGraphic(10, 10, 30, 30)
GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_RED, $COLOR_RED)
GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30)

$iButton1 = GUICtrlCreateButton("Pin 13", 50, 10, 55, 30)

GUISetState(@SW_SHOW, $hForm)
#endregion GUI

Local $iMsg = 0, $sReceivedFromCom = ""

While 1
Sleep(20)

$iMsg = GUIGetMsg()
Switch $iMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $iButton1
   If Not $blLEDON Then
    _CommSendString("0") ;Turn OFF led1
   Else
    _CommSendString("1") ;Turn ON led1
   EndIf

   $blLEDON = Not $blLEDON
EndSwitch

$sReceivedFromCom = _CommGetString()
If $sReceivedFromCom = "" Then ContinueLoop

ConsoleWrite("Received: " & $sReceivedFromCom & @CRLF)

Switch $sReceivedFromCom
  Case "1ON"
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_GREEN, $COLOR_GREEN)
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30)
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_REFRESH)
  Case "1OFF"
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_RED, $COLOR_RED)
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30)
   GUICtrlSetGraphic($iGrLed1, $GUI_GR_REFRESH)
EndSwitch
WEnd

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Looks Prettier, Led Graphic now stays red , nice try though

Realistically, probably just need a way to redraw/refresh the gui dependent on whats in the buffer.

Edited by JBAU
Link to comment
Share on other sites

Your version, led in gui stays red always, changes on uno.

Mine same as before, works gui and board.

What i wanted was to be able to close the script while the uno led was active, & re-run the script, with the gui graphic of the led be green, not red.

I don't think is possible without actually polling the pin & not the buffer, i could be wrong.

I am thankful as i can see a neater or more compounded way of writing things , thanks!

Link to comment
Share on other sites

Your version, led in gui stays red always, changes on uno.

Then something's wrong with what you have done.

What i wanted was to be able to close the script while the uno led was active, & re-run the script, with the gui graphic of the led be green, not red.

Yes it's possible, send another command to ask for the led status, then set it on the GUI.

Br, FireFox.

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