1

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

我的函数的名称是 function keysend() 所以本质上是:

  • 功能键发送接收一个 char 值,即 $ch。
  • 函数 keysend() 打开一个文件,其中包含一串以逗号作为分隔符的单词。
  • 函数keysend()通过函数StringSplit()对字符串进行拆分,生成字符串数组。
  • 使用 case 语句 function 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内容的文件在这里

包含密钥发送的文件在这里

包含 theCommgetstring() 的文件在这里

我感谢任何帮助。

4

0 回答 0