Jump to content

Script not running flawlessly


Recommended Posts

I have this script that I've been working on to help automate a CNC wire stripper and labeller.

It is supposed to pull data from an excel file,

put it into an array,

filter that array by an input,

and then use the info in that array to direct a landing platform to have the correct bin in place.

The main problem is that although it runs, it is not reliable. Once in a while the arduino script won't upload (that's more of an issue between the PC and the arduino, not AutoIT), the bin indexing does not always work as intended, and it can't handle duplicate job names (I don't know a way around this, I've just been re-naming those jobs to make them unique). As well as some errors like moving one more bin than it should or not moving when it is supposed to.

I am pretty new to AutoIT, so I imagine there are better ways I could be handling some things. Any input would be appreciated.

You'll find my code both attached and below:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         FattysFTW

 
 Script Function:
    AutoIt script which creates a graphical interface which is then used to control the CAYMAN wire stripper
    via pulling data from an excel sheet, located at a path input by the user.
    The data from this excel sheet is then cross-referenced with data input by the user.
    

#ce ----------------------------------------------------------------------------

;   Script Start:

#include <Constants.au3>
#include <AutoItConstants.au3>
#include <Excel.au3>
#include <Array.au3>
#RequireAdmin

Global $TotalBins = 6 

;   GUI Initialize:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=Z:\Anthony\CAYMAN Automator\Testing\Automator_GUI.kxf
Global $Form1 = GUICreate("Cayman Automator", 570, 260, 187, 125)
Global $Job1 = GUICtrlCreateInput("1", 48, 32, 121, 21)
Global $Job2 = GUICtrlCreateInput("2", 48, 64, 121, 21)
Global $Job3 = GUICtrlCreateInput("3", 48, 96, 121, 21)
Global $Job4 = GUICtrlCreateInput("4", 48, 128, 121, 21)
Global $Job5 = GUICtrlCreateInput("5", 48, 160, 121, 21)
Global $Job6 = GUICtrlCreateInput("6", 48, 192, 121, 21)
Global $BinLabel = GUICtrlCreateLabel("Schlueniger Wire Stripper Automation Script Version 2.1", 40, 8, 300, 17, $SS_CENTER)
Global $BinLabel1 = GUICtrlCreateLabel("Bin 1:", 8, 32, 28, 17)
Global $BinLabel2 = GUICtrlCreateLabel("Bin 2:", 8, 64, 28, 17)
Global $BinLabel3 = GUICtrlCreateLabel("Bin 3:", 8, 96, 28, 17)
Global $BinLabel4 = GUICtrlCreateLabel("Bin 4:", 8, 128, 28, 17)
Global $BinLabel5 = GUICtrlCreateLabel("Bin 5:", 8, 160, 28, 17)
Global $BinLabel6 = GUICtrlCreateLabel("Bin 6:", 8, 192, 28, 17)
Global $WireTypeInput = GUICtrlCreateCombo("Wire060", 192, 32, 150, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL,$WS_VSCROLL))
GUICtrlSetData(-1, "Wire061|Wire062|Wire064|Wire081|Wire082|Wire084|Wire100|Wire101|Wire102|Wire104|Wire106|Wire109|Wire120|Wire121|Wire122|Wire1221|Wire124|Wire125|Wire126|Wire128|Wire129|Wire139|Wire140|Wire141|Wire142|Wire143|Wire144|Wire145|Wire146|Wire148|Wire149|Wire150|Wire151|Wire152|Wire153|Wire157|Wire158|Wire160|Wire161|Wire162|Wire1621|Wire163|Wire164|Wire165|Wire166|Wire167|Wire168|Wire169|Wire170|Wire171|Wire172|Wire175|Wire176|Wire177|Wire178|Wire179|Wire180|Wire181|Wire182|Wire183|Wire185|Wire188|Wire192|Wire202|Wire220|Wire221|Wire222|Wire223")
Global $StartBtn = GUICtrlCreateButton("Start", 192, 192, 150, 25)
Global $Progress1 = GUICtrlCreateProgress(192, 160, 150, 17)
Global $ProgressLabel = GUICtrlCreateLabel("Script Progress:", 192, 136, 180, 17)
Global $MoveOneBtn = GUICtrlCreateButton("Move One Slot", 192, 64, 150, 25)
Global $PathInput = GUICtrlCreateInput("C:\Users\Public\Cayman\ImpExp\Cayman_export.xlsx", 8, 224, 550, 21)
Global $ScriptRunning = GUICtrlCreateLabel(" Script Not Running", 192, 104, 150, 17, $SS_CENTER)
GUICtrlSetBkColor(-1, 0xFF0000)
Global $Instructions1 = GUICtrlCreateLabel("Instructions for use:", 360, 32, 300, 17)
Global $Instructions2 = GUICtrlCreateLabel("· Export Wirelist from Cayman", 360, 48, 300, 17)
Global $Instructions3 = GUICtrlCreateLabel("· Input job numbers for each bin", 360, 64, 300, 17)
Global $Instructions4 = GUICtrlCreateLabel("· Input the filepath to the exported wirelist", 360, 80, 300, 17)
Global $Instructions4 = GUICtrlCreateLabel("· Select wiretype", 360, 96, 300, 17)
Global $Instructions5 = GUICtrlCreateLabel("· Open arduino script Step_ToMetal", 360, 112, 300, 17)
Global $Instructions6 = GUICtrlCreateLabel("· Start script", 360, 128, 300, 17)
Global $Instructions7 = GUICtrlCreateLabel("· Start stripping process", 360, 144, 300, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Path = GUICtrlRead($PathInput)








;   GUI Input cases:


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    
        Case $GUI_EVENT_CLOSE                                                       ; if the GUI is closed,
            ExitLoop                                                                ; close it
        
        Case $StartBtn                                                              ; if the start button is pressed,
            
            GUICtrlSetData($ScriptRunning, " Script Running")                       ; change the running indicator,
            GUICtrlSetBkColor($ScriptRunning, 0x00FF00)
            
            Cayman()                                                                ; run the main program,
            
            ClearGUI()                                                              ; reset the progress bar & running indicator
        
        Case $MoveOneBtn                                                            ; if the move one button is pressed,
            MotorStepOne()                                                          ; move the motor one slot
            
        Case $PathInput                                                             ; if the excel file path is changed,
            Global $Path = GUICtrlRead($PathInput)                                  ; reload it
        
    EndSwitch
WEnd
GUIDelete()








;   main program:


Func Cayman()                                                                       
    
    ; Set variables for user inputs here & pull data from excel sheet

    ExcelRead()
    
    Global $jobBin = [GUICtrlRead($Job1), GUICtrlRead($Job2), GUICtrlRead($Job3), GUICtrlRead($Job4), GUICtrlRead($Job5), GUICtrlRead($Job6)] ; Put the input job numbers in an array where the array slot (0-5) is able to be called and returns a string of the job number
    Global $iWire = 0
    Global $excelTotalWire = UBound($excelWireType)-1                               ; grab the number of values in the array of wire types, aka the total number of wires & the number of rows in the excel sheet
    Global $currentBin = 1
    
    
    
    ; Takes all wires from excel sheet and puts only the ones which will be run into 2 new arrays
    Global $iCount1 = 0
    Global $iCount2 = 0
    Global $wireType[1]
    Global $wireJob[1]
    
    While $iCount1 <= $excelTotalWire
        if $excelWireType[$iCount1] = GUICtrlRead($WireTypeInput) Then                      
            _ArrayInsert($wireType, $iCount2, $excelWireType[$iCount1])
            _ArrayInsert($wireJob, $iCount2, $excelWireJob[$iCount1])
            $iCount2 +=1
        EndIf
        $iCount1 +=1
    WEnd
    
    Global $totalWire = UBound($wireType)-1
    
    ; For testing purposes:
    
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "$wireJob: "&$wireJob[$iWire] & @CRLF & "$jobBin: "&$jobBin[$currentBin])
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "This is line 1" & @CRLF & @error & @CRLF & "This is line 3")
;   _ArrayDisplay($excelWireType, "Excel type Array Data:")
;   _ArrayDisplay($wireType, "Running type Array Data:")
;   _ArrayDisplay($wireJob, "Running job Array Data:")

;   #cs ---------------------------------------------------------------------------------------------------------
;   Comment section for testing purposes so that loop does not run forever without arduino hooked up
    
    While $iWire < $totalWire                                                       ; While the wire we are on is less than the total number of wires,
    
        While $wireJob[$iWire] <> $jobBin[$currentBin-1]                            ; if the current bin is not equal to the bin we want
            MotorStepOne()                                                          ; move the motor until we are on the correct bin
;           Sleep(20000)                                                            ; wait for the motor to move
            
;           For testing purposes: comment out MotorStepOne & sleep() and uncomment the message below to not run the motor, or comment out sleep and uncomment this to use messagebox to move motor instead of timer
            MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "This wire's job: " & $wireJob[$iWire] & @CRLF & "This wire's bin: " & $jobBin[$currentBin] & @CRLF & "Current Bin: " & $currentBin)
            
            $currentBin +=1
                if $currentBin = 7  Then                                            ; if we return to the first bin
                    $currentBin = 1                                                 ; reset the index
                EndIf
        WEnd

        WinWait("[CLASS:#32770]")                                                   ; Wait for a CAYMAN popup dialogue,
        
        Local $popupText = ControlGetText("[CLASS:#32770]","","[CLASSNN:Static4]")  ; Pull the text from the popup,
            
            if $popupText = "HotStamp Timeout." Then                                ; if it's a hotstamp error popup
                $iWire = $totalWire                                                 ; end the while loop,
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                                                                                    ; and here is where a light would turn on and the program would break
                                                                                    ; IF WE HAD ONE
                
            ElseIf $popupText = "Waste piece will now be ejected." Then             ; if it's a waste piece popup
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                
            ElseIf $popupText = "Quantity complete." Then                           ; if it's a popup saying the wire is done
                GUICtrlSetData($Progress1, ($iWire/$totalWire)*100)                 ; update the progress bar
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                $iWire +=1                                                          ; Index the wire

                EndIf
    WEnd

;   #ce ---------------------------------------------------------------------------------------------------------

EndFunc




;  Arduino control function:

Func MotorStepOne()                                                                 ; Move the motor one position using the arduino script
    WinWait("Step_ToMetal | Arduino 1.8.5")
    WinActivate("Step_ToMetal | Arduino 1.8.5", "")
    Send("^u")
    WinActivate("Cayman Automator", "")
EndFunc




;   Clear GUI function:

Func ClearGUI()
    GUICtrlSetData($Progress1, 0)                                                   ; Set progress bar to 0
    GUICtrlSetData($ScriptRunning, " Script Not Running")                           ; Change label to NOT running
    GUICtrlSetBkColor($ScriptRunning, 0xFF0000)                                     ; Change label color to red
EndFunc




;   Read excel file at $Path function:

Func ExcelRead()
    $oExcel = _Excel_Open()
    Local $oWorkbook = _Excel_BookOpen($oExcel, $Path)
    Global $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("K:K"), 2)
    Global $excelWireJob = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("D:D"), 2)
    _Excel_Close($oExcel, false, true)
EndFunc

 

Cayman Export Test Fixed.xlsx

Automator.au3

Link to comment
Share on other sites

I get the error:

"C:\Users\W10\Desktop\New AutoIt3.au3" (233) : ==> Variable must be of type "Object".:
Global $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("K:K"), 2)
Global $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook^ ERROR

Try commenting out your #requireadmin and see if any error comes up.

Im getting an error in _Excel_Open, so maybe that's why rangeread fails

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

I do not receive those errors, and the #requireadmin is necessary to control the CAYMAN software.

I don't have any real issues with the code itsself, it creates the arrays just fine and works the majority of the time. I just need it to be more reliable.

I am going to try putting some sleep timers in certain places so that it is not trying to do everything at once, and see if that makes a difference.

Link to comment
Share on other sites

On 19-7-2018 at 10:27 PM, careca said:

I get the error:

"C:\Users\W10\Desktop\New AutoIt3.au3" (233) : ==> Variable must be of type "Object".:
Global $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("K:K"), 2)
Global $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook^ ERROR

Try commenting out your #requireadmin and see if any error comes up.

Im getting an error in _Excel_Open, so maybe that's why rangeread fails

you are getting an error because the filename/path is incorrect , I had it as well and once I changed the path and the filename it runs.

However it is waiting for a popup from cayman that is NOT coming .

 

 

to op:

As for the error of not moving or moving to much, could you elaborate a bit more about how and when it is supposed to move, the input and output of the machine, so we can simulate that .

 

Personally I would declare  $excelwiretype and $excelwirejob as global without setting them equal to anything  at the very beginning of your script that will get rid of 2 warnings you get. Then just remove the global tags in the func _exelread()

You can also pass variables to and from functions by filling them in the parentheses  or using Return. I ran the script , set the wire type to 160, it works up to the point where it awaits the cayman program (         WinWait("[CLASS:#32770]")          )

 

last but not least could you elaborate a bit more on handling same jobs?
 

 

Link to comment
Share on other sites

Thank you for the input, I have revised the script and it works great now! I've attached it with the updates I have made for anyone's reference. (the main changes are some logic order revision and some wait timers, as well as it starting the stripping process itself)

The only two issues remaining are having duplicate jobs and the arduino upload issue.

 

Say I need to make 2 duplicates of job 1000. In the cayman software, they are both labelled as job 1000, are thus put into the excel file as job 1000, and the input for bins 1 and 2 (or whatever) will be 1000. I can't think of a way for the code to handle this automatically, because right now it would just put all of it in the first bin I believe. There is a method around this, (by renaming one of the duplicates within the excel file) so it is not the end of the world.

The second issue, however, is a real problem. Every so often (about once every 50 uploads or so) the arduino will fail to upload its Move_ToMetal script (or any script, for that matter) due to some error causing the board to not be seen on the COM port. It is a minor error, and simply re-uploading immediately following the error will work. However, the arduino IDE does not have any kind of output dialogue or searchable text. I need to either find a way to get a winwait-able popup from the IDE when this occurs, or find a way to search the IDE for the error. You can find a screenshot of the error attached. Maybe I can get the color of a pixel within the error bar? it is not orange if there is no error, but I don't want to rely on the window being in the same place everytime.

Thanks again for any input.

5b55d79507408_COMPORTErrorMessagePicture.JPG.756645a3fab563bc4cb77de53989d10d.JPG

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         FattysFTW

 
 Script Function:
    AutoIt script which creates a graphical interface which is then used to control the CAYMAN wire stripper
    via pulling data from an excel sheet, located at a path input by the user.
    The data from this excel sheet is then cross-referenced with data input by the user.
    

#ce ----------------------------------------------------------------------------

;   Script Start:

#include <Constants.au3>
#include <AutoItConstants.au3>
#include <Excel.au3>
#include <Array.au3>
#RequireAdmin

Global $TotalBins = 6
Global $excelWireType
Global $excelWireJob

;   GUI Initialize:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=Z:\Anthony\CAYMAN Automator\Testing\Automator_GUI.kxf
Global $Form1 = GUICreate("Cayman Automator", 570, 260, 187, 125)
Global $Job1 = GUICtrlCreateInput("1", 48, 32, 121, 21)
Global $Job2 = GUICtrlCreateInput("2", 48, 64, 121, 21)
Global $Job3 = GUICtrlCreateInput("3", 48, 96, 121, 21)
Global $Job4 = GUICtrlCreateInput("4", 48, 128, 121, 21)
Global $Job5 = GUICtrlCreateInput("5", 48, 160, 121, 21)
Global $Job6 = GUICtrlCreateInput("6", 48, 192, 121, 21)
Global $BinLabel = GUICtrlCreateLabel("Schlueniger Wire Stripper Automation Script Version 2.1", 40, 8, 300, 17, $SS_CENTER)
Global $BinLabel1 = GUICtrlCreateLabel("Bin 1:", 8, 32, 28, 17)
Global $BinLabel2 = GUICtrlCreateLabel("Bin 2:", 8, 64, 28, 17)
Global $BinLabel3 = GUICtrlCreateLabel("Bin 3:", 8, 96, 28, 17)
Global $BinLabel4 = GUICtrlCreateLabel("Bin 4:", 8, 128, 28, 17)
Global $BinLabel5 = GUICtrlCreateLabel("Bin 5:", 8, 160, 28, 17)
Global $BinLabel6 = GUICtrlCreateLabel("Bin 6:", 8, 192, 28, 17)
Global $WireTypeInput = GUICtrlCreateCombo("Wire060", 192, 32, 150, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL,$WS_VSCROLL))
GUICtrlSetData(-1, "Wire061|Wire062|Wire064|Wire081|Wire082|Wire084|Wire100|Wire101|Wire102|Wire104|Wire106|Wire109|Wire120|Wire121|Wire122|Wire1221|Wire124|Wire125|Wire126|Wire128|Wire129|Wire139|Wire140|Wire141|Wire142|Wire143|Wire144|Wire145|Wire146|Wire148|Wire149|Wire150|Wire151|Wire152|Wire153|Wire157|Wire158|Wire160|Wire161|Wire162|Wire1621|Wire163|Wire164|Wire165|Wire166|Wire167|Wire168|Wire169|Wire170|Wire171|Wire172|Wire175|Wire176|Wire177|Wire178|Wire179|Wire180|Wire181|Wire182|Wire183|Wire185|Wire188|Wire192|Wire202|Wire220|Wire221|Wire222|Wire223")
Global $StartBtn = GUICtrlCreateButton("Start", 192, 192, 150, 25)
Global $Progress1 = GUICtrlCreateProgress(192, 160, 150, 17)
Global $ProgressLabel = GUICtrlCreateLabel("Script Progress:", 192, 136, 180, 17)
Global $MoveOneBtn = GUICtrlCreateButton("Move One Slot", 192, 64, 150, 25)
Global $PathInput = GUICtrlCreateInput("C:\Users\Public\Cayman\ImpExp\Cayman_export.txt", 8, 224, 550, 21)
Global $ScriptRunning = GUICtrlCreateLabel(" Script Not Running", 192, 104, 150, 17, $SS_CENTER)
GUICtrlSetBkColor(-1, 0xFF0000)
Global $Instructions1 = GUICtrlCreateLabel("Instructions for use:", 360, 32, 300, 17)
Global $Instructions2 = GUICtrlCreateLabel("· Export Wirelist from Cayman", 360, 48, 300, 17)
Global $Instructions3 = GUICtrlCreateLabel("· Input job numbers for each bin", 360, 64, 300, 17)
Global $Instructions4 = GUICtrlCreateLabel("· Input the filepath to the exported wirelist", 360, 80, 300, 17)
Global $Instructions4 = GUICtrlCreateLabel("· Select wiretype", 360, 96, 300, 17)
Global $Instructions5 = GUICtrlCreateLabel("· Open arduino script Step_ToMetal", 360, 112, 300, 17)
Global $Instructions6 = GUICtrlCreateLabel("· Start script", 360, 128, 300, 17)
Global $Instructions7 = GUICtrlCreateLabel("· Start stripping process", 360, 144, 300, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Path = GUICtrlRead($PathInput)








;   GUI Input cases:


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    
        Case $GUI_EVENT_CLOSE                                                       ; if the GUI is closed,
            ExitLoop                                                                ; close it
        
        Case $StartBtn                                                              ; if the start button is pressed,
            
            GUICtrlSetData($ScriptRunning, " Script Running")                       ; change the running indicator,
            GUICtrlSetBkColor($ScriptRunning, 0x00FF00)
            
            Cayman()                                                                ; run the main program,
            
            ClearGUI()                                                              ; reset the progress bar & running indicator
        
        Case $MoveOneBtn                                                            ; if the move one button is pressed,
            MotorStepOne()                                                          ; move the motor one slot
            
        Case $PathInput                                                             ; if the excel file path is changed,
            Global $Path = GUICtrlRead($PathInput)                                  ; reload it
        
    EndSwitch
WEnd
GUIDelete()








;   main program:


Func Cayman()                                                                       
    
    ; Set variables for user inputs here & pull data from excel sheet

    ExcelRead()
    
    Global $jobBin = [GUICtrlRead($Job1), GUICtrlRead($Job2), GUICtrlRead($Job3), GUICtrlRead($Job4), GUICtrlRead($Job5), GUICtrlRead($Job6)] ; Put the input job numbers in an array where the array slot (0-5) is able to be called and returns a string of the job number
    Global $iWire = 0
    Global $excelTotalWire = UBound($excelWireType)-1                               ; grab the number of values in the array of wire types, aka the total number of wires & the number of rows in the excel sheet
    Global $currentBin = 1
    
    
    
    ; Takes all wires from excel sheet and puts only the ones which will be run into 2 new arrays
    Global $iCount1 = 0
    Global $iCount2 = 0
    Global $wireType[1]
    Global $wireJob[1]
    
    While $iCount1 <= $excelTotalWire
        if $excelWireType[$iCount1] = GUICtrlRead($WireTypeInput) Then                      
            _ArrayInsert($wireType, $iCount2, $excelWireType[$iCount1])
            _ArrayInsert($wireJob, $iCount2, $excelWireJob[$iCount1])
            $iCount2 +=1
        EndIf
        $iCount1 +=1
    WEnd
    
    Global $totalWire = UBound($wireType)-1
    
    ; For testing purposes:
    
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "$wireJob: "&$wireJob[$iWire] & @CRLF & "$jobBin: "&$jobBin[$currentBin])
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "This is line 1" & @CRLF & @error & @CRLF & "This is line 3")
;   _ArrayDisplay($excelWireType, "Excel type Array Data:")
;   _ArrayDisplay($wireType, "Running type Array Data:")
;   _ArrayDisplay($wireJob, "Running job Array Data:")
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "Total number of wires: " & $totalWire)

;   #cs ---------------------------------------------------------------------------------------------------------
;   Comment section for testing purposes so that loop does not run forever without arduino hooked up
    
    
    
    
;   Check the slot for wire 0 & move to it:

    While $wireJob[$iWire] <> $jobBin[$currentBin-1]                                ; if the current bin is not equal to the bin we want
        MotorStepOne()                                                              ; move the motor until we are on the correct bin
        Sleep(20000)                                                                ; wait for the motor to move
            
;       For testing purposes: comment out MotorStepOne & sleep() and uncomment the message below to not run the motor, or comment out sleep and uncomment this to use messagebox to move motor instead of timer
;       MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "Next wire's job: " & $wireJob[$iWire] & @CRLF & "This bin's job: " & $jobBin[$currentBin-1] & @CRLF & "Current Bin: " & $currentBin)
            
        $currentBin +=1
            if $currentBin > 6  Then                                                ; if we return to the first bin
                $currentBin = 1                                                     ; reset the index
            EndIf
    WEnd
        
        
        
        
    WinActivate("[CLASS:CaymanMainFrame]")
    ControlClick("[CLASS:CaymanMainFrame]", "", 59403, "left", 1, 350, 21)
        
        
    While $iWire < $totalWire                                                       ; While the wire we are on is less than the total number of wires (-1 for the empty array space),
    

        WinWait("[CLASS:#32770]")                                                   ; Wait for a CAYMAN popup dialogue,
        
        Sleep(200)                                                                  ; wait 0.2s
        

        
;       Local $popupText = "Quantity complete."                                     ; For testing purposes, don't use the cayman popup
        Local $popupText = ControlGetText("[CLASS:#32770]","","[CLASSNN:Static4]")  ; Pull the text from the popup,
            
            if $popupText = "HotStamp Timeout." Then                                ; if it's a hotstamp error popup
                $iWire = $totalWire                                                 ; end the while loop,
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                                                                                    ; and here is where a light would turn on and the program would break
                                                                                    ; IF WE HAD ONE
                
            ElseIf $popupText = "Waste piece will now be ejected." Then             ; if it's a waste piece popup
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
            
            ElseIf $popupText = "Production Complete." Then                         ; if it's a waste piece popup
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
;               MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "Just stripped wire: " & $iWire)
                Send("{F1}")
                $iWire +=1                                                          ; Index the wire
                
            ElseIf $popupText = "Quantity complete." Then                           ; if it's a popup saying the wire is done
                GUICtrlSetData($Progress1, ($iWire+1/$totalWire)*100)               ; update the progress bar
;                   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "Just stripped wire: " & $iWire)

                if $iWire+1 < $totalWire Then
                    While $wireJob[$iWire+1] <> $jobBin[$currentBin-1]                  ; if the current bin is not equal to the bin we want
                        MotorStepOne()                                                  ; move the motor until we are on the correct bin
                        Sleep(20000)                                                    ; wait for the motor to move
            
;                       For testing purposes: comment out MotorStepOne & sleep() and uncomment the message below to not run the motor, or comment out sleep and uncomment this to use messagebox to move motor instead of timer
;                       MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "Next wire's job: " & $wireJob[$iWire+1] & @CRLF & "Next Wire: " & $iWire & @CRLF & "Current Bin: " & $currentBin)
            
                        $currentBin +=1
                            if $currentBin > 6  Then                                    ; if we return to the first bin
                                $currentBin = 1                                         ; reset the index
                            EndIf
                    WEnd
                EndIf   
                
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")


                sleep(5000)
                $iWire +=1                                                          ; Index the wire

            EndIf
                

                
    WEnd
    
    While $currentBin <> 1                                                          ; if the current bin is not equal to the starting bin
            MotorStepOne()                                                          ; move the motor until we are on the correct bin
            Sleep(20000)                                                            ; wait for the motor to move
        
            $currentBin +=1
                if $currentBin > 6  Then                                            ; if we return to the first bin
                    $currentBin = 1                                                 ; reset the index
                EndIf
    WEnd
;   #ce ---------------------------------------------------------------------------------------------------------

EndFunc




;  Arduino control function:

Func MotorStepOne()                                                                 ; Move the motor one position using the arduino script
    WinWait("Step_ToMetal | Arduino 1.8.5")
    WinActivate("Step_ToMetal | Arduino 1.8.5", "")
    Send("^u")
    
    Local $arduinoText = ControlGetText("[CLASS:#32770]","","[CLASSNN:Static4]")    ; Pull the text from the popup,
    
    WinActivate("Cayman Automator", "")
EndFunc




;   Clear GUI function:

Func ClearGUI()
    GUICtrlSetData($Progress1, 0)                                                   ; Set progress bar to 0
    GUICtrlSetData($ScriptRunning, " Script Not Running")                           ; Change label to NOT running
    GUICtrlSetBkColor($ScriptRunning, 0xFF0000)                                     ; Change label color to red
EndFunc




;   Read excel file at $Path function:

Func ExcelRead()
    $oExcel = _Excel_Open()
;   Local $oWorkbook = _Excel_BookOpen($oExcel, $Path) -Replaced with section below, which auto-formats the .txt file rather than needing an imported .xlsx
    
    Local $aField1[2] = [1, $xlTextFormat]
    Local $aField2[2] = [2, $xlTextFormat]
    Local $aField3[2] = [3, $xlGeneralFormat]
    Local $aField4[2] = [4, $xlDMYFormat]
    Local $aField5[2] = [5, $xlTextFormat]
    Local $aFieldInfo[5] = [$aField1, $aField2, $aField3, $aField4, $aField5]
    Local $oWorkbook = _Excel_BookOpenText($oExcel, $Path, Default, $xlDelimited, Default, True, "  "&",", $aFieldInfo, ".", ",")
    
    $excelWireType = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("K:K"), 2)
    $excelWireJob = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("D:D"), 2)
    _Excel_Close($oExcel, false, true)
EndFunc
Link to comment
Share on other sites

As a rule of thumb, you should never declare globals inside functions, you could possible place conditions before starting the process, for example if no WireType is found in Excel, you should probably just stop the process.

Basic example: rather than use two separate arrays, used one array from Excel with only the wiretype selected, unfortunately don't know how Cayman function works but gave it my best shot :)

#cs ----------------------------------------------------------------------------
 AutoIt Version: 3.3.14.5
 Author:         FattysFTW
 Script Function:
    AutoIt script which creates a graphical interface which is then used to control the CAYMAN wire stripper
    via pulling data from an excel sheet, located at a path input by the user.
    The data from this excel sheet is then cross-referenced with data input by the user.
#ce ----------------------------------------------------------------------------

#include <Array.au3>
#include <Debug.au3>
#include <Excel.au3>
#include <Constants.au3>
#include <AutoItConstants.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $sXLPath = @ScriptDir & "\Cayman Export Test Fixed.xlsx", $aXLWireJobType[0][2]
Global $idJob1, $idJob2, $idJob3, $idJob4, $idJob5, $idJob6
Global $idWireType, $idProgress, $idScriptStatus

_Cayman_Automator()

Func _Cayman_Automator()
    Local $sWireType
    Local $hGUI = GUICreate("Cayman Automator", 570, 260, 187, 125)
    GUICtrlCreateLabel("Schlueniger Wire Stripper Automation Script Version 2.1", 40, 8, 300, 17, $SS_CENTER)
        GUICtrlCreateLabel("Bin 1:", 8, 32, 28, 17)
    $idJob1 = GUICtrlCreateInput("1", 48, 32, 121, 21)
        GUICtrlCreateLabel("Bin 2:", 8, 64, 28, 17)
    $idJob2 = GUICtrlCreateInput("2", 48, 64, 121, 21)
        GUICtrlCreateLabel("Bin 3:", 8, 96, 28, 17)
    $idJob3 = GUICtrlCreateInput("3", 48, 96, 121, 21)
        GUICtrlCreateLabel("Bin 4:", 8, 128, 28, 17)
    $idJob4 = GUICtrlCreateInput("4", 48, 128, 121, 21)
        GUICtrlCreateLabel("Bin 5:", 8, 160, 28, 17)
    $idJob5 = GUICtrlCreateInput("5", 48, 160, 121, 21)
        GUICtrlCreateLabel("Bin 6:", 8, 192, 28, 17)
    $idJob6 = GUICtrlCreateInput("6", 48, 192, 121, 21)

    $idWireType = GUICtrlCreateCombo("Wire060", 192, 32, 150, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $WS_VSCROLL))
        GUICtrlSetData(-1, "Wire061|Wire062|Wire064|Wire081|Wire082|Wire084|Wire100|Wire101|Wire102|Wire104|Wire106|Wire109|Wire120|Wire121|Wire122|Wire1221|Wire124|Wire125|Wire126|Wire128|Wire129|Wire139|Wire140|Wire141|Wire142|Wire143|Wire144|Wire145|Wire146|Wire148|Wire149|Wire150|Wire151|Wire152|Wire153|Wire157|Wire158|Wire160|Wire161|Wire162|Wire1621|Wire163|Wire164|Wire165|Wire166|Wire167|Wire168|Wire169|Wire170|Wire171|Wire172|Wire175|Wire176|Wire177|Wire178|Wire179|Wire180|Wire181|Wire182|Wire183|Wire185|Wire188|Wire192|Wire202|Wire220|Wire221|Wire222|Wire223", "Wire102")
    Local $idStart = GUICtrlCreateButton("Start", 192, 192, 150, 25)
    GUICtrlCreateLabel("Script Progress:", 192, 136, 180, 17)
        $idProgress = GUICtrlCreateProgress(192, 160, 150, 17)
    Local $idMoveOne = GUICtrlCreateButton("Move One Slot", 192, 64, 150, 25)
    Local $idXLPath = GUICtrlCreateInput($sXLPath, 8, 224, 550, 21)
    $idScriptStatus = GUICtrlCreateLabel(" Script Not Running", 192, 104, 150, 17, $SS_CENTER)
        GUICtrlSetBkColor(-1, 0xFF0000)
    Local $sInstructions = "Instructions for use:" & @CRLF
        $sInstructions &= "· Export Wirelist from Cayman" & @CRLF
        $sInstructions &= "· Input job numbers for each bin" & @CRLF
        $sInstructions &= "· Input the filepath to the exported wirelist" & @CRLF
        $sInstructions &= "· Select wiretype" & @CRLF
        $sInstructions &= "· Open arduino script Step_ToMetal" & @CRLF
        $sInstructions &= "· Start script" & @CRLF
        $sInstructions &= "· Start stripping process"
        GUICtrlCreateLabel($sInstructions, 360, 32, 300, 140, $BS_MULTILINE)

    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE                                                       ; if the GUI is closed,
                ExitLoop                                                                ; close it
            Case $idStart                                                               ; if the start button is pressed,
                GUICtrlSetData($idScriptStatus, " Script Running")                      ; change the running indicator,
                GUICtrlSetBkColor($idScriptStatus, 0x00FF00)
                ExcelRead(GUICtrlRead($idXLPath), GUICtrlRead($idWireType))
                If @error Then
                    Switch @error
                        Case -1
                            MsgBox(4096, "Excel File Path Error:", "File: " & GUICtrlRead($idXLPath) & " could not be found.")
                        Case -2
                            MsgBox(4096, "WireType Selection", "WireType: " & GUICtrlRead($idWireType) & " was not listed within: " & @CRLF & GUICtrlRead($idXLPath))
                    EndSwitch
                    ClearGUI()
                    ContinueLoop
                EndIf
                Cayman()                                                                ; run the main program,
                ClearGUI()                                                              ; reset the progress bar & running indicator
            Case $idMoveOne                                                         ; if the move one button is pressed,
                MotorStepOne()                                                          ; move the motor one slot
        EndSwitch
    WEnd
    GUIDelete($hGUI)
    Exit
EndFunc

Func Cayman()
;~  Set variables for user inputs here & pull data from excel sheet
;~  Put the input job numbers in an array where the array slot (0-5) is able to be called and returns a string of the job number
    Local $aJobBin[6] = [GUICtrlRead($idJob1), GUICtrlRead($idJob2), GUICtrlRead($idJob3), GUICtrlRead($idJob4), GUICtrlRead($idJob5), GUICtrlRead($idJob6)]
    _DebugArrayDisplay($aXLWireJobType)
    ; For testing purposes:

;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "$aWireJob: "&$aWireJob[$iWire] & @CRLF & "$aJobBin: "&$aJobBin[$currentBin])
;   MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "This is line 1" & @CRLF & @error & @CRLF & "This is line 3")
;   _ArrayDisplay($aXLWireType, "Excel type Array Data:")
;   _ArrayDisplay($aWireType, "Running type Array Data:")
;   _ArrayDisplay($aWireJob, "Running job Array Data:")

;   #cs ---------------------------------------------------------------------------------------------------------
;   Comment section for testing purposes so that loop does not run forever without arduino hooked up

    For $i = 1 To UBound($aXLWireJobType) - 1                                       ; While the wire we are on is less than the total number of wires,
        For $j = 0 To UBound($aJobBin) - 1                                          ; if the current bin is not equal to the bin we want
            If $aXLWireJobType[$i][0] = $aJobBin[$j] Then ExitLoop
            MotorStepOne()                                                      ; move the motor until we are on the correct bin
;~          Sleep(20000)                                                            ; wait for the motor to move

;~          For testing purposes: comment out MotorStepOne & sleep() and uncomment the message below to not run the motor, or comment out sleep and uncomment this to use messagebox to move motor instead of timer
            MsgBox($MB_SYSTEMMODAL, "AutoIt script for CAYMAN", "This wire's job: " & $aXLWireJobType[$i][0] & @CRLF & "This wire's bin: " & $aJobBin[$j] & @CRLF & "Current Bin: " & $j)
        Next
        WinWait("[CLASS:#32770]")                                                   ; Wait for a CAYMAN popup dialogue,

        Local $sPopupText = ControlGetText("[CLASS:#32770]","","[CLASSNN:Static4]")     ; Pull the text from the popup,

        If $sPopupText = "HotStamp Timeout." Then                               ; if it's a hotstamp error popup
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                                                                                    ; and here is where a light would turn on and the program would break
                                                                                    ; IF WE HAD ONE
                ExitLoop
            ElseIf $sPopupText = "Waste piece will now be ejected." Then                ; if it's a waste piece popup
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
                ExitLoop
            ElseIf $sPopupText = "Quantity complete." Then                          ; if it's a popup saying the wire is done
                GUICtrlSetData($idProgress, ($i/UBound($aXLWireJobType) - 1)*100)                   ; update the progress bar
                WinActivate("[CLASS:#32770]")                                       ; Target & close the CAYMAN popup
                Send("{F1}")
            EndIf
    Next

;   #ce ---------------------------------------------------------------------------------------------------------

EndFunc

;  Arduino control function:

Func MotorStepOne()                                                                 ; Move the motor one position using the arduino script
    WinWait("Step_ToMetal | Arduino 1.8.5")
    WinActivate("Step_ToMetal | Arduino 1.8.5", "")
    Send("^u")
    WinActivate("Cayman Automator", "")
EndFunc

;   Clear GUI function:

Func ClearGUI()
    GUICtrlSetData($idProgress, 0)                                                  ; Set progress bar to 0
    GUICtrlSetData($idScriptStatus, " Script Not Running")                          ; Change label to NOT running
    GUICtrlSetBkColor($idScriptStatus, 0xFF0000)                                    ; Change label color to red
EndFunc

;   Read excel file at $sXLPath function:

Func ExcelRead($_sXLPath, $_sWireType)
    If FileExists($_sXLPath) = 0 Then Return SetError(-1, 0, "")
    Local $oExcel = _Excel_Open()
    Local $oWorkbook = _Excel_BookOpen($oExcel, $sXLPath)
    $oWorkbook.ActiveSheet.Usedrange.Columns("E:J").EntireColumn.Delete
    _Excel_FilterSet($oWorkbook, Default, "E:E", 1, '=' & $_sWireType)
    $oRange = $oWorkbook.ActiveSheet.Usedrange.Columns("D:E").SpecialCells($xlCellTypeVisible)
    ReDim $aXLWireJobType[0][$oRange.columns.Count]
    Local $aResult
    For $oArea In $oRange.Areas
        $aResult = _Excel_RangeRead($oWorkbook, Default, $oArea, Default, True)
        _ArrayConcatenate($aXLWireJobType, $aResult)
    Next
    _Excel_Close($oExcel, False, True)
    If UBound($aXLWireJobType) - 1 <= 0 Then Return SetError(-2, 0, "")
EndFunc

 

Link to comment
Share on other sites

do you have acces to the ardiuno's programming?
if so you could put in a watchdog timer.
like the pc runs a timer, if the arduino does not reset the timer within x ammount of time a timeout error occurs.

 

I do not know if this is valid for your situation. but this is how ( to my knowledge at least) they solve such problems in industrial machines. There is a physical timer in the electrical kabinet that gets resetted by a plc (not to far from an ardiuno)  all 30 seconds for example.

In the case the PLC gets stuck in a loop or on whatever, or awaits certain input to long the watchdog timer will expire  and actions can be taken (for example setting an input on 1 that runs through the OB100,initialising script)

If the arduino would not be seen then the timer does not reset and you know you should await reconnection? This is a long shot but I hope it helps

 

as for putting the jobs in bins , can't you set a variable for each bin so the program knows it should take a new bin?

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