Function executing twice in AutoIt

不想你离开。 提交于 2019-12-24 06:47:09

问题


I'm developing an autoit function which is required to read from a .txt file, split the string that has been read from the .txt file, run through the array holding the string that has just been split and pass these on to function send() as an argument to be sent to the currently open window.

The name of my function is function keysend() so essentially:

  • Function keysend recieves a char value i.e. $ch.
  • Function keysend() opens a file with contains a string of words with comma used as delimeters.
  • Function keysend() through function StringSplit() splits the string and generates a string array.
  • Using case statements function keysend() compares the taken inchar value i.e. $ch and compares this with 17 different cases and based on a matching case it takes a specific element of the array passes it on as an argument to the function Send()

The code runs through the above steps twice whilst it's only meant to run once as function keysend() is called only once.

Is there a reason as to why it would be executing twice - the only loop in the code is to run through the array generated by StringSplit and display each element in a message box.

Below is my code

   Func keysend($ch)    ; translate the character into a keystroke and send it out

   MsgBox (0, "$ch is: ","$ch is: " & $ch)

   $file = FileOpen($filepath, 0)

   if FileExists($filepath) Then

      ; Check if file opened for reading OK
      If $file = -1 Then
          MsgBox(0, "Error", "Unable to open file.")
          Exit
      EndIf

      ; Read in lines of text until the EOF is reached
      $line = FileReadLine($file)

      MsgBox(0, "Line from file is:", $line)

      ; Split the string using the delimeter into an array
      Local $userconfigasstring = $line

      $userconfigasarray = StringSplit($userconfigasstring, ',')
      Local $userconfigasarraysize = UBound($userconfigasarray)

      MsgBox (0, "Array size: ", "Array size is: " & $userconfigasarraysize)

      If IsArray($userconfigasarray) Then 
          For $i = 0 to $userconfigasarraysize - 1  
            MsgBox (0, "$userconfigasarray", "$userconfigasarray["&$i&"] is: "&$userconfigasarray[$i])
          Next

          Switch $ch
           Case "A"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[1])
               ;$myvar = $userconfigasarray[1]
               Send("{"& $userconfigasarray[1] &"}")
               MsgBox (0, "SEND STATUS", "SEND HAS BEEN CALLED")
               ;Exit
           Case "B"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[2])
               $myvar = $userconfigasarray[2]
               ;send("{LEFT}")
               send("{"& $myvar &"}")
            Case "C"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[3])
               $myvar = $userconfigasarray[3]
               send("{"& $myvar &"}")        
           Case "D"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[4])
               $myvar = $userconfigasarray[4]
               send("{"& $myvar &"}")
           Case "E"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[5])
               $myvar = $userconfigasarray[5]
               send("{"& $myvar &"}")
           Case "F"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[6])
               $myvar = $userconfigasarray[6]
               send("{"& $myvar &"}")
               ;loggit("SEND STATUS - SEND HAS BEEN CALLED")
           Case "G"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[7])
               $myvar = $userconfigasarray[7]
               send("{"& $myvar &"}")        
           Case "H"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[8])
               $myvar = $userconfigasarray[8]
               send("{"& $myvar &"}")        
           Case "1"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[9])
               $myvar = $userconfigasarray[9]
               send("{"& $myvar &"}")        
           Case "2"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[10])
               $myvar = $userconfigasarray[10]
               send("{"& $myvar &"}")        
           Case "3"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[11])
               $myvar = $userconfigasarray[11]
               send("{"& $myvar &"}")        
           Case "4"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[12])
               $myvar = $userconfigasarray[12]
               send("{"& $myvar &"}")        
           Case "5"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[13])
               $myvar = $userconfigasarray[13]
               Send("{"& $myvar &"}")        
           Case "6"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[14])
               $myvar = $userconfigasarray[14]
               send("{"& $myvar &"}")        
           Case "7"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[15])
               $myvar = $userconfigasarray[15]
               send("{"& $myvar &"}")        
           Case "8"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[16])
               $myvar = $userconfigasarray[16]
               send("{"& $myvar &"}")        
           Case "9"
               MsgBox (0, "SWITCH STATEMENT", $userconfigasarray[17])
               $myvar = $userconfigasarray[17]
               send("{"& $myvar &"}")        
           Case Else
               MsgBox (0, "SWITCH STATEMENT", "Can't handle '" & $ch & "' <0x" & Hex(Asc($ch), 2) & ">")
               ;loggit("Can't handle '" & $ch & "' <0x" & Hex(Asc($ch), 2) & ">")

         EndSwitch

      EndIf

      FileClose($file)

      MsgBox (0, "keysend status", "END OF keysend and FileClose($file) has been closed")

      ;Exit

   Else
       MsgBox(0, "Error", "FILE DOES NOT EXIST")
       Exit  
   EndIf

EndFunc

The idea is that the buffer - gets filled with bits of the incoming stream until there's a CR in it, whereupon the part up to and including the CR is removed and processed

Below is where the function keysend() is being called:

    While 1

    ;ManageTray()

    ;Sleep(50)  ; (to allow script to make way for other software. Not sure if it does though?)
    $inbuff = $inbuff & _Commgetstring()

    loggit("$inbuff & _Commgetstring() is: " & $inbuff)

    If @error <> 0 then loggit("Error returned by _Commgetstring()")

    ;loggit("@error is: " & @error)

    $n = StringInStr($inbuff, @CR)

    loggit("$n is: " & $n)

    if $n > 0 Then  ; found CR

        ;loggit("$n is: " & $n)

        ;$inbuff - The string to evaluate.
        ;@LF - The substring to search for or the character position to start the replacement.
        ;"" - The replacement string.

        $inbuff = StringReplace($inbuff, @LF, "")

        loggit("$inbuff using StringReplace is: " & $inbuff)

        loggit("Received i.e. StringLeft($inbuff, $n-1) " & StringLeft($inbuff, $n-1) )

        loggit("$packetID is: " & $packetID)

        ;If StringInStr($inbuff, $packetID) And StringInStr($inbuff, $remoteID) Then
        If StringInStr($inbuff, $packetID) Then

            loggit( "$inbuff in StringInStr($inbuff, $packetID) " & $inbuff)

            loggit( "StringMid($inbuff, $n-1, 1) in StringInStr($inbuff, $packetID) is: " & StringMid($inbuff, $n-1, 1))

            keysend(StringMid($inbuff, $n-1, 1) ); process the char before the CR
        EndIf

        $inbuff = StringTrimLeft($inbuff, $n)   ; inbuff holds everything after the CR

        loggit( "$inbuff after StringTrimLeft($inbuff, $n) is: " & StringTrimLeft($inbuff, $n))

    ;Else 
      ;loggit( "$n is not greater than i.e. $n is: " & $n )
    EndIf
WEnd

Below are the outcomes of the loggit:

26-06-2013 09:09:48.723--> $inbuff & _Commgetstring() is: 
UCAST:000D6F0001B557E3,07=RFC-951

26-06-2013 09:09:48.724--> $n is: 1
26-06-2013 09:09:48.725--> $inbuff using StringReplace is: 
UCAST:000D6F0001B557E3,07=RFC-951

26-06-2013 09:09:48.726--> Received i.e. StringLeft($inbuff, $n-1) 
26-06-2013 09:09:48.727--> $packetID is: UCAST:
26-06-2013 09:09:48.729--> $inbuff in StringInStr($inbuff, $packetID) 
UCAST:000D6F0001B557E3,07=RFC-951

26-06-2013 09:09:48.730--> StringMid($inbuff, $n-1, 1) in StringInStr($inbuff, $packetID) is: 
26-06-2013 09:09:53.147--> $inbuff after StringTrimLeft($inbuff, $n) is: CAST:000D6F0001B557E3,07=RFC-951

I've posted the _Commgetstring() code below:

    ;================================================================================
;
; Function Name:  _CommGetstring()
; Description:    Get whatever characters are available received by the port for the selected channel
; Parameters:     none
; Returns:  on success the string and @error is 0
;           if input buffer empty then empty string returned
;           on failure an empty string and @error set to the error set by DllCall
; Notes: Use _CommGetLine to get a whole line treminated by @CR or a defined character.
;=================================================================================

Func _Commgetstring()
    ;get a string NB could be part of a line depending on what is in buffer
    Local $vDllAns

    If Not $fPortOpen Then
        SetError(1)
        Return 0
    EndIf

    ;$sStr1 = ''
    ;$vDllAns = DllCall($hDll,'str','GetByte')
    $vDllAns = DllCall($hDll, 'str', 'GetString')

    If @error <> 0 Then
        SetError(1)
        mgdebugCW('error in _commgetstring' & @CRLF)
        Return ''
    EndIf
    Return $vDllAns[0]
EndFunc   ;==>_Commgetstring

Below is my function loggit and the function stamp which it calls:

    $logfile = "file.log" ; this is a .txt file

    func stamp()    ; provide a timestamp for the log file
    return @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & "." & @MSEC & "--> "
EndFunc

    Func loggit($logarg)
    FileWriteLine($logfile,stamp() & $logarg & @CRLF)
EndFunc

File indicating the loggit content is here

File containing the keysend is here

File containing theCommgetstring() is here

I appreciate any assistance.

来源:https://stackoverflow.com/questions/17295428/function-executing-twice-in-autoit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!