Jump to content

Log changes to a Control in External Program


Steven_H
 Share

Go to solution Solved by Zedna,

Recommended Posts

Hello I'm new to AutoIt, but impressed so far with how well it works to solve an ongoing problem I have had. Firstly my script below does work and it does do what I need it to, but I'm sure it is very badly written and consumes 25% CPU. I'm hoping someone can point out how it could be improved or done in a much better way.

#include <FileConstants.au3>

; Create file in same folder as script
$sFileName = "c:\Log\Amaya_Log.txt"

$sID = ""
$oLd = ""
Run("AMAYA.exe")

Func getAmaya()
While 1
if WinExists("AMAYA OS Lite") Then
   $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]")
    If ($sID == $oLD) Then
        ContinueLoop
    Else
        ConsoleWrite($sID & @CRLF)
        ; Append a line
      Local $hFileOpen = FileOpen($sFileName, $FO_APPEND)
      FileWrite($hFileOpen, @CRLF & $sID)
      FileClose($hFileOpen)
        $oLd = $sID
    EndIf
Sleep (50)
EndIf
WEnd
EndFunc
getAmaya()

What I am doing here is reading a control from an old Embroidery Machine program which does not save external logs, it only shows these inside the program and once it's closed they are lost. In the attached image the Control I am capturing is at the bottom left of the screen which has a dropdown box, currently showing "20:22:30 - Cluster 01 - Head 01 - Design has completed sewing." The script constantly Loops checking if this value changes and if it does, writes it to a text file. 

I was hoping there was some sort of OnChange or EnChange event I could use, but as this is an external Program not a GUI I've created in AutoIt, I couldn't find a less resource intensive way to achieve this.

I will probably also log several other controls from the program as well, but initially just want to get this part running more efficiently and learn from others in the forums.

Please don't be too harsh, but I'm aware this is probably the total wrong way to achieve this.

Thanks in advance

 

 

 

OS Lite Screen.png

Link to comment
Share on other sites

  • Solution

fix:                 Sleep(50) ; --> added by Zedna

#include <FileConstants.au3>

; Create file in same folder as script
$sFileName = "c:\Log\Amaya_Log.txt"

$sID = ""
$oLd = ""
Run("AMAYA.exe")

Func getAmaya()
    While 1
        If WinExists("AMAYA OS Lite") Then
            $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]")
            If ($sID == $oLd) Then
                Sleep(50) ; --> added by Zedna
                ContinueLoop
            Else
                ConsoleWrite($sID & @CRLF)
                ; Append a line
                Local $hFileOpen = FileOpen($sFileName, $FO_APPEND)
                FileWrite($hFileOpen, @CRLF & $sID)
                FileClose($hFileOpen)
                $oLd = $sID
            EndIf
            Sleep(50)
        EndIf
    WEnd
EndFunc   ;==>getAmaya

getAmaya()

 

Link to comment
Share on other sites

You only need to open the file once at the top of the script and as Zedna mentioned move the Sleep function although I would have placed it outside of the If statement for example (untested):

#include <FileConstants.au3>
;~ Set a hotkey Escape to exit the script
HotKeySet("{ESC}", "_ExitScript")

; Create file in same folder as script
Global $sFileName = "c:\Log\Amaya_Log.txt"
Global $hFileOpen = FileOpen($sFileName, $FO_APPEND)

Run("AMAYA.exe")

getAmaya()

Func getAmaya()
    Local $sID = "", $oLd = ""
    While 1
        If WinExists("AMAYA OS Lite") Then
            $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]")
            If ($sID == $oLD) Then
                ContinueLoop
            Else
                ConsoleWrite($sID & @CRLF)
                ; Append a line
                FileWrite($hFileOpen, $sID & @CRLF)
                $oLd = $sID
            EndIf
        EndIf
        Sleep (50)
    WEnd
EndFunc

Func _ExitScript()
    FileClose($hFileOpen)
    Exit
EndFunc

 

Link to comment
Share on other sites

Thanks to you both, will give this a try once there is a break in the machines use.

Looks like I wasn't a millions miles away, I had the Sleep (50) in the wrong place and not specifying Local/Global. I was going to to look at a way to exit the script without having to exit it from the System Tray, saved me searching @Subz.

 

Link to comment
Share on other sites

  • 2 weeks later...

Sorry for not replying and confirming the solution sooner. I marked @Zedna as the solution because it solved the problem of the high CPU usage. Although I combined it with @Subzas well. The current version I am now using is this:

 

#include <FileConstants.au3>
;~ Set a hotkey Escape to exit the script
HotKeySet("{ESC}", "_ExitScript")

; Create file in same folder as script
Global $sFileName = "c:\Log\Amaya_Log.txt"
Global $hFileOpen = FileOpen($sFileName, $FO_APPEND)

Run("AMAYA.exe")

getAmaya()

Func getAmaya()
    Local $sID = "", $oLd = ""
    While 1
        If WinExists("AMAYA OS Lite") Then
            $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]")
            If ($sID == $oLD) Then
                Sleep(50)
                ContinueLoop
            Else
                ConsoleWrite($sID & @CRLF)
                ; Append a line
                FileWrite($hFileOpen, $sID & @CRLF)
                $oLd = $sID
            EndIf
        EndIf
        Sleep (50)
    WEnd
EndFunc

Func _ExitScript()
    FileClose($hFileOpen)
    Exit
EndFunc

Currently working on slightly more complicated version which is pulling multiple controls and trying to then submit as Json to an MQTT broker, but will post that as its own post. Thanks for the help.

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