1414' This definition is used to select code that's only applicable for one device type
1515# Const Device = "ObservingConditions"
1616
17- Imports ASCOM.Astrometry
18- Imports ASCOM.Astrometry.AstroUtils
17+ Option Infer On
18+ Option Strict On
19+
20+ Imports System.Threading
1921Imports ASCOM.DeviceInterface
2022Imports ASCOM.Utilities
2123
@@ -31,15 +33,16 @@ Public Class ObservingConditions
3133 ' Driver ID and descriptive string that shows in the Chooser
3234 '
3335 Friend Shared driverID As String = "ASCOM.SimpleSQM.ObservingConditions"
34- Private Shared driverDescription As String = "SimpleSQM sensor "
36+ Private Shared driverDescription As String = "SimpleSQM"
3537
3638 Friend Shared comPortProfileName As String = "COM Port"
37- Friend Shared traceStateProfileName As String = "Debug"
38- Friend Shared comPortDefault As String = "COM1"
39- Friend Shared traceStateDefault As String = "False"
40-
4139 Friend Shared comPort As String
42- Friend Shared traceState As Boolean
40+
41+ Friend WithEvents serial As IO.Ports.SerialPort = New IO.Ports.SerialPort()
42+ Private WithEvents updateTimer As Threading.Timer = New Threading.Timer( AddressOf updateTimer_Tick, Nothing , Timeout.Infinite, Timeout.Infinite)
43+
44+ Private sqmValue As Double = - 1.0
45+ Private sqmUpdateTime As DateTime = Nothing
4346
4447 Private connectedState As Boolean
4548 Private TL As TraceLogger
@@ -71,8 +74,8 @@ Public Class ObservingConditions
7174 If IsConnected Then
7275 MessageBox.Show( "Già connesso." , "SimpleSQM" , MessageBoxButtons.OK, MessageBoxIcon.Information)
7376 End If
74- Using F As SimpleSQM = New SimpleSQM()
75- Dim result As DialogResult = F .ShowDialog()
77+ Using dialog As SimpleSQM = New SimpleSQM()
78+ Dim result As DialogResult = dialog .ShowDialog()
7679 If result = DialogResult.OK Then
7780 WriteProfile()
7881 End If
@@ -85,22 +88,22 @@ Public Class ObservingConditions
8588 End Get
8689 End Property
8790
88- Public Function Action( ByVal ActionName As String , ByVal ActionParameters As String ) As String Implements IObservingConditions.Action
91+ Public Function Action(ActionName As String , ActionParameters As String ) As String Implements IObservingConditions.Action
8992 Throw New ActionNotImplementedException( "Action " & ActionName & " is not supported by this driver" )
9093 End Function
9194
92- Public Sub CommandBlind( ByVal Command As String , Optional ByVal Raw As Boolean = False ) Implements IObservingConditions.CommandBlind
95+ Public Sub CommandBlind(Command As String , Optional Raw As Boolean = False ) Implements IObservingConditions.CommandBlind
9396 CheckConnected( "CommandBlind" )
9497 Throw New MethodNotImplementedException( "CommandBlind" )
9598 End Sub
9699
97- Public Function CommandBool( ByVal Command As String , Optional ByVal Raw As Boolean = False ) As Boolean _
100+ Public Function CommandBool(Command As String , Optional Raw As Boolean = False ) As Boolean _
98101 Implements IObservingConditions.CommandBool
99102 CheckConnected( "CommandBool" )
100103 Throw New MethodNotImplementedException( "CommandBool" )
101104 End Function
102105
103- Public Function CommandString( ByVal Command As String , Optional ByVal Raw As Boolean = False ) As String _
106+ Public Function CommandString(Command As String , Optional Raw As Boolean = False ) As String _
104107 Implements IObservingConditions.CommandString
105108 CheckConnected( "CommandString" )
106109 Throw New MethodNotImplementedException( "CommandString" )
@@ -113,22 +116,91 @@ Public Class ObservingConditions
113116 End Get
114117 Set (value As Boolean )
115118 TL.LogMessage( "Connected Set" , value.ToString())
116- If value = IsConnected Then
119+ If value = connectedState Then
117120 Return
118121 End If
119-
120122 If value Then
121- connectedState = True
123+ If serial.IsOpen Then
124+ Throw New DriverException( "Already connected!" )
125+ End If
126+ If comPort = "" Then
127+ Throw New DriverException( "Set the serial port first." )
128+ End If
122129 TL.LogMessage( "Connected Set" , "Connecting to port " + comPort)
123- ' TODO connect to the device
130+ serial.PortName = comPort
131+ serial.NewLine = vbCr
132+ 'serial.DtrEnable = True
133+ 'serial.RtsEnable = True
134+ serial.ReadTimeout = 1500
135+ serial.BaudRate = 115200
136+ serial.Open()
137+ Thread.Sleep( 300 )
138+ serial.WriteLine( ">" )
139+ Thread.Sleep( 500 )
140+ Dim msg As String
141+ Try
142+ msg = serial.ReadLine().Replace(vbCr, "" ).Trim()
143+ Catch tex1 As TimeoutException
144+ serial.WriteLine( ">" )
145+ Thread.Sleep( 500 )
146+ Try
147+ msg = serial.ReadLine().Replace(vbCr, "" ).Trim()
148+ Catch tex2 As TimeoutException
149+ serial.Close()
150+ Throw New DriverException( "No SimpleSQM device detected on the selected port!" )
151+ Return
152+ End Try
153+ End Try
154+ TL.LogMessage( "SerialPort" , msg)
155+ If msg.StartsWith( "<" ) Then
156+ sqmValue = Double .Parse(msg.Substring( 1 ))
157+ TL.LogMessage( "SQM" , sqmValue.ToString())
158+ sqmUpdateTime = Date .Now
159+ updateTimer.Change( 10000 , 10000 )
160+ connectedState = True
161+ Else
162+ serial.Close()
163+ Throw New DriverException( "No SimpleSQM device detected on the selected port!" )
164+ Return
165+ End If
124166 Else
125- connectedState = False
126167 TL.LogMessage( "Connected Set" , "Disconnecting from port " + comPort)
127- ' TODO disconnect from the device
168+ If serial.IsOpen Then
169+ serial.Close()
170+ End If
171+ updateTimer.Change(Timeout.Infinite, Timeout.Infinite)
172+ sqmValue = - 1.0
173+ sqmUpdateTime = Nothing
174+ connectedState = False
128175 End If
129176 End Set
130177 End Property
131178
179+ Private Sub serialPort_DataReceived(sender As Object , e As IO.Ports.SerialDataReceivedEventArgs) Handles serial.DataReceived
180+ If connectedState Then
181+ Thread.Sleep( 100 )
182+ Try
183+ While serial.BytesToRead > 0
184+ Dim msg As String = serial.ReadExisting()
185+ TL.LogMessage( "SerialPort" , msg)
186+ If msg.StartsWith( "<" ) Then
187+ sqmValue = Double .Parse(msg.Substring( 1 ))
188+ TL.LogMessage( "SQM" , sqmValue.ToString())
189+ sqmUpdateTime = Date .Now
190+ End If
191+ End While
192+ Catch ex As Exception
193+ TL.LogMessage( "SerialPort" , ex.Message)
194+ End Try
195+ End If
196+ End Sub
197+
198+ Private Sub updateTimer_Tick(state As Object )
199+ If serial.IsOpen Then
200+ serial.WriteLine( ">" )
201+ End If
202+ End Sub
203+
132204 Public ReadOnly Property Description As String Implements IObservingConditions.Description
133205 Get
134206 Return driverDescription
@@ -137,8 +209,7 @@ Public Class ObservingConditions
137209
138210 Public ReadOnly Property DriverInfo As String Implements IObservingConditions.DriverInfo
139211 Get
140- Dim m_version As Version = Reflection.Assembly.GetExecutingAssembly().GetName().Version
141- Return "SimpleSQM ASCOM ObservingConditions driver. v" + m_version.Major.ToString() + "." + m_version.Minor.ToString()
212+ Return "SimpleSQM ASCOM driver"
142213 End Get
143214 End Property
144215
@@ -172,10 +243,16 @@ Public Class ObservingConditions
172243
173244 Public Property AveragePeriod() As Double Implements IObservingConditions.AveragePeriod
174245 Get
246+ CheckConnected( "AveragePeriod" )
175247 Return 0.0
176248 End Get
177249 Set (value As Double )
250+ CheckConnected( "AveragePeriod" )
178251 ' Do nothing
252+ ' Check if value is negative for ASCOM Conformance
253+ If value < 0.0 Then
254+ Throw New InvalidValueException( "Negative average period!" )
255+ End If
179256 End Set
180257 End Property
181258
@@ -217,7 +294,11 @@ Public Class ObservingConditions
217294
218295 Public ReadOnly Property SkyQuality() As Double Implements IObservingConditions.SkyQuality
219296 Get
220- Return 0.0 'TODO: Implement!
297+ CheckConnected( "SkyQuality" )
298+ If sqmValue < 0.0 Or sqmValue >= 30 . 0 Then
299+ Throw New DriverException( "Invalid SQM value received." )
300+ End If
301+ Return sqmValue
221302 End Get
222303 End Property
223304
@@ -258,30 +339,19 @@ Public Class ObservingConditions
258339 End Property
259340
260341 Public Function TimeSinceLastUpdate(PropertyName As String ) As Double Implements IObservingConditions.TimeSinceLastUpdate
261- If Not String .IsNullOrEmpty(PropertyName) Then
262- Select Case PropertyName.Trim.ToLowerInvariant
263- Case "skyquality"
264- Return 0.0 'TODO: implement
265- Case "averageperiod"
266- Case "cloudcover"
267- Case "dewpoint"
268- Case "humidity"
269- Case "pressure"
270- Case "rainrate"
271- Case "skybrightness"
272- Case "skytemperature"
273- Case "starfwhm"
274- Case "temperature"
275- Case "winddirection"
276- Case "windgust"
277- Case "windspeed"
278- Throw New MethodNotImplementedException( "TimeSinceLastUpdate(" + PropertyName + ")" )
279- Case Else
280- TL.LogMessage( "TimeSinceLastUpdate" , PropertyName & " - unrecognised" )
281- Throw New InvalidValueException( "TimeSinceLastUpdate(" + PropertyName + ")" )
282- End Select
342+ CheckConnected( "TimeSinceLastUpdate" )
343+ If String .IsNullOrEmpty(PropertyName) Then
344+ If IsNothing(sqmUpdateTime) Then
345+ Throw New DriverException( "sqmUpdateTime = null" )
346+ End If
347+ Return ( Date .Now - sqmUpdateTime).TotalSeconds
348+ ElseIf PropertyName.Trim().ToLowerInvariant().Equals( "skyquality" ) Then
349+ If IsNothing(sqmUpdateTime) Then
350+ Throw New DriverException( "sqmUpdateTime = null" )
351+ End If
352+ Return ( Date .Now - sqmUpdateTime).TotalSeconds
283353 Else
284- Return 0.0 'TODO: implement
354+ Throw New MethodNotImplementedException( "Property not implemented" )
285355 End If
286356 End Function
287357
@@ -291,22 +361,9 @@ Public Class ObservingConditions
291361 Return "Not implemented, data is instantaneous."
292362 Case "skyquality"
293363 Return "Sky quality measured in magnitudes per square arc second."
294- Case "cloudcover"
295- Case "dewpoint"
296- Case "humidity"
297- Case "pressure"
298- Case "rainrate"
299- Case "skybrightness"
300- Case "skytemperature"
301- Case "starfwhm"
302- Case "temperature"
303- Case "winddirection"
304- Case "windgust"
305- Case "windspeed"
306- Throw New MethodNotImplementedException( $"SensorDescription - Property {PropertyName} is not implemented" )
364+ Case Else
365+ Throw New MethodNotImplementedException( "Property not implemented" )
307366 End Select
308- TL.LogMessage( "SensorDescription" , $"Invalid sensor name: {PropertyName}" )
309- Throw New InvalidValueException( $"SensorDescription - Invalid property name: {PropertyName}" )
310367 End Function
311368
312369 Public Sub Refresh() Implements IObservingConditions.Refresh
@@ -330,12 +387,12 @@ Public Class ObservingConditions
330387 End Sub
331388
332389 <ComRegisterFunction()>
333- Public Shared Sub RegisterASCOM( ByVal T As Type)
390+ Public Shared Sub RegisterASCOM(T As Type)
334391 RegUnregASCOM( True )
335392 End Sub
336393
337394 <ComUnregisterFunction()>
338- Public Shared Sub UnregisterASCOM( ByVal T As Type)
395+ Public Shared Sub UnregisterASCOM(T As Type)
339396 RegUnregASCOM( False )
340397 End Sub
341398
@@ -346,7 +403,6 @@ Public Class ObservingConditions
346403 ''' </summary>
347404 Private ReadOnly Property IsConnected As Boolean
348405 Get
349- ' TODO check that the driver hardware connection exists and is connected to the hardware
350406 Return connectedState
351407 End Get
352408 End Property
@@ -355,7 +411,7 @@ Public Class ObservingConditions
355411 ''' Use this function to throw an exception if we aren't connected to the hardware
356412 ''' </summary>
357413 ''' <param name="message"></param>
358- Private Sub CheckConnected( ByVal message As String )
414+ Private Sub CheckConnected(message As String )
359415 If Not IsConnected Then
360416 Throw New NotConnectedException(message)
361417 End If
@@ -367,8 +423,7 @@ Public Class ObservingConditions
367423 Friend Sub ReadProfile()
368424 Using driverProfile As New Profile()
369425 driverProfile.DeviceType = "ObservingConditions"
370- traceState = Convert.ToBoolean(driverProfile.GetValue(driverID, traceStateProfileName, String .Empty, traceStateDefault))
371- comPort = driverProfile.GetValue(driverID, comPortProfileName, String .Empty, comPortDefault)
426+ comPort = driverProfile.GetValue(driverID, comPortProfileName, String .Empty, "" )
372427 End Using
373428 End Sub
374429
@@ -378,12 +433,11 @@ Public Class ObservingConditions
378433 Friend Sub WriteProfile()
379434 Using driverProfile As New Profile()
380435 driverProfile.DeviceType = "ObservingConditions"
381- driverProfile.WriteValue(driverID, traceStateProfileName, traceState.ToString())
382436 driverProfile.WriteValue(driverID, comPortProfileName, comPort.ToString())
383437 End Using
384438
385439 End Sub
386440
387441# End Region
388442
389- End Class
443+ End Class
0 commit comments