etc-FwMultiDownloader/MainForm.vb
2025-12-17 15:34:38 +09:00

627 lines
23 KiB
VB.net

Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Imports System.Text
Imports System.Threading
Imports System.Windows.Forms.VisualStyles
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar
Imports FW_Multi_Downloader.My
Public Class MainForm
Const CNT_PC As Integer = 10
Public cmdProcess(CNT_PC) As Process
Public startInfo(CNT_PC) As ProcessStartInfo
Public runThread(CNT_PC) As Thread
Public comboBoxPort(CNT_PC) As ComboBox
Public buttonPortScan(CNT_PC) As Button
Public comboBoxTarget(CNT_PC) As ComboBox
Public textBoxBinary(CNT_PC) As TextBox
Public buttonFile(CNT_PC) As Button
Public textBoxCmdWindow(CNT_PC) As TextBox
Public buttonDisable(CNT_PC) As Button
Public buttonDownload(CNT_PC) As Button
Public buttonEnable(CNT_PC) As Button
Public executablePath As String = System.AppDomain.CurrentDomain.BaseDirectory() & My.Settings.ExecutableFlashLoader
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SetGroupBoxProcessControls()
Initialize()
Me.Text = "Flash Loader Util " & My.Settings.ProgramVersion
End Sub
Private Sub SetGroupBoxProcessControls()
comboBoxPort(1) = comboBoxPort01
comboBoxPort(2) = comboBoxPort02
comboBoxPort(3) = comboBoxPort03
comboBoxPort(4) = comboBoxPort04
comboBoxPort(5) = comboBoxPort05
comboBoxPort(6) = comboBoxPort06
comboBoxPort(7) = comboBoxPort07
comboBoxPort(8) = comboBoxPort08
comboBoxPort(9) = comboBoxPort09
comboBoxPort(10) = comboBoxPort10
buttonPortScan(1) = buttonPortScan01
buttonPortScan(2) = buttonPortScan02
buttonPortScan(3) = buttonPortScan03
buttonPortScan(4) = buttonPortScan04
buttonPortScan(5) = buttonPortScan05
buttonPortScan(6) = buttonPortScan06
buttonPortScan(7) = buttonPortScan07
buttonPortScan(8) = buttonPortScan08
buttonPortScan(9) = buttonPortScan09
buttonPortScan(10) = buttonPortScan10
comboBoxTarget(1) = comboBoxTarget01
comboBoxTarget(2) = comboBoxTarget02
comboBoxTarget(3) = comboBoxTarget03
comboBoxTarget(4) = comboBoxTarget04
comboBoxTarget(5) = comboBoxTarget05
comboBoxTarget(6) = comboBoxTarget06
comboBoxTarget(7) = comboBoxTarget07
comboBoxTarget(8) = comboBoxTarget08
comboBoxTarget(9) = comboBoxTarget09
comboBoxTarget(10) = comboBoxTarget10
textBoxBinary(1) = textBoxBinary01
textBoxBinary(2) = textBoxBinary02
textBoxBinary(3) = textBoxBinary03
textBoxBinary(4) = textBoxBinary04
textBoxBinary(5) = textBoxBinary05
textBoxBinary(6) = textBoxBinary06
textBoxBinary(7) = textBoxBinary07
textBoxBinary(8) = textBoxBinary08
textBoxBinary(9) = textBoxBinary09
textBoxBinary(10) = textBoxBinary10
buttonFile(1) = buttonFile01
buttonFile(2) = buttonFile02
buttonFile(3) = buttonFile03
buttonFile(4) = buttonFile04
buttonFile(5) = buttonFile05
buttonFile(6) = buttonFile06
buttonFile(7) = buttonFile07
buttonFile(8) = buttonFile08
buttonFile(9) = buttonFile09
buttonFile(10) = buttonFile10
textBoxCmdWindow(1) = textBoxCmdWindow01
textBoxCmdWindow(2) = textBoxCmdWindow02
textBoxCmdWindow(3) = textBoxCmdWindow03
textBoxCmdWindow(4) = textBoxCmdWindow04
textBoxCmdWindow(5) = textBoxCmdWindow05
textBoxCmdWindow(6) = textBoxCmdWindow06
textBoxCmdWindow(7) = textBoxCmdWindow07
textBoxCmdWindow(8) = textBoxCmdWindow08
textBoxCmdWindow(9) = textBoxCmdWindow09
textBoxCmdWindow(10) = textBoxCmdWindow10
buttonDisable(1) = buttonDisable01
buttonDisable(2) = buttonDisable02
buttonDisable(3) = buttonDisable03
buttonDisable(4) = buttonDisable04
buttonDisable(5) = buttonDisable05
buttonDisable(6) = buttonDisable06
buttonDisable(7) = buttonDisable07
buttonDisable(8) = buttonDisable08
buttonDisable(9) = buttonDisable09
buttonDisable(10) = buttonDisable10
buttonDownload(1) = buttonDownload01
buttonDownload(2) = buttonDownload02
buttonDownload(3) = buttonDownload03
buttonDownload(4) = buttonDownload04
buttonDownload(5) = buttonDownload05
buttonDownload(6) = buttonDownload06
buttonDownload(7) = buttonDownload07
buttonDownload(8) = buttonDownload08
buttonDownload(9) = buttonDownload09
buttonDownload(10) = buttonDownload10
buttonEnable(1) = buttonEnable01
buttonEnable(2) = buttonEnable02
buttonEnable(3) = buttonEnable03
buttonEnable(4) = buttonEnable04
buttonEnable(5) = buttonEnable05
buttonEnable(6) = buttonEnable06
buttonEnable(7) = buttonEnable07
buttonEnable(8) = buttonEnable08
buttonEnable(9) = buttonEnable09
buttonEnable(10) = buttonEnable10
End Sub
Private Sub Initialize()
SetDefaultSetting()
HandleButton()
End Sub
Private Sub SetDefaultSetting()
Dim baudRate() As String = My.Settings.BaudRate.Split("|")
comboBoxBaudRate.Items.AddRange(baudRate)
comboBoxBaudRate.SelectedIndex = 0
LoadComportName(0)
Dim target() As String = My.Settings.Target.Split("|")
For i = 1 To 10
comboBoxTarget(i).Items.AddRange(target)
Next
comboBoxTargetDefault.Items.AddRange(target)
comboBoxTargetDefault.SelectedIndex = 0
End Sub
Private Sub HandleButton()
For i = 1 To 10
If comboBoxPort(i).SelectedIndex > -1 And comboBoxBaudRate.SelectedIndex > -1 And comboBoxTarget(i).SelectedIndex > -1 Then
buttonDisable(i).Enabled = True
buttonEnable(i).Enabled = True
If textBoxBinary(i).Text.Trim.Length > 0 Then
buttonDownload(i).Enabled = True
Else
buttonDownload(i).Enabled = False
End If
Else
buttonDisable(i).Enabled = False
buttonDownload(i).Enabled = False
buttonEnable(i).Enabled = False
End If
Next
End Sub
Private Sub LoadComportName(i As Int16)
Try
Dim portName() As String = SerialPort.GetPortNames
If i = 0 Then
For j = 1 To 10
comboBoxPort(j).Items.Clear()
comboBoxPort(j).Items.AddRange(portName)
comboBoxPort(j).Text = "PRESS!"
Next
Else
comboBoxPort(i).Items.Clear()
comboBoxPort(i).Items.AddRange(portName)
comboBoxPort(i).Text = "PRESS!"
End If
Catch ex As Exception
MsgBox("COM 포트 에러" & vbCrLf & "", vbOK + vbCritical, Me.Text)
End Try
End Sub
Private Function Openfile() As String
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.Title = "바이너리 파일 불러오기"
ofd.FileName = ""
ofd.Filter = "바이너리 파일(*.bin) | *.bin"
Dim dr As DialogResult = ofd.ShowDialog
If dr = DialogResult.OK Then
Return ofd.FileName
End If
Return "fail"
End Function
Private Sub ComboBoxTargetDefault_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxTargetDefault.SelectedIndexChanged
For i = 1 To 10
comboBoxTarget(i).SelectedIndex = comboBoxTargetDefault.SelectedIndex
Next
HandleButton()
End Sub
Private Sub ButtonFileDefault_Click(sender As Object, e As EventArgs) Handles buttonFileDefault.Click
Dim fileAddress As String = Openfile()
If fileAddress <> "fail" Then
textBoxBinaryDefault.Text = fileAddress
End If
HandleButton()
End Sub
Private Sub TextBoxBinaryDefault_TextChanged(sender As Object, e As EventArgs) Handles textBoxBinaryDefault.TextChanged
If textBoxBinaryDefault.Text.Length > 0 Then
For i = 1 To 10
textBoxBinary(i).Text = textBoxBinaryDefault.Text
Next
End If
HandleButton()
End Sub
Private Sub buttonDisableAll_Click(sender As Object, e As EventArgs) Handles buttonDisableAll.Click
For i = 1 To 10
Dim processNumber As Int16 = i
If buttonDisable(processNumber).Enabled = True Then
runThread(processNumber) = New Thread(Sub() RunDisableReadProtection(processNumber))
runThread(processNumber).Start()
End If
Next
End Sub
Private Sub ButtonDownloadAll_Click(sender As Object, e As EventArgs) Handles buttonDownloadAll.Click
For i = 1 To 10
Dim processNumber As Int16 = i
If buttonDownload(processNumber).Enabled = True Then
runThread(processNumber) = New Thread(Sub() RunDownload(processNumber))
runThread(processNumber).Start()
End If
Next
End Sub
Private Sub buttonEnableAll_Click(sender As Object, e As EventArgs) Handles buttonEnableAll.Click
For i = 1 To 10
Dim processNumber As Int16 = i
If buttonEnable(processNumber).Enabled = True Then
runThread(processNumber) = New Thread(Sub() RunEnableReadProtection(processNumber))
runThread(processNumber).Start()
End If
Next
End Sub
Private Sub ButtonPortScan_Click(sender As Object, e As EventArgs) _
Handles buttonPortScan01.Click,
buttonPortScan02.Click,
buttonPortScan03.Click,
buttonPortScan04.Click,
buttonPortScan05.Click,
buttonPortScan06.Click,
buttonPortScan07.Click,
buttonPortScan08.Click,
buttonPortScan09.Click,
buttonPortScan10.Click
Dim clickedButton As Button = TryCast(sender, Button)
Dim processNumber As Short = clickedButton.Parent.TabIndex
LoadComportName(processNumber)
End Sub
Private Sub ComboBoxPort_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles comboBoxPort01.SelectedIndexChanged,
comboBoxPort02.SelectedIndexChanged,
comboBoxPort03.SelectedIndexChanged,
comboBoxPort04.SelectedIndexChanged,
comboBoxPort05.SelectedIndexChanged,
comboBoxPort06.SelectedIndexChanged,
comboBoxPort07.SelectedIndexChanged,
comboBoxPort08.SelectedIndexChanged,
comboBoxPort09.SelectedIndexChanged,
comboBoxPort10.SelectedIndexChanged
Dim clickedComboBox As ComboBox = TryCast(sender, ComboBox)
Dim processNumber As Short = clickedComboBox.Parent.TabIndex
For i = 1 To 10
If comboBoxPort(i).Text <> "PRESS!" And comboBoxPort(i).Text = clickedComboBox.Text And i <> processNumber Then
MsgBox("Process" & i & "에 선택된 Port Name 입니다." & vbCrLf & "Port Name 을 다시 선택하세요.", vbCritical, Me.Text)
clickedComboBox.SelectedIndex = -1
clickedComboBox.Text = "PRESS!"
Return
End If
Next
HandleButton()
End Sub
Private Sub ComboBoxTarget_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles comboBoxTarget01.SelectedIndexChanged,
comboBoxTarget02.SelectedIndexChanged,
comboBoxTarget03.SelectedIndexChanged,
comboBoxTarget04.SelectedIndexChanged,
comboBoxTarget05.SelectedIndexChanged,
comboBoxTarget06.SelectedIndexChanged,
comboBoxTarget07.SelectedIndexChanged,
comboBoxTarget08.SelectedIndexChanged,
comboBoxTarget09.SelectedIndexChanged,
comboBoxTarget10.SelectedIndexChanged
Dim clickedComboBox As ComboBox = TryCast(sender, ComboBox)
Dim processNumber As Short = clickedComboBox.Parent.TabIndex
comboBoxTarget(processNumber).SelectedIndex = clickedComboBox.SelectedIndex
HandleButton()
End Sub
Private Sub ButtonFile_Click(sender As Object, e As EventArgs) _
Handles buttonFile01.Click,
buttonFile02.Click,
buttonFile03.Click,
buttonFile04.Click,
buttonFile05.Click,
buttonFile06.Click,
buttonFile07.Click,
buttonFile08.Click,
buttonFile09.Click,
buttonFile10.Click
Dim fileAddress As String = Openfile()
If fileAddress <> "fail" Then
Dim clickedButton As Button = TryCast(sender, Button)
Dim processNumber As Short = clickedButton.Parent.TabIndex
textBoxBinary(processNumber).Text = fileAddress
End If
HandleButton()
End Sub
Private Sub ButtonDisable_Click(sender As Object, e As EventArgs) _
Handles buttonDisable01.Click,
buttonDisable02.Click,
buttonDisable03.Click,
buttonDisable04.Click,
buttonDisable05.Click,
buttonDisable06.Click,
buttonDisable07.Click,
buttonDisable08.Click,
buttonDisable09.Click,
buttonDisable10.Click
Dim clickedButton As Button = TryCast(sender, Button)
Dim processNumber As Short = clickedButton.Parent.TabIndex
runThread(processNumber) = New Thread(Sub() RunDisableReadProtection(processNumber))
runThread(processNumber).Start()
End Sub
Private Sub ButtonDownload_Click(sender As System.Object, e As System.EventArgs) _
Handles buttonDownload01.Click,
buttonDownload02.Click,
buttonDownload03.Click,
buttonDownload04.Click,
buttonDownload05.Click,
buttonDownload06.Click,
buttonDownload07.Click,
buttonDownload08.Click,
buttonDownload09.Click,
buttonDownload10.Click
Dim clickedButton As Button = TryCast(sender, Button)
Dim processNumber As Short = clickedButton.Parent.TabIndex
runThread(processNumber) = New Thread(Sub() RunDownload(processNumber))
runThread(processNumber).Start()
End Sub
Private Sub ButtonEnable_Click(sender As Object, e As EventArgs) _
Handles buttonEnable01.Click,
buttonEnable02.Click,
buttonEnable03.Click,
buttonEnable04.Click,
buttonEnable05.Click,
buttonEnable06.Click,
buttonEnable07.Click,
buttonEnable08.Click,
buttonEnable09.Click,
buttonEnable10.Click
Dim clickedButton As Button = TryCast(sender, Button)
Dim processNumber As Short = clickedButton.Parent.TabIndex
runThread(processNumber) = New Thread(Sub() RunEnableReadProtection(processNumber))
runThread(processNumber).Start()
End Sub
Private Sub RunDisableReadProtection(i As Int16)
Dim port As String = GetPort(i)
Dim baudRate As String = GetBaudRate(i)
Dim target As String = GetTarget(i)
Dim binary As String = getBinary(i)
Try
Dim arguments As String = "-c --pn " & port & " --br " & baudRate & " --db 8 --pr Even -i " & target & " -p --drp"
Dim cmdProcess As New Process()
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = """" & executablePath & """"
startInfo.Arguments = arguments
'startInfo.UseShellExecute = False
'startInfo.RedirectStandardOutput = True
'startInfo.CreateNoWindow = True
startInfo.UseShellExecute = True
startInfo.RedirectStandardOutput = False
startInfo.CreateNoWindow = False
ClearTextBoxCmdWindow(i)
UpdateTextBoxCmdWindow(i, startInfo.FileName)
UpdateTextBoxCmdWindow(i, Environment.NewLine & startInfo.Arguments)
cmdProcess.StartInfo = startInfo
cmdProcess.Start()
UpdateTextBoxCmdWindow(i, Environment.NewLine & Environment.NewLine & "Disabling Read Protection...")
' 표준 출력 스트림 리더 가져오기
'Dim reader As StreamReader = cmdProcess.StandardOutput
'Dim line As String
'' 프로세스가 끝날 때까지 또는 스트림의 끝에 도달할 때까지 한 줄씩 읽기
'While Not reader.EndOfStream
' line = reader.ReadLine().Trim
' ' 여기서 읽어온 한 줄(line)을 처리합니다 (예: Console.WriteLine(line) 또는 UI 업데이트)
' UpdateTextBoxCmdWindow(i, Environment.NewLine & line)
'End While
cmdProcess.WaitForExit()
UpdateTextBoxCmdWindow(i, Environment.NewLine & "Disable Read Protection finished.")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub RunDownload(i As Int16)
Dim port As String = GetPort(i)
Dim baudRate As String = GetBaudRate(i)
Dim target As String = GetTarget(i)
Dim binary As String = getBinary(i)
Try
Dim arguments As String = "-c --pn " & port & " --br " & baudRate & " -i " & target & " -e --all -d --a 08000000 --fn " & binary & " -p --drp"
Dim cmdProcess As New Process()
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = """" & executablePath & """"
startInfo.Arguments = arguments
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
startInfo.CreateNoWindow = True
ClearTextBoxCmdWindow(i)
UpdateTextBoxCmdWindow(i, startInfo.FileName)
UpdateTextBoxCmdWindow(i, Environment.NewLine & startInfo.Arguments)
cmdProcess.StartInfo = startInfo
cmdProcess.Start()
UpdateTextBoxCmdWindow(i, Environment.NewLine & Environment.NewLine & "STARTING...")
' 표준 출력 스트림 리더 가져오기
Dim reader As StreamReader = cmdProcess.StandardOutput
Dim line As String
' 프로세스가 끝날 때까지 또는 스트림의 끝에 도달할 때까지 한 줄씩 읽기
While Not reader.EndOfStream
line = reader.ReadLine().Trim
' 여기서 읽어온 한 줄(line)을 처리합니다 (예: Console.WriteLine(line) 또는 UI 업데이트)
UpdateTextBoxCmdWindow(i, Environment.NewLine & line)
End While
cmdProcess.WaitForExit()
UpdateTextBoxCmdWindow(i, Environment.NewLine & "Download finished.")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub RunEnableReadProtection(i As Int16)
Dim port As String = GetPort(i)
Dim baudRate As String = GetBaudRate(i)
Dim target As String = GetTarget(i)
Dim binary As String = getBinary(i)
Try
Dim arguments As String = "-c --pn " & port & " --br " & baudRate & " --db 8 --pr Even -i " & target & " -p --erp"
Dim cmdProcess As New Process()
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = """" & executablePath & """"
startInfo.Arguments = arguments
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
startInfo.CreateNoWindow = True
ClearTextBoxCmdWindow(i)
UpdateTextBoxCmdWindow(i, startInfo.FileName)
UpdateTextBoxCmdWindow(i, Environment.NewLine & startInfo.Arguments)
cmdProcess.StartInfo = startInfo
cmdProcess.Start()
UpdateTextBoxCmdWindow(i, Environment.NewLine & Environment.NewLine & "Enabling Read Protection...")
' 표준 출력 스트림 리더 가져오기
Dim reader As StreamReader = cmdProcess.StandardOutput
Dim line As String
' 프로세스가 끝날 때까지 또는 스트림의 끝에 도달할 때까지 한 줄씩 읽기
While Not reader.EndOfStream
line = reader.ReadLine().Trim
' 여기서 읽어온 한 줄(line)을 처리합니다 (예: Console.WriteLine(line) 또는 UI 업데이트)
UpdateTextBoxCmdWindow(i, Environment.NewLine & line)
End While
cmdProcess.WaitForExit()
UpdateTextBoxCmdWindow(i, Environment.NewLine & "Enable Read Protection finished.")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function GetPort(i As Int16) As String
Dim port As String
If comboBoxPort(i).InvokeRequired Then
comboBoxPort(i).Invoke(Sub()
port = comboBoxPort(i).Text.Replace("COM", "")
End Sub)
Else
port = comboBoxPort(i).Text.Replace("COM", "")
End If
Return port
End Function
Private Function GetBaudRate(i As Int16) As String
Dim baudRate As String
If comboBoxBaudRate.InvokeRequired Then
comboBoxBaudRate.Invoke(Sub()
BaudRate = comboBoxBaudRate.Text
End Sub)
Else
BaudRate = comboBoxBaudRate.Text
End If
Return baudRate
End Function
Private Function GetTarget(i As Int16) As String
Dim target As String
If comboBoxTarget(i).InvokeRequired Then
comboBoxTarget(i).Invoke(Sub()
target = comboBoxTarget(i).Text
End Sub)
Else
target = comboBoxTarget(i).Text
End If
Return target
End Function
Private Function getBinary(i As Int16) As String
Dim binary As String
If textBoxBinary(i).InvokeRequired Then
textBoxBinary(i).Invoke(Sub()
binary = """" & textBoxBinary(i).Text & """"
End Sub)
Else
binary = """" & textBoxBinary(i).Text & """"
End If
Return binary
End Function
Private Sub UpdateTextBoxCmdWindow(i As Int16, state As String)
If textBoxCmdWindow(i).InvokeRequired Then
textBoxCmdWindow(i).Invoke(Sub()
textBoxCmdWindow(i).AppendText(state)
End Sub)
Else
textBoxCmdWindow(i).AppendText(state)
End If
End Sub
Private Sub ClearTextBoxCmdWindow(i As Int16)
If textBoxCmdWindow(i).InvokeRequired Then
textBoxCmdWindow(i).Invoke(Sub()
textBoxCmdWindow(i).Clear()
End Sub)
Else
textBoxCmdWindow(i).Clear()
End If
End Sub
Private Sub buttonClean_Click(sender As Object, e As EventArgs) Handles buttonClean.Click
Initialize()
For i = 1 To 10
ClearTextBoxCmdWindow(i)
Next
End Sub
End Class