Attribute VB_Name = "Shutter"
Option Explicit
Private UseShutter As Boolean
Private ShutterComm As MSComm    ' Create object variable.

Public Sub SetShutterComm(Comm As MSComm)
    Set ShutterComm = Comm ' Get reference to MSComm control
End Sub

Public Function ConfigureShutter(Port As Integer, ErrStr As String)
    
    If Port = 0 Then
        UseShutter = False
        ErrStr = ""
        ConfigureShutter = True
        Exit Function
    Else
        UseShutter = True
    End If
    
    
    ' Set comm port
    ShutterComm.CommPort = Port
    ' 9600 baud, no parity, 8 data, and 2 stop bits.
    ShutterComm.Settings = "9600,N,8,1"
    ' Tell the control to read all that is available
    ShutterComm.InputLen = 0
    ' Open the port.
    ShutterComm.PortOpen = True
   
   
    ErrStr = ""
    ConfigureShutter = True
End Function

Public Function OpenShutter(ErrStr As String)
    
    If UseShutter = False Then
        ErrStr = ""
        OpenShutter = True
        Exit Function
    End If
    
    ErrStr = SendCommand("OPEN")
     
    If Left(ErrStr, 3) = "RDY" Then
        ErrStr = ""
        OpenShutter = True
        Exit Function
    End If
    
    OpenShutter = False
End Function

Public Function CloseShutter(ErrStr As String)
    
    If UseShutter = False Then
        ErrStr = ""
        CloseShutter = True
        Exit Function
    End If
    
    ErrStr = SendCommand("CLOSE")
     
    If Left(ErrStr, 3) = "RDY" Then
        ErrStr = ""
        CloseShutter = True
        Exit Function
    End If
    
    CloseShutter = False
End Function

Public Function GetStatShutter(ErrStr As String)
    
    If UseShutter = False Then
        ErrStr = ""
        GetStatShutter = True
        Exit Function
    End If
    
    ErrStr = SendCommand("GET_STAT")
     
    If Left(ErrStr, 4) = "STAT" Then
        GetStatShutter = True
        Exit Function
    End If
    
    GetStatShutter = False
End Function

Public Sub WriteShutter(Cmd As String)
    
    If UseShutter = True Then
        'Send the command
        ShutterComm.Output = Cmd & vbCr
    End If
End Sub

Private Function SendCommand(Cmd As String)
    Dim ResponseString As String
    Dim TimeOut As Long
    
    'Set TimeOut variable for 10 seconds
    TimeOut = AppTickCount + 100
    
    'Clear out the serial port.  Sometimes the shutter spits out a random
    'char on power up
    ResponseString = ShutterComm.Input
    
    'Clear the ResponseString
    ResponseString = ""
        
    'Send the command
    ShutterComm.Output = Cmd & vbCr
    
    ' grab chars until you hit a "RDY<CR>" or you time out
    Do Until Right(ResponseString, 4) = ("RDY" & vbCr)
        ResponseString = ResponseString & ShutterComm.Input
        DoEvents
        If AppTickCount > TimeOut Then
            SendCommand = "-1 Timed out waiting for shutter response"
            Exit Function
        End If
    Loop
    
    SendCommand = ResponseString
    
End Function




