Trax Posted February 12, 2020 Share Posted February 12, 2020 I have been out of commission way to long. Making newbie mistakes. For some reason the Close (X in upper right) isn't exiting the program. No matter how many times I click on that X $nMsg comes back as -11 so the program never closes. What am I doing wrong? $nMsg = GUIGetMsg() ConsoleWrite($nMsg) Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted February 12, 2020 Moderators Share Posted February 12, 2020 (edited) This is a basic template you can use, should show you why you're having the issue. #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Local $hGUI = GuiCreate("Test", 300, 400) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Edited February 12, 2020 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
water Posted February 12, 2020 Share Posted February 12, 2020 Do you do this in a loop? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Trax Posted February 12, 2020 Author Share Posted February 12, 2020 It is in a loop and this is hard to explain but if you look through GUIConstantsEx.au3 $GUI_EVENT_CLOSE is defined as -3. For some reason when I click on the X in the upper right, or anywhere on the screen for that fact, a value of -11 is returned GUIGetMsg() is returning a value of -11 so it never exits........ So to define the question I guess it would really be... does anyone have any idea why GUIGetMsg() is returning a value of -11 when the X is clicked? Link to comment Share on other sites More sharing options...
water Posted February 12, 2020 Share Posted February 12, 2020 The example scripts exit as expected when clicking the X. Your script doesn't. So I guess the problem is related to your script. Without seeing the whole script it is impossible to track down the issue. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Trax Posted February 12, 2020 Author Share Posted February 12, 2020 (edited) OK. The code isn't really all that long: expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=SRT.ico #AutoIt3Wrapper_Outfile=MeterReader.exe #AutoIt3Wrapper_Res_Comment= #AutoIt3Wrapper_Res_Description= #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/so #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** AutoItSetOption("MouseCoordMode", 1) ; 0=Relative to Active Window, 1=Absolute Screen 2=Relitive to Client Area ;AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("WinTitleMatchMode", 2) ; match any substring in title #include-once Opt("TrayMenuMode", 1) #include <GUIConstantsEx.au3> #include <GUIListBox.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <CommInterface.au3> #include <GuiListBox.au3> $Form1_1 = GUICreate("Meter Readings", 531, 366, 239, 155) $Label1 = GUICtrlCreateLabel("EF Deck Meters", 104, 24, 81, 17) $Label2 = GUICtrlCreateLabel("Remelt Meters", 352, 24, 72, 17) $Label3 = GUICtrlCreateLabel("Meter 1:", 48, 56, 43, 17) $Label4 = GUICtrlCreateLabel("Meter 2:", 48, 96, 43, 17) $Label5 = GUICtrlCreateLabel("Meter 3:", 48, 136, 43, 17) $Label6 = GUICtrlCreateLabel("Meter 4:", 48, 176, 43, 17) $Label7 = GUICtrlCreateLabel("Meter 5:", 48, 216, 43, 17) $Label8 = GUICtrlCreateLabel("Meter 6:", 48, 256, 43, 17) $Label9 = GUICtrlCreateLabel("ERMF Roof Temp:", 256, 56, 93, 17) $Label10 = GUICtrlCreateLabel("ERMF Bath Temp:", 256, 96, 92, 17) $Label11 = GUICtrlCreateLabel("Meter 9:", 304, 136, 43, 17) $Label12 = GUICtrlCreateLabel("Meter 10:", 296, 176, 49, 17) $Label13 = GUICtrlCreateLabel("Meter 11:", 296, 216, 49, 17) $List1 = GUICtrlCreateList("", 96, 56, 89, 19) $List2 = GUICtrlCreateList("", 96, 96, 89, 19) $List3 = GUICtrlCreateList("", 96, 136, 89, 19) $List4 = GUICtrlCreateList("", 96, 176, 89, 19) $List5 = GUICtrlCreateList("", 96, 216, 89, 19) $List6 = GUICtrlCreateList("", 96, 256, 89, 19) $List7 = GUICtrlCreateList("", 352, 56, 89, 19) $List8 = GUICtrlCreateList("", 352, 96, 89, 19) $List9 = GUICtrlCreateList("", 352, 136, 89, 19) $List10 = GUICtrlCreateList("", 352, 176, 89, 19) $List11 = GUICtrlCreateList("", 352, 216, 89, 19) $List12 = GUICtrlCreateList("", 48, 312, 441, 19) GUISetState(@SW_SHOW) Global $Baud = 9600, $Databits = 7, $Parity = "O", $Stopbits = 1 Global $ReadTimeout = 5000 Global $CommPort, $Meter, $Message, $Value Global $SleepBetweenReads = 2000, $SleepAfterSend = 2000 While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch $CommPort = 35 $Meter = 7 ReadMeter() DisplayResults() Sleep($SleepBetweenReads) $CommPort = 35 $Meter = 8 ReadMeter() DisplayResults() Sleep($SleepBetweenReads) WEnd Func DisplayResults() Select Case $Meter = 1 _GUICtrlListBox_DeleteString($List1, 0) _GUICtrlListBox_AddString($List1, $Value) Case $Meter = 2 _GUICtrlListBox_DeleteString($List2, 0) _GUICtrlListBox_AddString($List2, $Value) Case $Meter = 3 _GUICtrlListBox_DeleteString($List3, 0) _GUICtrlListBox_AddString($List3, $Value) Case $Meter = 4 _GUICtrlListBox_DeleteString($List4, 0) _GUICtrlListBox_AddString($List4, $Value) Case $Meter = 5 _GUICtrlListBox_DeleteString($List5, 0) _GUICtrlListBox_AddString($List5, $Value) Case $Meter = 6 _GUICtrlListBox_DeleteString($List6, 0) _GUICtrlListBox_AddString($List6, $Value) Case $Meter = 7 _GUICtrlListBox_DeleteString($List7, 0) _GUICtrlListBox_AddString($List7, $Value) Case $Meter = 8 _GUICtrlListBox_DeleteString($List8, 0) _GUICtrlListBox_AddString($List8, $Value) Case $Meter = 9 _GUICtrlListBox_DeleteString($List9, 0) _GUICtrlListBox_AddString($List9, $Value) Case $Meter = 10 _GUICtrlListBox_DeleteString($List10, 0) _GUICtrlListBox_AddString($List10, $Value) Case $Meter = 11 _GUICtrlListBox_DeleteString($List11, 0) _GUICtrlListBox_AddString($List11, $Value) Case Else ; If nothing matches then execute the following. DisplayMessage("Invalid Case Number") EndSelect DisplayMessage($Message) EndFunc Func DisplayMessage($StringToDisplay) _GUICtrlListBox_DeleteString($List12, 0) _GUICtrlListBox_AddString($List12, $StringToDisplay) EndFunc Func ReadMeter() Local $PortHandle, $ReceiveResult, $SendString Local $BytesToSend, $BytesSent $Message = "" DisplayMessage("Reading Meter " & $Meter) $PortHandle = _CommAPI_OpenCOMPort($Commport, $Baud, $Parity, $Databits, $StopBits) If $PortHandle = 0 Then $Value = "Error" $Message = "@error return code opening Meter " & $Meter & " was " & @error Return EndIf $SendString = "N" & $Meter & "TA$" $BytesToSend = StringLen($SendString) $BytesSent = _CommAPI_TransmitString($PortHandle, $SendString, 0) Sleep($SleepAfterSend) If ($BytesSent <> $BytesToSend) Then $Value = "Error" $Message = "@error return code sending " & $SendString & " was " & @error _CommAPI_ClosePort($PortHandle) Return EndIf $ReceiveResult = _CommAPI_ReceiveString($PortHandle, $ReadTimeout, 0, "", 0) _CommAPI_ClosePort($PortHandle) If $ReceiveResult = "" Then $Value = "Error" $Message = "@error return code receiving meter " & $Meter & " was " & @error Return EndIf $Value = StringRight($ReceiveResult, 10) $Value = StringStripWS($Value, 😎 EndFunc Edited February 12, 2020 by JLogan3o13 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted February 12, 2020 Moderators Share Posted February 12, 2020 The problem is your sleeps; you're calling so many that the script never has time to exit. If change this section: While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch $CommPort = 35 $Meter = 7 ReadMeter() DisplayResults() Sleep($SleepBetweenReads) $CommPort = 35 $Meter = 8 ReadMeter() DisplayResults() Sleep($SleepBetweenReads) WEnd to just this: While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd You'll see that it now works. So you need to look at the multiple sleeps (each at 2 seconds long) that you have going on in that while loop "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
argumentum Posted February 13, 2020 Share Posted February 13, 2020 5 hours ago, JLogan3o13 said: The problem is your sleeps 7 hours ago, Trax said: Making newbie mistakes. use Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Nine Posted February 13, 2020 Share Posted February 13, 2020 (edited) Or you could use an alertable sleep like this : _SleepEx($SleepBetweenReads,1) Func _SleepEx($nMilliseconds, $bAlertable = 0) Local $aRet = DllCall('kernel32.dll', 'dword', 'SleepEx', 'dword', $nMilliseconds, 'bool', $bAlertable) If @error Then Return SetError(@error, @extended, 0) Return $aRet[0] EndFunc But the GUI may take up to 4 seconds to respond, since it is stuck in the sleep functions. Another approach is to use timers like this : Local $hTimer = TimerInit(), $bFirst = False While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch If TimerDiff($hTimer) >= $SleepBetweenReads Then $bFirst = Not $bFirst $CommPort = 35 $Meter = $bFirst ? 7 : 8 ConsoleWrite ($Meter & @CRLF) ; ReadMeter() ; DisplayResults() $hTimer = TimerInit() EndIf WEnd You do not need to set $CommPort repeatedly to the same value, but it is not an issue in itself. Edited February 13, 2020 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Trax Posted February 13, 2020 Author Share Posted February 13, 2020 OK. Thanks Guys. The sleeps are needed for the serial I/O. I did a HotKeySet("{ESC}", "Quit") and it exits immediately. Live and learn. Link to comment Share on other sites More sharing options...
argumentum Posted February 13, 2020 Share Posted February 13, 2020 (edited) 1 hour ago, Trax said: I did a HotKeySet("{ESC}", "Quit") and it exits immediately expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=SRT.ico #AutoIt3Wrapper_Outfile=MeterReader.exe #AutoIt3Wrapper_Res_Comment= #AutoIt3Wrapper_Res_Description= #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/so #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** AutoItSetOption("MouseCoordMode", 1) ; 0=Relative to Active Window, 1=Absolute Screen 2=Relitive to Client Area ;AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("WinTitleMatchMode", 2) ; match any substring in title #include-once Opt("TrayMenuMode", 1) #include <GUIConstantsEx.au3> #include <GUIListBox.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <CommInterface.au3> #include <GuiListBox.au3> #include <StringConstants.au3> $Form1_1 = GUICreate("Meter Readings", 531, 366, 239, 155) $Label1 = GUICtrlCreateLabel("EF Deck Meters", 104, 24, 81, 17) $Label2 = GUICtrlCreateLabel("Remelt Meters", 352, 24, 72, 17) $Label3 = GUICtrlCreateLabel("Meter 1:", 48, 56, 43, 17) $Label4 = GUICtrlCreateLabel("Meter 2:", 48, 96, 43, 17) $Label5 = GUICtrlCreateLabel("Meter 3:", 48, 136, 43, 17) $Label6 = GUICtrlCreateLabel("Meter 4:", 48, 176, 43, 17) $Label7 = GUICtrlCreateLabel("Meter 5:", 48, 216, 43, 17) $Label8 = GUICtrlCreateLabel("Meter 6:", 48, 256, 43, 17) $Label9 = GUICtrlCreateLabel("ERMF Roof Temp:", 256, 56, 93, 17) $Label10 = GUICtrlCreateLabel("ERMF Bath Temp:", 256, 96, 92, 17) $Label11 = GUICtrlCreateLabel("Meter 9:", 304, 136, 43, 17) $Label12 = GUICtrlCreateLabel("Meter 10:", 296, 176, 49, 17) $Label13 = GUICtrlCreateLabel("Meter 11:", 296, 216, 49, 17) $List1 = GUICtrlCreateList("", 96, 56, 89, 19) $List2 = GUICtrlCreateList("", 96, 96, 89, 19) $List3 = GUICtrlCreateList("", 96, 136, 89, 19) $List4 = GUICtrlCreateList("", 96, 176, 89, 19) $List5 = GUICtrlCreateList("", 96, 216, 89, 19) $List6 = GUICtrlCreateList("", 96, 256, 89, 19) $List7 = GUICtrlCreateList("", 352, 56, 89, 19) $List8 = GUICtrlCreateList("", 352, 96, 89, 19) $List9 = GUICtrlCreateList("", 352, 136, 89, 19) $List10 = GUICtrlCreateList("", 352, 176, 89, 19) $List11 = GUICtrlCreateList("", 352, 216, 89, 19) $List12 = GUICtrlCreateList("", 48, 312, 441, 19) GUISetState(@SW_SHOW) Global $Baud = 9600, $Databits = 7, $Parity = "O", $Stopbits = 1 Global $ReadTimeout = 5000 Global $CommPort, $Meter, $Message, $Value Global $SleepBetweenReads = 2000, $SleepAfterSend = 2000 While 1 SleepWhileGUIGetMsgAllInOne() ; you had your GUI thing here $CommPort = 35 $Meter = 7 ReadMeter() ; this is better DisplayResults() ; than the HotKey() SleepWhileGUIGetMsgAllInOne($SleepBetweenReads) ; <--- this is the way :) $CommPort = 35 $Meter = 8 ReadMeter() DisplayResults() SleepWhileGUIGetMsgAllInOne($SleepBetweenReads) WEnd Func SleepWhileGUIGetMsgAllInOne($SleepBetweenReads = 0) Local $hTimer = TimerInit() Do Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUIDelete() Exit EndSwitch Until TimerDiff($hTimer) > $SleepBetweenReads Return TimerDiff($hTimer) EndFunc Func DisplayResults() Select Case $Meter = 1 _GUICtrlListBox_DeleteString($List1, 0) _GUICtrlListBox_AddString($List1, $Value) Case $Meter = 2 _GUICtrlListBox_DeleteString($List2, 0) _GUICtrlListBox_AddString($List2, $Value) Case $Meter = 3 _GUICtrlListBox_DeleteString($List3, 0) _GUICtrlListBox_AddString($List3, $Value) Case $Meter = 4 _GUICtrlListBox_DeleteString($List4, 0) _GUICtrlListBox_AddString($List4, $Value) Case $Meter = 5 _GUICtrlListBox_DeleteString($List5, 0) _GUICtrlListBox_AddString($List5, $Value) Case $Meter = 6 _GUICtrlListBox_DeleteString($List6, 0) _GUICtrlListBox_AddString($List6, $Value) Case $Meter = 7 _GUICtrlListBox_DeleteString($List7, 0) _GUICtrlListBox_AddString($List7, $Value) Case $Meter = 8 _GUICtrlListBox_DeleteString($List8, 0) _GUICtrlListBox_AddString($List8, $Value) Case $Meter = 9 _GUICtrlListBox_DeleteString($List9, 0) _GUICtrlListBox_AddString($List9, $Value) Case $Meter = 10 _GUICtrlListBox_DeleteString($List10, 0) _GUICtrlListBox_AddString($List10, $Value) Case $Meter = 11 _GUICtrlListBox_DeleteString($List11, 0) _GUICtrlListBox_AddString($List11, $Value) Case Else ; If nothing matches then execute the following. DisplayMessage("Invalid Case Number") EndSelect DisplayMessage($Message) EndFunc Func DisplayMessage($StringToDisplay) _GUICtrlListBox_DeleteString($List12, 0) _GUICtrlListBox_AddString($List12, $StringToDisplay) EndFunc Func ReadMeter() Local $PortHandle, $ReceiveResult, $SendString Local $BytesToSend, $BytesSent $Message = "" DisplayMessage("Reading Meter " & $Meter) $PortHandle = _CommAPI_OpenCOMPort($Commport, $Baud, $Parity, $Databits, $StopBits) If $PortHandle = 0 Then $Value = "Error" $Message = "@error return code opening Meter " & $Meter & " was " & @error Return EndIf $SendString = "N" & $Meter & "TA$" $BytesToSend = StringLen($SendString) $BytesSent = _CommAPI_TransmitString($PortHandle, $SendString, 0) SleepWhileGUIGetMsgAllInOne($SleepAfterSend) If ($BytesSent <> $BytesToSend) Then $Value = "Error" $Message = "@error return code sending " & $SendString & " was " & @error _CommAPI_ClosePort($PortHandle) Return EndIf $ReceiveResult = _CommAPI_ReceiveString($PortHandle, $ReadTimeout, 0, "", 0) _CommAPI_ClosePort($PortHandle) If $ReceiveResult = "" Then $Value = "Error" $Message = "@error return code receiving meter " & $Meter & " was " & @error Return EndIf $Value = StringRight($ReceiveResult, 10) $Value = StringStripWS($Value, $STR_STRIPALL) ; forum messing with me !!! EndFunc Edited February 13, 2020 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now