forked from fblumenberg/BaseflightGUI2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodCOM.vb
352 lines (318 loc) · 12.7 KB
/
modCOM.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
Module modCOM
Public dtComPorts As New DataTable
Public sSerialSpeeds As String() = {"115200", "57600", "38400", "19200", "9600"}
Public guiDefaultPort As String = "COM9"
Public guiDefaultSerialSpeed As String = "115200"
Public serialPort As IO.Ports.SerialPort
Public isConnected As Boolean = False
Public isRealtime As Boolean = False
Public comError As Boolean = False
Public gpsBuffer As Byte()
Public comTimeOut As Integer = 500
Public isBluetooth As Boolean = False
Public Timeout As Integer = 10
Public USBTimeout As Integer = 10
Public BluetoothTimeout As Integer = 10
Public RebootTimeout As Integer = 120
Public fcCount As Integer = 1
Public LastReceived As DateTime = Now
Public Sub timeoutError()
If serialPort.BytesToRead > 0 Then
serialPort.DiscardInBuffer()
End If
frmMain.timerRealtime.Stop()
comError = True
isConnected = False
MessageBox.Show(frmMain, lngTimeOut.Replace("{sec}", Timeout), lngError, MessageBoxButtons.OK, MessageBoxIcon.Error)
disconnectCOM()
End Sub
Public Sub createDtComPorts()
If dtComPorts.Columns.Count = 0 Then
Dim col As DataColumn
col = New DataColumn("Port", GetType(System.String))
dtComPorts.Columns.Add(col)
col = New DataColumn("Description", GetType(System.String))
dtComPorts.Columns.Add(col)
dtComPorts.TableName = "ComPorts"
End If
End Sub
Public Sub serial_ports_enumerate()
createDtComPorts()
'Enumerate all serial ports
frmMain.cmdConnect.Enabled = True
'Enable the connect button
Dim ports As String() = IO.Ports.SerialPort.GetPortNames()
dtComPorts.Clear()
For Each port As String In ports
Dim newRow As DataRow = dtComPorts.NewRow
newRow("Port") = port
newRow("Description") = getAddPortInformation(port)
dtComPorts.Rows.Add(newRow)
Next
frmMain.cmbCOMPort.ComboBox.DataSource = dtComPorts
frmMain.cmbCOMPort.ComboBox.DisplayMember = "Description"
frmMain.cmbCOMPort.ComboBox.ValueMember = "Port"
frmMain.cmbCOMPort.ComboBox.SelectedValue = guiDefaultPort
'if prefered port is not available then select the first one
If frmMain.cmbCOMPort.Text = "" Then
If frmMain.cmbCOMPort.Items.Count > 0 Then
frmMain.cmbCOMPort.SelectedIndex = 0
End If
End If
'Thisable connect button if there is no selected com port
If frmMain.cmbCOMPort.Items.Count = 0 Then
frmMain.cmdConnect.Enabled = False
End If
End Sub
Private Function getAddPortInformation(ByVal port As String) As String
Dim result As String = port
Dim searcher As New Management.ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity")
For Each queryObj As Management.ManagementObject In searcher.Get()
If InStr(queryObj("Caption"), "(" & port & ")") > 0 Then
'For Each po As Management.PropertyData In queryObj.Properties
' Debug.Write(po.Name & " - " & cccString(po.Value) & vbCrLf)
'Next
result = port & " - " & queryObj("Description")
Exit For
End If
Next
Return result
End Function
Private Function cccString(ByVal value As Object) As String
Dim result As String = ""
If IsNothing(value) = False Then
result = value.ToString
End If
Return result
End Function
Public Sub initCOMPorts()
isStartup = True
serial_ports_enumerate()
For Each speed As String In sSerialSpeeds
frmMain.cmbCOMSpeed.Items.Add(speed)
Next
frmMain.cmbCOMSpeed.SelectedItem = guiDefaultSerialSpeed
'Init serial port object
serialPort = New IO.Ports.SerialPort()
'Set up serial port parameters (at least the ones what we know upfront
serialPort.DataBits = 8
serialPort.Parity = IO.Ports.Parity.None
serialPort.StopBits = IO.Ports.StopBits.One
serialPort.Handshake = IO.Ports.Handshake.None
serialPort.DtrEnable = False
serialPort.RtsEnable = False
'??
serialPort.ReadBufferSize = 8 * 1024 ' 8K byte of read buffer
serialPort.ReadTimeout = comTimeOut * 1000 ' 500msec timeout;
isStartup = False
End Sub
Public Sub disconnectCOM()
frmMain.timerRealtime.Stop() ''Stop timer(s), whatever it takes
If serialPort.IsOpen = True Then
serialPort.DiscardInBuffer()
Dim CloseDown As New Threading.Thread(New Threading.ThreadStart(AddressOf CloseSerialOnExit))
'close port in new thread to avoid hang
CloseDown.Start()
'close port in new thread to avoid hang
End If
frmMain.cmdConnect.Text = "Connect"
frmMain.cmdConnect.Image = Global.BaseflightGUI.My.Resources.Resources.Link_Add_32_n_p
If isConnected = True Then
isConnected = False
End If
frmMain.setButtons(False)
isConnected = False
serial_packet_count = 0
frmMain.lblVPacketReceived.Text = serial_packet_count
End Sub
Private Sub CloseSerialOnExit()
Try
serialPort.Close()
Catch ex As Exception
End Try
End Sub
Public Function connectCOM() As Boolean
Dim result As Boolean = False
comError = False
frmMain.cmdConnect.Image = Global.BaseflightGUI.My.Resources.Resources.Link_Search_32_n_p
frmMain.cmdConnect.Text = "Connecting"
If serialPort.IsOpen = False Then
serialPort.PortName = frmMain.cmbCOMPort.ComboBox.SelectedValue
serialPort.BaudRate = CInt(frmMain.cmbCOMSpeed.Text)
Try
isConnected = True
serialPort.Open()
serialPort.ReadExisting()
Catch
''WRONG, it seems that the combobox selection pointed to a port which is no longer available
MessageBox.Show(frmMain, "Please check that your USB cable is still connected." & vbCrLf & "After you press OK, Serial ports will be re-enumerated", "Error opening COM port", MessageBoxButtons.OK, MessageBoxIcon.Error)
serial_ports_enumerate()
frmMain.cmdConnect.Text = "Connect"
frmMain.cmdConnect.Image = Global.BaseflightGUI.My.Resources.Resources.Link_Add_32_n_p
Return False
End Try
End If
''We have to do it for a couple of times to ensure that we will have parameters loaded
Application.DoEvents()
Try
serialPort.WriteTimeout = 1000 * 15
serialPort.Write("Exit" & vbCrLf)
serialPort.ReadExisting()
For i As Integer = 0 To 10
MSPquery(MSP_BOXNAMES)
readCOM()
If comError = True Then
result = False
frmMain.cmdConnect.Text = "Connect"
frmMain.cmdConnect.Image = Global.BaseflightGUI.My.Resources.Resources.Link_Add_32_n_p
Exit For
End If
mw_params = New mw_settings(iPidItems, iCheckBoxItems, iSoftwareVersion)
If iCheckBoxItems > 0 Then
frmMain.cmdConnect.Image = Global.BaseflightGUI.My.Resources.Resources.Link_Remove_32_n_p()
frmMain.cmdConnect.Text = "Disconnect"
isConnected = True
result = True
Exit For
End If
Next
Catch ex As Exception
lostConnection()
End Try
Return result
End Function
Public Sub lostConnection()
Try
frmMain.timerRealtime.Stop()
comError = True
isConnected = False
MessageBox.Show(frmMain, "Please check that your USB cable is still connected.", "Error finding FlightControl", MessageBoxButtons.OK, MessageBoxIcon.Error)
disconnectCOM()
Catch ex As Exception
End Try
End Sub
Public Sub readCOM()
Try
Dim bIsPortOpen As Boolean = serialPort.IsOpen
Catch
'Hmm, if this throws an exception it should mean that we have an issue here
comError = True
Return
End Try
If serialPort.IsOpen Then
'Just process what is received. Get received commands and put them into
Try
If isRealtime = False Then
Dim startTime As DateTime = Now
Do While serialPort.BytesToRead = 0 And comError = False
If Now > DateAdd(DateInterval.Second, Timeout, starttime) Then
comError = True
End If
Application.DoEvents()
Loop
System.Threading.Thread.Sleep(250)
frmMain.lblRealtimeWarning.Visible = False
frmMain.lblMapWarning.Visible = False
frmMain.lblRCWarning.Visible = False
frmMain.lblParameterWarning.Visible = False
End If
If comError = False Then
Try
If Timeout = RebootTimeout Then
If fcCount > 0 Then
fcCount -= 1
Else
If isBluetooth = False Then
Timeout = USBTimeout
Else
Timeout = BluetoothTimeout
End If
End If
End If
ReadMSP()
Catch
'port not opened, (it could happen when U disconnect the usb cable while connected
'comError = true; //do nothing
'return;
lostConnection()
End Try
Else
lostConnection()
End If
Catch ex As Exception
lostConnection()
End Try
Else
'port not opened, (it could happen when U disconnect the usb cable while connected
lostConnection()
End If
End Sub
Public Sub readBaseflightBasics()
sUID = "???"
MSPquery(MSP_UID)
readCOM()
If comError = True Then Exit Sub
frmMain.lblVUID.Text = sUID
frmMain.updateStatus()
MSPquery(MSP_IDENT)
readCOM()
If comError = True Then Exit Sub
updateParameterIdent()
MSPquery(MSP_RC)
readCOM()
If comError = True Then Exit Sub
frmMain.updateStatus()
MSPquery(MSP_BOXNAMES)
readCOM()
If comError = True Then Exit Sub
frmMain.updateStatus()
If mw_gui.version >= 220 Then
MSPquery(MSP_BOXIDS)
readCOM()
If comError = True Then Exit Sub
frmMain.updateStatus()
Else
Dim BOXIDS(iCheckBoxItems - 1) As Integer
For i As Integer = 0 To iCheckBoxItems - 1
BOXIDS(i) = i
Next
iBoxIdents = BOXIDS
End If
MSPquery(MSP_BOX)
readCOM()
If comError = True Then Exit Sub
frmMain.lblVPacketReceived.Text = serial_packet_count
frmMain.lblVPacketError.Text = serial_error_count
Application.DoEvents()
If cfgBoxWidth = 4 Then
If AUX_CHANNELS >= 8 Then
boxAUX_CHANNELS = 8
Else
boxAUX_CHANNELS = AUX_CHANNELS
End If
Else
boxAUX_CHANNELS = 4
End If
frmMain.updateStatus()
frmMain.initRCChannel()
create_aux_panal()
frmMain.initRealtimeChannel()
initIndicatorLamps()
MSPquery(MSP_MOTOR)
readCOM()
If comError = True Then Exit Sub
frmMain.Motor.SetMotorsIndicatorParameters(mw_gui.motors, mw_gui.servos, mw_gui.multiType)
frmMain.updateStatus()
MSPquery(MSP_SERVO)
readCOM()
If comError = True Then Exit Sub
'ToDo Servos must be done
frmMain.updateStatus()
frmMain.lblVParameterBaseflightVersion.Text = ""
MSPquery(MSP_FIRMWARE)
readCOM()
If comError = True Then Exit Sub
frmMain.lblVParameterBaseflightVersion.Text = mw_gui.firmware
frmMain.updateStatus()
End Sub
End Module