forked from fblumenberg/BaseflightGUI2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodFirmwareUpdate.vb
114 lines (104 loc) · 4.73 KB
/
modFirmwareUpdate.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
Module modFirmwareUpdate
Public firmwareFile As String = ""
Public STMFlashLoader As String = ""
Public StartFWUpdate As String = "RRR"
Public Sub initFirmwareUpdate()
If isStartup = True Then Exit Sub
Dim openComPort As Boolean = False
frmMain.LoadingCircle.Visible = False
firmwareFile = frmMain.txtFirmwareFile.Text
STMFlashLoader = Application.StartupPath & "\STMFlashLoader.exe"
ini.Write("GUI", "FirmwareShowOutput", frmMain.chkFWShowOutput.Checked)
If System.IO.File.Exists(firmwareFile) = True And System.IO.File.Exists(STMFlashLoader) = True Then
frmMain.cmdFWUpdate.Enabled = True
Else
frmMain.cmdFWUpdate.Enabled = False
End If
End Sub
Public Sub readFirmwareFile()
Dim ofdLoadParameters As New OpenFileDialog()
ofdLoadParameters.Filter = "Baseflight Firmware File|*.hex"
ofdLoadParameters.Title = "Load Firmware"
ofdLoadParameters.InitialDirectory = sFirmwareFolder
If ofdLoadParameters.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'we have file to open
frmMain.txtFirmwareFile.Text = ofdLoadParameters.FileName
If sFirmwareFolder <> System.IO.Path.GetDirectoryName(ofdLoadParameters.FileName) Then
sFirmwareFolder = System.IO.Path.GetDirectoryName(ofdLoadParameters.FileName)
ini.Write("GUI", "FirmwareFolder", sFirmwareFolder)
End If
End If
initFirmwareUpdate()
End Sub
Public Sub updateFirmware()
Dim result As Integer = 0
frmMain.LoadingCircle.Visible = True
frmMain.LoadingCircle.Active = True
Application.DoEvents()
frmMain.lblFWError.Visible = False
frmMain.lblFWSuccessful.Visible = False
If frmMain.chkFWSendR.Checked = True Then
If serialPort.IsOpen = False Then
serialPort.PortName = frmMain.cmbCOMPort.ComboBox.SelectedValue
serialPort.BaudRate = CInt(frmMain.cmbCOMSpeed.Text)
serialPort.Open()
serialPort.ReadExisting()
End If
serialPort.Write(StartFWUpdate)
System.Threading.Thread.Sleep(2000)
End If
If serialPort.IsOpen = True Then
serialPort.Close()
End If
Dim Arguments As String = ""
Arguments = Arguments & " -c --pn " & serialPort.PortName.Replace("COM", "") & " --br " & serialPort.BaudRate & " --db 8"
Arguments = Arguments & " -i STM32_Med-density_128K"
Arguments = Arguments & " -e --all"
Arguments = Arguments & " -d --fn """ & firmwareFile & """"
Arguments = Arguments & " -r --a 0x8000000"
Dim myProcess As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo()
'msiexec als zu startendes Programm und die Parameter lauten Bspw. /a C:\Install.MSI /quiet
processStartInfo.FileName = STMFlashLoader
processStartInfo.Arguments = Arguments
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = False
processStartInfo.CreateNoWindow = Not frmMain.chkFWShowOutput.Checked
'processStartInfo.RedirectStandardOutput = True
'processStartInfo.RedirectStandardError = True
Try
myProcess = System.Diagnostics.Process.Start(processStartInfo)
Do
If Not myProcess.HasExited Then
' Refresh the current process property values.
myProcess.Refresh()
If myProcess.Responding Then
'Console.WriteLine("Status = Running")
' Display the results.
Else
Console.WriteLine("Status = Not Responding")
End If
End If
Application.DoEvents()
Loop While Not myProcess.WaitForExit(1000)
result = myProcess.ExitCode
If result = 0 Then
frmMain.lblFWSuccessful.Visible = True
Else
frmMain.lblFWError.Visible = True
End If
Catch ex As Exception
MessageBox.Show(frmMain, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If Not (myProcess Is Nothing) Then
myProcess.Dispose()
End If
End Try
frmMain.LoadingCircle.Visible = False
frmMain.LoadingCircle.Active = False
disconnectCOM()
System.Threading.Thread.Sleep(5000)
frmMain.tabMain.SelectedIndex = 0
End Sub
End Module