#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=Notifiervirtualprintv2.exe #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Res_Comment=CHANGES PARSING TO REFLECT SUPER STUFF #AutoIt3Wrapper_Res_Description=Virtual printer and parser #AutoIt3Wrapper_Res_Fileversion=1.0.0.57 #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p #AutoIt3Wrapper_Res_LegalCopyright=Robert Harwood 2021 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; *** Start added by AutoIt3Wrapper *** #include #include #include #include #include ; *** End added by AutoIt3Wrapper *** #include #include #include 'CommMG.au3';or If you save the commMg.dll In the @scripdir use ; ;#include @ScriptDir & '\commg.dll' #include #include #include #include #include Opt("WINTITLEMATCHMODE", 3) OnAutoItExitRegister("_ExitScript") ;OnAutoItExitRegister("alldone") HotKeySet("{ESC}", "alldone") HotKeySet("{ESC}", "_ExitScript") Global $oExcelDoc = '' ;use for excel funtion and parsing routine Global $hnd = '' ;used for logging file Global $counter = '' ;used in excel function and parsing routine Global $sAddress = '' ;used in excel function and parsing routine Global $excellocation = '' ;used to store location of excel executable Global $filename = '' ;used for file picker Global $Input1 = '' Global $BtnSend = '' Global $sCell2value = '' Global $sWorkbook = "C:\fireutils\mapleleaf.xlsx" Global $iFirstLine = 11 ; Row number of first data record in the workbook (starting with 1) Global $iAddress = 6 ; Index of column "address" in $aData (starting with 0) Global $iCompleted = 4 ; Index of column "completed" in $aData (starting with 0) Global $sCompleted = "E" ; Letter of column "completed" in the workbook Global $sLogFile = "C:\fireutils\testlocation.txt" ; Path to the log file Global $sScriptName = "Notifervirtualprinterv1" $result = '' ;used for any returned error message setting port Const $settitle = "VIRTUAL PRINT- set Port", $maintitle = "Virtual Print" $setflow = 2 ;default to no flow control Dim $FlowType[3] = ["XOnXoff", "Hardware (RTS, CTS)", "NONE"] ; Register a function to be called when autoIt exits. Close log file and Excel workbook #Region main program #Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\ExampleComm.kxf $Form2 = GUICreate("Virtual Print", 473, 370, 339, 333, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS)) $helpmenu = GUICtrlCreateMenu("Help") $aboutitem = GUICtrlCreateMenuItem("About", $helpmenu) $Edit1 = GUICtrlCreateEdit("", 10, 25, 449, 223, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL)) $BtnSend = GUICtrlCreateButton("Send", 380, 273, 53, 30, $BS_FLAT) $Btnreset = GUICtrlCreateButton("Reset", 425, 273, 53, 30, $BS_FLAT) $Input1 = GUICtrlCreateInput("", 18, 279, 361, 21) $Checkbox1 = GUICtrlCreateCheckbox("Add LF to incoming CR", 340, 4, 150, 17) $Checkbox2 = GUICtrlCreateCheckbox("Log to file", 105, 4, 100, 17) $Checkbox3 = GUICtrlCreateCheckbox("Enable Sounds", 205, 4, 100, 17) $Checkbox4 = GUICtrlCreateCheckbox("Auto update", 18, 260, 100, 17) ;$Checkbox5 = GUICtrlCreateCheckbox("Auto Panel Silence", 30, 260, 100, 17) GUICtrlSetState($Checkbox1, $GUI_CHECKED) GUICtrlSetState($Checkbox2, $GUI_UNCHECKED) GUICtrlSetState($Checkbox3, $GUI_UNCHECKED) GUICtrlSetState($Checkbox4, $GUI_UNCHECKED) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $BtnSetPort = GUICtrlCreateButton("Set Port", 16, 312, 73, 30, $BS_FLAT) $BtnSilence = GUICtrlCreateButton("Silence Alarms", 150, 312, 73, 30, $BS_FLAT) $Label21 = GUICtrlCreateLabel("Received text", 34, 6, 70, 17) $Label31 = GUICtrlCreateLabel("commg.dll version unknown", 272, 328, 135, 17) GUICtrlSetColor(-1, 0x008080) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### WinSetTitle($Form2, "", $maintitle) ; get excel path and write to ini file If FileExists("C:\fireutils\settings.ini") Then $hinin = FileOpen("C:\fireutils\settings.ini", 0) $excellocation = FileRead($hinin) FileClose($hinin) Else SplashTextOn("Notice", "Please wait for comm dialog to appear before proceeding, this message should only appear the first time you use this program", -1, -1, -1, -1, 4, "", 24) Sleep(10000) SplashOff() $excellocation = _Find("excel.exe") $hiniout = FileOpen("c:\fireutils\settings.ini", 1) FileWrite($hiniout, $excellocation) FileFlush($hiniout) FileClose($hiniout) EndIf ; if ini file exist get location from there else run find then write ini file for future reading that way we only need to find once While SetPort(0) = -1 If MsgBox(4, 'Port not set', 'Do you want to quit the program?') = 6 Then Exit WEnd ;GUISwitch($Form2) ConsoleWrite("stage 1" & @CRLF) GUICtrlSetData($Label31, 'using ' & _CommGetVersion(1)) ConsoleWrite("stage 2" & @CRLF) Events() GUICtrlSetBkColor($Edit1, 0x000000) ;black GUICtrlSetColor($Edit1, 0xffffff) ; white GUICtrlSetState($Edit1, $GUI_FOCUS) While 1 ;get characters returning when @CR received (@CR is included in the return!) $instr = _CommGetLine() ;_CommGetLine([$endChar=@CR,$maxchar = 0, $maxtime = 0) If $instr <> '' Then ;if we got something from serial port If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr, @CR, @CRLF) ; see if we want crlf ;*****************This section does the stream parsing for colour codes based on data returned and if logging ;****************is required log the data to a file ;*************************************************************************************************************** ;******************************************* ALARM ******************************************************* ;*************************************************************************************************************** Select Case StringInStr($instr, 'Z0') Or StringInStr($instr, 'Alarm activated') Or StringInStr($instr, 'ALARM ACTIVE') Or StringInStr($instr, 'WATERFLOW ACTIVE') ;EST3 SYNTAX ; process alarm condition GUICtrlSetColor($Edit1, 0xff0000) ; Red If GUICtrlRead($Checkbox3) = $GUI_CHECKED Then ;if sound is enabled SoundPlay("c:\fireutils\sounds\Whopfast.wav") ; Alert user to alarm condtion can be silenced by button EndIf If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then ;if file logging on FileWrite($hnd, $instr) FileFlush($hnd) EndIf GUICtrlSetData($Edit1, $instr, 1) ;send data to screen If GUICtrlRead($Checkbox4) = $GUI_CHECKED Then ;If the autoupdate checkboX is checked $Cardstart = StringInStr($instr, "Z0") + 51 ; read stream for card address $CARDADD = StringMid($instr, $Cardstart, 7) $panelstart = StringInStr($instr, "P:") + 2 ;not used with mircom $PANELADD = StringMid($instr, $panelstart, 1) If $PANELADD < "10" Then $panelstart = $panelstart + 1 $PANELADD = StringMid($instr, $panelstart, 1) EndIf $devicestart = StringInStr($instr, "ss") + 4 $DEVICEADD = StringMid($instr, $devicestart - 1, 3) $sInStream = StringStripWS($CARDADD, 8) ;************************************************************************************* ;*********************************EXCEL STUFF **************************************** ;************************************************************************************* ; Start up Excel or connect to a running instance This following code delevoped by user water at Autoitv3.com forums Global $oExcel = _Excel_Open() If @error Then Exit MsgBox($MB_ICONERROR, $sScriptName, "Error opening Excel! @error=" & @error & ", @extended=0x" & Hex(@extended, 8)) ; Open the workbook Global $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook) If @error Then Exit MsgBox($MB_ICONERROR, $sScriptName, "Error opening Workbook '" & $sWorkbook & "'! @error=" & @error & ", @extended=0x" & Hex(@extended, 8)) ; Read the whole workbook into an array Global $aData = _Excel_RangeRead($oWorkbook) If @error Then Exit MsgBox($MB_ICONERROR, $sScriptName, "Error reading data! @error=" & @error & ", @extended=0x" & Hex(@extended, 8)) ; Open the Logfile - create path and file if they do no exist Global $hLog = FileOpen($sLogFile, BitOR($FO_APPEND, $FO_CREATEPATH)) _WriteLog("I", "Starting Script") ; ---------------------------------------------------------- ; Modify the following lines cof code if needed ; ---------------------------------------------------------- Moved to top of file ;Global $sWorkbook = "C:\Local\test\testlocationsheets.xls" ;Global $sInStream = "" ;Global $iFirstLine = 1 ; Row number of first data record in the workbook (starting with 1) ;Global $iAddress = 3 ; Index of column "address" in $aData (starting with 0) ;Global $iCompleted = 4 ; Index of column "completed" in $aData (starting with 0) ;Global $sCompleted = "E" ; Letter of column "completed" in the workbook ;Global $sLogFile = "C:\Local\test\testlocation.txt" ; Path to the log file ; ---------------------------------------------------------- ; Do not modify code beyond this line ; ---------------------------------------------------------- ;While _ProcessInStream($sInStream) ;WEnd ;Exit ;-------------------------------------- ; Wites a message to the logfile ; Format: YYYY/MM/DD HH:MM:SS Type Message ; Type: I - Information (everything worked as expected) ; W - Warning (everything worked fine - but I wanted to tell you about this) ; E - Error (Processing could not be started or completed) ;-------------------------------------- EndIf Case Else GUICtrlSetColor($Edit1, 0xffffff) ; set text to white for all normal display If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then ;Check for logging FileWrite($hnd, $instr) FileFlush($hnd) EndIf GUICtrlSetData($Edit1, $instr, 1) ;send data to screen EndSelect EndIf WEnd ; ********************************When we are done close things off *************************************** If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then FileClose($hnd) EndIf ; not needed anymore ;If GUICtrlRead($Checkbox4) = $GUI_CHECKED Then ; $oExcelDoc.saved = 1 ; $oExcelDoc.close ; $oExcelDoc = 0 ;E;ndIf AllDone() Func port11() ; no ideas what this does was part of comm example script ;MsgBox(0,'now set to channel',_CommSwitch(2)) _commSwitch(2) $s2 = "1 2 3 4" ;_CommGetString() ConsoleWrite("comm1 gets " & $s2 & @CRLF) _CommSendString($s2) _CommSwitch(1) EndFunc ;==>port11 #EndRegion main program Func Events() Opt("GUIOnEventMode", 1) GUISetOnEvent($GUI_EVENT_CLOSE, "justgo") GUICtrlSetOnEvent($BtnSend, "SendEvent") GUICtrlSetOnEvent($BtnSetPort, "SetPortEvent") GUICtrlSetOnEvent($BtnSilence, "Silence") ;GUICtrlSetOnEvent($Checkbox4, "AutoUpdate") GUICtrlSetOnEvent($Checkbox2, "Logfile") GUICtrlSetOnEvent($aboutitem, "About") EndFunc ;==>Events Func SetPortEvent() SetPort() ;needed because a parameter is optional for setport so we can't use "setport" for the event GUICtrlSetState($Edit1, $GUI_FOCUS) EndFunc ;==>SetPortEvent Func _ProcessInStream($sAddress) Local $bFound = False ; Search for the address For $i = ($iFirstLine - 1) To UBound($aData) - 1 If $aData[$i][$iAddress] = $sAddress Then ; address found $bFound = True If $aData[$i][$iCompleted] <> "" Then ; Completed has already been set _WriteLog("W", "Address '" & $sAddress & "' has already been processed!") Else $aData[$i][$iCompleted] = "Y" ; Set "completed" in the array _Excel_RangeWrite($oWorkbook, Default, "Y", $sCompleted & ($i + 1)) ; Write the completed flag to the workbook If @error Then Exit MsgBox($MB_ICONERROR, $sScriptName, "Error writing data to workbook! @error=" & @error & ", @extended=0x" & Hex(@extended, 8)) _Excel_BookSave($oWorkbook) If @error Then Exit MsgBox($MB_ICONERROR, $sScriptName, "Error saving workbook! @error=" & @error & ", @extended=0x" & Hex(@extended, 8)) _WriteLog("I", "Address '" & $sAddress & "' successfully processed!") EndIf ExitLoop EndIf Next If $bFound = False Then _WriteLog("E", "Address '" & $sAddress & "' could not be found in the Excel workbook!") EndFunc ;==>_ProcessInStream Func Logfile() ;*******************This function turns on and off file logging If GUICtrlRead($Checkbox2) = $GUI_UNCHECKED Then FileClose($hnd) ConsoleWrite("close log file" & @CRLF) EndIf If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then ConsoleWrite("open log file" & @CRLF) $hnd = FileOpen("Virtualprintlog.txt", 1) EndIf EndFunc ;==>Logfile Func justgo() Exit EndFunc ;==>justgo Func SendEvent() ;send the text in the input and append CR _CommSendstring(GUICtrlRead($Input1) & @CR) GUICtrlSetData($Input1, '') ;clear the input ;GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen EndFunc ;==>SendEvent Func Silence() ;stop the sound from alarms etc SoundPlay("") EndFunc ;==>Silence Func About() ;Display version info $ver = FileGetVersion("c:\fireutils\virtualprint.exe") MsgBox(0, "About", "File version:" & $ver & " Written by Robert Harwood using AutoIt Gui Creator (C) 2010") EndFunc ;==>About Func AllDone() ;MsgBox(0,'will close ports','') _Commcloseport() ;MsgBox(0,'port closed','') Exit EndFunc ;==>AllDone Func _Find($s, $d = @HomeDrive) ;only way I could find to locate the excel. exe file If StringRight($d, 1) <> "\" Then $d &= "\" Local $h = FileFindFirstFile($d & "*") If $h = -1 Then Return 0 While 1 $t = FileFindNextFile($h) If $t = $s Then Return $d & $t $t = $d & $t If @error Then Return 0 * FileClose($h) If StringInStr(FileGetAttrib($t), "D") Then $tmp = _Find($s, $t) If $tmp <> "0" Then Return $tmp ContinueLoop EndIf WEnd FileClose($h) Return 0 EndFunc ;==>_Find Func _FindAll($s, $d = @HomeDrive) If Not IsDeclared("sRet") Then Static $sRet = "" EndIf If StringRight($d, 1) <> "\" Then $d &= "\" Local $h = FileFindFirstFile($d & "*") If $h = -1 Then Return "" While 1 $t = FileFindNextFile($h) If @error Then ExitLoop If $t = $s Then $sRet &= @CRLF & $d & $t $t = $d & $t If StringInStr(FileGetAttrib($t), "D") Then $tmp = _FindAll($s, $t) WEnd FileClose($h) Return $sRet EndFunc ;==>_FindAll ; Function SetPort($mode=1) ; Creates a form for the port settings ;Parameter $mode sets the return value depending on whether the port was set ;Returns 0 if $mode <> 1 ; -1 If` the port not set and $mode is 1 Func SetPort($mode = 1) ;if $mode = 1 then returns -1 if settings not made Opt("GUIOnEventMode", 0) ;keep events for $Form2, use GuiGetMsg for $Form3 #Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\examplecommsetport.kxf $Form3 = GUICreate("virtual Print - set Port", 422, 279, 329, 268, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS, $DS_MODALFRAME), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE)) $Group1 = GUICtrlCreateGroup("Set COM Port", 18, 8, 288, 252) $CmboPortsAvailable = GUICtrlCreateCombo("", 127, 28, 145, 25) $CmBoBaud = GUICtrlCreateCombo("9600", 127, 66, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SORT, $WS_VSCROLL)) GUICtrlSetData(-1, "10400|110|115200|1200|128000|14400|150|15625|1800|19200|2000|2400|256000|28800|3600|38400|4800|50|56000|57600|600|7200|75") $CmBoStop = GUICtrlCreateCombo("1", 127, 141, 145, 25) GUICtrlSetData(-1, "1|2|1.5") $CmBoParity = GUICtrlCreateCombo("none", 127, 178, 145, 25) GUICtrlSetData(-1, "odd|even|none") $Label2 = GUICtrlCreateLabel("Port", 94, 32, 23, 17) $Label3 = GUICtrlCreateLabel("baud", 89, 70, 28, 17) $Label4 = GUICtrlCreateLabel("No. Stop bits", 52, 145, 65, 17) $Label5 = GUICtrlCreateLabel("parity", 88, 182, 29, 17) $CmboDataBits = GUICtrlCreateCombo("8", 127, 103, 145, 25) GUICtrlSetData(-1, "7|8") $Label7 = GUICtrlCreateLabel("No. of Data Bits", 38, 107, 79, 17) $ComboFlow = GUICtrlCreateCombo("NONE", 127, 216, 145, 25) GUICtrlSetData(-1, "NONE|XOnXOff|Hardware (RTS, CTS)") $Label1 = GUICtrlCreateLabel("flow control", 59, 220, 58, 17) GUICtrlCreateGroup("", -99, -99, 1, 1) $BtnApply = GUICtrlCreateButton("Apply", 315, 95, 75, 35, $BS_FLAT) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $BtnCancel = GUICtrlCreateButton("Cancel", 316, 147, 76, 35, $BS_FLAT) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### WinSetTitle($Form3, "", $settitle) ;ensure a change to Koda design doesn't stop script working $mainxy = WinGetPos($Form2) WinMove($Form3, "", $mainxy[0] + 20, $mainxy[1] + 20) $portlist = _CommListPorts(0) ;find the available COM ports and write them into the ports combo If @error = 1 Then MsgBox(0, 'trouble getting portlist', 'Program will terminate!') Exit EndIf For $pl = 1 To $portlist[0] GUICtrlSetData($CmboPortsAvailable, $portlist[$pl]) ;_CommListPorts()) Next GUICtrlSetData($CmboPortsAvailable, $portlist[1]) ;show the first port found GUICtrlSetData($ComboFlow, $FlowType[$setflow]) _GUICtrlComboBox_SetMinVisible($CmBoBaud, 10) ;restrict the length of the drop-down list $retval = 0 While 1 $msg = GUIGetMsg() If $msg = $BtnCancel Then If Not $mode Then $retval = -1 ExitLoop EndIf If $msg = $BtnApply Then Local $sportSetError $comboflowsel = GUICtrlRead($ComboFlow) For $n = 0 To 2 If $comboflowsel = $FlowType[$n] Then $setflow = $n ConsoleWrite("flow = " & $setflow & @CRLF) ExitLoop EndIf Next $setport = StringReplace(GUICtrlRead($CmboPortsAvailable), 'COM', '') _CommSetPort($setport, $sportSetError, GUICtrlRead($CmBoBaud), GUICtrlRead($CmboDataBits), GUICtrlRead($CmBoParity), GUICtrlRead($CmBoStop), $setflow) If $sportSetError = '' Then MsgBox(262144, 'Connected ', 'to COM' & $setport) Else MsgBox(262144, 'Setport error = ', $sportSetError) EndIf $mode = 1 ; ExitLoop EndIf ;stop user switching back to $form2 If WinActive($maintitle) Then ConsoleWrite('main is active' & @CRLF) If WinActivate($settitle) = 0 Then MsgBox(0, 'not found', $settitle) EndIf WEnd GUIDelete($Form3) WinActivate($maintitle) Events() Return $retval EndFunc ;==>SetPort Func _WriteLog($sType, $sMessage) FileWrite($hLog, @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $sType & " " & $sMessage & @CRLF) EndFunc ;==>_WriteLog ;-------------------------------------- ; Does some cleanup before the scriptt ends ;-------------------------------------- Func _ExitScript() ; Save workbook and close Excel _Excel_BookClose($oWorkbook, True) _Excel_Close($oExcel, True) ; Close the log file _WriteLog("I", "Exiting Script") FileClose($hLog) EndFunc ;==>_ExitScript