Jump to content
Sign in to follow this  
tim1986

My script is closing immediately

Recommended Posts

Hello. My script closes immediately on execution. What do I do??

#include <GUIConstants.au3>
#include <Mixer_Include.au3>
#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
;$showhideitem   = TrayCreateItem("Show/Hide Settings")
$testitem    = TrayCreateItem("Test Serial Receive")
$restartitem    = TrayCreateItem("Restart Program")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")
TraySetState()

;default mixer is always 0 (so far) which is last audio device installed or prefered deviced selected by user

$MsComm = ObjCreate("MSCOMMLib.MsComm.1")
$MsComm.CommPort = 4
$MsComm.Settings = "9600,N,8,1"
$MsComm.Handshaking = 0
$MsComm.InBufferSize = 1024
$MsComm.InputLen = 1
$MsComm.PortOpen = 1

$UpdateInterval = 500

Func Main()
While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        ;Case $msg = $showhideitem
            ;WindowHideShow($Form1,$Form1State)
        Case $msg = $testitem
            _MsgTest()
        Case $msg = $restartitem
            _restart()
        Case $msg = $exititem
            ExitLoop
        EndSelect
    Sleep($UpdateInterval)
VolumeSet(SerialGetVolume())
WEnd
EndFunc

;;;;;;;;;;;;;;;
;Serial Section
;;;;;;;;;;;;;;;
;_MsgOut("RT" & @CR)
;MsgBox(0,"Testing",_MsgIn())

Func _MsgTest()
    MsgBox(0,"Testing",SerialGetVolume())
EndFunc

Func _MsgOut($str)
    $MsComm.OutBufferCount = 0
    $MsComm.InBufferCount = 0
    
    If $MsComm.PortOpen = True Then
        $MsComm.Output = $str
    EndIf
    $MsComm.InputLen = 0
EndFunc

Func _MsgIn()
    $TIMEOUT = 1000
    $nTimeCtr = 0
    $sBuffer = ""
    Do
        $nTimeCtr += 1
        If $MsComm.InBufferCount > 0 Then
            $sBuffer = $sBuffer & $MsComm.Input
        EndIf
        Sleep(500)
    Until StringInStr($sBuffer,@CR) OR $nTimeCtr > $TIMEOUT
    
    If $nTimeCtr < $TIMEOUT Then
        $nI = StringInStr($sBuffer,@CR)
        Return StringLeft($sBuffer,$nI)
    Else
        Return "Error"
    EndIf
EndFunc

Func SerialGetVolume()
    _MsgOut("P" & @CR)
    Sleep(200);
    $Volume = _MsgIn()
    ConsoleWrite("'" & $Volume & "'")
    Return $Volume
EndFunc

Func VolumeSet($volume)
    _SetMixerVolume($MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, $volume)
EndFunc

Func WindowHideShow($winhandle,ByRef $state)
    if $state = 0 Then
        GUISetState(@SW_SHOW, $winhandle)
        $state = 1
    Else
        GUISetState(@SW_HIDE, $winhandle)
        $state = 0
    EndIf
EndFunc

; Restart the program
Func _restart()
    If @Compiled = 1 Then
        Run( FileGetShortName(@ScriptFullPath))
    Else
        Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...