函数在AutoIt中执行两次

时间:2013-06-25 10:53:52

标签: autoit

我正在开发一个自动函数,需要从.txt文件中读取,拆分从.txt文件中读取的字符串,运行包含刚刚拆分的字符串的数组并传递这些将send()作为一个参数发送到当前打开的窗口。

我的函数的名称是函数keysend(),所以基本上是:

  • 函数keysend收到char值,即$ ch。
  • 功能keysend()打开一个包含一串单词的文件 逗号用作定界符。
  • 函数keysend()通过函数StringSplit()拆分字符串 并生成一个字符串数组。
  • 使用case语句函数keysend()比较获取的inchar值,即$ ch 并将其与17种不同的案例进行比较并基于匹配 它需要一个特定的数组元素传递它作为一个 函数Send()
  • 的参数

代码运行上述步骤两次,而它只是运行一次因为函数keysend()只被调用一次。

有没有理由为什么它会执行两次 - 代码中唯一的循环是运行StringSplit生成的数组并在消息框中显示每个元素。

以下是我的代码

   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

想法是缓冲区 - 填充输入流的位,直到其中有CR,然后删除并处理包括CR的部分

下面是调用函数keysend()的地方:

    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

以下是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

我在下面发布了_Commgetstring()代码:

    ;================================================================================
;
; 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

下面是我的函数loggit和它调用的函数标记:

    $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

指示loggit内容的文件是here

包含keysend的文件是here

包含Commgetstring()的文件是here

我感谢任何帮助。

0 个答案:

没有答案