Skip to content

Instantly share code, notes, and snippets.

@AMonari1
Last active December 19, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMonari1/53cb82d6cc338243f6be to your computer and use it in GitHub Desktop.
Save AMonari1/53cb82d6cc338243f6be to your computer and use it in GitHub Desktop.
Remote reader
$version="ver 0.3 02jul2013"
$copyright = chr(169)&" 2013"
$logfile = "file.log"
$configfile = "file.ini"
$iconfile = "icon.ico"
$filepath = "userautoitbuttonconfig.txt"
;$qiconfile = "query.ico"
$allowstring = "ATS0A=0000:password" & @CR
$preventstring = "ATS0A=0001:password" & @CR
$SetupStr1 = "ATS4E=0EFF" & @CR
$SetupStr2 = $preventstring
$SetupStr3 = "ATZ" & @CR
$remoteID = "RFC-95" ; the device ID of the remote control
$packetID = "UCAST:" ; ignore packets that don't contain this
$AllowWindow = 30000 ; window for network joins
$tm_allow = TimerInit() ; (prevents "undeclared" error on syntax check)
$parity = 0 ; 0 - none, 1 - odd, 2 - even
$stopbits = 1
$databits = 8
$baudrate = 19200
Global $holdUserConfig
Global $var[3]
Global $test = "test"
Global $line
Global $ch
loggit("Started")
;--------------------------------------
; Manage the systray icon
;--------------------------------------
Opt("TrayMenuMode",3) ; 1 -> Default tray menu items (Script Paused/Exit) will not be shown.
; 2 -> No auto tick/untick
$exititem = TrayCreateItem("Stop")
$allowitem = TrayCreateItem("Allow joining")
TraySetState() ; force configuration of systray item according to the opts above
TraySetToolTip("BGE remote capture - " & $version)
if FileExists($iconfile) Then ; use external icon if present
TraySetIcon($iconfile)
Else
TraySetIcon(@ScriptName) ; else use our own .exe for the icon
EndIf
;--------------------------------------
; Miscellaneous bits
;--------------------------------------
; load the dll file into the compiled executable and copy it to the local folder at run-time;
; (We can't delete the dll when done as it is locked by the executable).
FileInstall("commg.dll", "commg.dll", 1)
$portnumber = 999 ; 'bad' seed value tested for at the end of readconfigfile()
;--------------------------------------
; Check that we can see the necessary files
;--------------------------------------
if FileExists($logfile) Then FileDelete($logfile)
if not FileExists("commg.dll") Then
loggit("commg.dll missing")
MsgBox(48,"Error","Cannot find commg.dll")
leave()
EndIf
#include 'commMG.au3' ; the serial comms UDF
if FileExists($configfile) Then
loggit("Loading data from config file...")
readconfigfile() ; get the data from the config file ()
If $portnumber = 999 Then
loggit("Failed to get com port from config file")
pickport()
Else
loggit("...got COM" & $portnumber)
EndIf
Else
loggit("Config file <" & $configfile & "> missing")
pickport()
EndIf
;--------------------------------------
; Set up the serial port
;--------------------------------------
dim $portSetError
_CommSetPort($portnumber, $portSetError, $baudrate, $databits, $parity, $stopbits, 2) ; last 2 is no flow control
if $portSetError = '' Then
loggit('Connected to COM' & $portnumber)
_CommClearInputBuffer()
Else
MsgBox(48,"Error","Cannot open COM" & $portnumber & " - " & $portSetError)
loggit('Failed to open COM' & $portnumber & " - error was <" & $portSetError & ">"&@CRLF)
leave()
EndIf
_CommSendString($SetupStr1)
loggit("Sending setup string 1")
sleep(200)
_CommSendString($SetupStr2)
loggit("Sending setup string 2")
sleep(200)
_CommSendString($SetupStr3)
loggit("Sending setup string 3")
sleep(200)
$inbuff = "" ; 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
;--------------------------------------
; Main Loop
;--------------------------------------
While 1
ManageTray()
;$commgetstring = _Commgetstring()
loggit("**************************************************************")
loggit("$inbuff is: " &$inbuff)
;loggit("_Commgetstring() is: " & $commgetstring)
;Sleep(50) ; (to allow script to make way for other software. Not sure if it does though?)
$inbuff = $inbuff & _Commgetstring()
;MsgBox(0, "$inbuff", "$inbuff is: " & $inbuff)
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)
;$inbuff -The string to evaluate.
;count i.e. $n-1 - The number of characters to get.
; Returns a string containing the leftmost count characters of the string.
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( "StringInStr($inbuff, $packetID) is: " & $inbuff)
;string The string to evaluate.
;start The character position to start. (1 = first character)
;count [optional] The number of characters to extract. By default the entire remainder of the string.
loggit( "StringMid($inbuff, $n-1, 1) of " & $inbuff & " 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))
loggit("**************************************************************")
;Else
;loggit( "$n is not greater than i.e. $n is: " & $n )
;EndIf
WEnd
;--------------------------------------
; Functions
;--------------------------------------
Func ManageTray() ; called every time we go round the main loop
;$msg = TrayGetMsg()
loggit("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
loggit("START OF ManageTray()")
$eventctrlidFilepath = "C:\Boston\RFC-95\exititemeventcontrolid.txt"
$eventctrlidfile = FileOpen($eventctrlidfilepath, 0)
If FileExists($eventctrlidfilepath) Then
; Check if file opened for reading OK
If $eventctrlidfile = -1 Then
loggit("$file = -1 hence Unable to open file.")
;MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Read in lines of text until the EOF is reached
$exititemfiledata = FileReadLine($eventctrlidfile)
loggit("Read lines of text until the EOF are: " & $exititemfiledata)
$exititemfiledatastringlength = StringLen($exititemfiledata)
; Need to check if the file is empty
if $exititemfiledatastringlength > 0 Then
loggit("$exititemfiledatastringlength > 0")
#cs
If $exititemfiledata == 7 Then
leave()
ElseIf $exititemfiledata == 8 Then
If BitAND(TrayItemGetState($allowitem), 1) Then ; it's checked, turn it off
FnPrevent()
Else
FnAllow()
EndIf
; Case $preventitem
; FnPrevent()
EndIf
#ce
;Create array to take in values
Local $arreventctrlid[2]
$arreventctrlid[0] = $exititemfiledata
$arreventctrlid[1] = $exititemfiledata
Local $arreventctrlidsize = UBound($arreventctrlid)
If IsArray($arreventctrlid) Then
For $i = 0 to $arreventctrlidsize - 1
;MsgBox (0, "$userconfigasarray", "$userconfigasarray["&$i&"] is: "&$userconfigasarray[$i])
loggit("$arreventctrlid["& $i &"] is: " & $arreventctrlid[$i])
loggit("$exititem is: " & $exititem)
ConsoleWrite($arreventctrlid[$i] == $exititem)
If $arreventctrlid[$i] == $exititem Then
loggit("$arreventctrlid[$i] == $exititem")
leave()
ElseIf $arreventctrlid[$i] == $allowitem Then
loggit("$arreventctrlid[$i] == $allowitem")
If BitAND(TrayItemGetState($allowitem), 1) Then ; it's checked, turn it off
FnPrevent()
Else
FnAllow()
EndIf
;Case $preventitem
;FnPrevent()
EndIf
#cs
Switch $arreventctrlid[$i]
Case $exititem
leave()
Case $allowitem
If BitAND(TrayItemGetState($allowitem), 1) Then ; it's checked, turn it off
FnPrevent()
Else
FnAllow()
EndIf
Case 7
If BitAND(TrayItemGetState($allowitem), 1) Then ; it's checked, turn it off
FnPrevent()
Else
FnAllow()
EndIf
; Case $preventitem
; FnPrevent()
EndSwitch
#ce
Next
EndIf
;
; if allow is still ticked and timer has expired, turn it off
if BitAND( TrayItemGetState($allowitem), 1) and TimerDiff($tm_allow) >= $AllowWindow then
FnPrevent()
EndIf
EndIf
Else
loggit("FILE DOES NOT EXIST")
Exit
EndIf
loggit("END OF ManageTray()")
loggit("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
EndFunc
Func FnAllow() ; send string to co-ordinator to allow devices to join
_CommSendString($allowstring)
loggit("Sent 'allow joining' string")
TrayItemSetState($allowitem, 1) ; 'allow' checked
TrayTip("PAN joining", "Enabled", $AllowWindow/1000, 1)
$tm_allow = TimerInit()
EndFunc
Func FnPrevent() ; send string to co-ordinator to stop devices joining
_CommSendString($preventstring)
loggit("Sent 'prevent joining' string")
TrayItemSetState($allowitem, 4) ; 'allow' unchecked
TrayTip("", "", 0)
EndFunc
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])
loggit( "$userconfigasarray[1] is: " & $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
Func leave() ; tidy exit
ConsoleWrite("leave()")
_CommClosePort()
loggit("Leaving now...")
exit
EndFunc
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
Func pickport() ; enumerate the ports, allow the user to pick one and save it in config file
; FileInstall("q.ico", $qiconfile, 1) ; plant the revised systray icon file
; TraySetIcon($qiconfile)
TraySetState(4) ;flash
$comports = _CommListPorts(0) ; $comports[0] now holds quantity of ports,
; and $comports[1] contains (e.g.) "COM2", etc.
; (bug in dll - no ports reports as one port, with $comport[1] empty and @error set to 0)
If $comports[1] = "" Then ; ZERO ports
loggit("No ports found")
MsgBox(0, "Error", "No com ports found on this computer")
leave()
Else
If $comports[0] = 1 Then ; ONE port
$portnumber = StringTrimLeft($comports[1], 3)
loggit("One port found - port " & $portnumber & " - using it")
FileWriteLine($configfile, "[COM" & $portnumber & "]")
Else ; MORE THAN ONE port
$temp = "found " & $comports[0] & " ports:"
For $n = $comports[0] to 1 step -1
$temp = $temp & " " & $comports[$n] & ","
Next
loggit(StringTrimRight($temp,1)) ; (strip the last ',')
Dim $portlist[$comports[0]] ; these will be our menu items
$separator = TrayCreateItem("") ; make divider
$selector = TrayCreateMenu("Pick com port") ; make a menu holder
For $n = $comports[0] to 1 step -1
$portlist[$n-1] = TrayCreateItem($comports[$n], $selector)
Next
TrayTip("Unconfigured", "Please choose a com port from the systray icon", 30)
while $portnumber = 999 ; wait for the user to quit or pick a port
$msg = TrayGetMsg()
If $msg = $exititem Then leave()
For $n = $comports[0] to 1 step -1
If $msg = $portlist[$n-1] then
$portnumber = StringTrimLeft($comports[$n], 3)
ExitLoop
EndIf
Next
WEnd
TrayItemDelete($selector) ; kill the menu items
TrayItemDelete($separator)
TrayTip("", "", 0)
If FileExists($configfile) then FileDelete($configfile)
FileWriteLine($configfile, "[" & $comports[$n] & "]")
FileWriteLine($configfile, @CRLF & " (delete this file to force re-configuration)")
loggit($comports[$n] & " selected")
EndIf
EndIf
; replace the proper icon
if FileExists($iconfile) Then ; use external icon if present
TraySetIcon($iconfile)
Else
TraySetIcon(@ScriptName) ; else use our own .exe for the icon
EndIf
TraySetState(8) ; un-flash
; FileDelete($qiconfile)
EndFunc
Func readconfigfile() ; get the data out of the config file
$cfile = FileOpen($configfile, 0)
If $cfile = -1 Then
MsgBox(0, "Error", "Cannot open config file.")
loggit("Can't open " & $configfile)
leave()
EndIf
while 1
$line = FileReadLine($cfile)
if @error = -1 Then ExitLoop
$line = StringStripWS($line, 8) ; strips ALL spaces
if StringLen($line) > 0 Then
if StringLeft($line,1) = "[" Then
$x = StringInStr($line, "]")
if $x > 2 Then
$line = StringMid($line, 2, $x-2)
If StringLeft( $line, 3) = "COM" Then
$line = StringTrimLeft( $line, 3 )
$portnumber = $line
EndIf
EndIf
EndIf
EndIf
WEnd
FileClose($cfile)
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment