📄 frmmain.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmMain
Caption = "MegaLoader V0.5 http://www.psunsky.com.cn"
ClientHeight = 2535
ClientLeft = 60
ClientTop = 345
ClientWidth = 6330
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 2535
ScaleWidth = 6330
StartUpPosition = 3 '窗口缺省
Begin MSComDlg.CommonDialog dlgFile
Left = 0
Top = 600
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.CommandButton cmdExit
Caption = "Free"
Height = 375
Left = 1800
TabIndex = 5
Top = 1800
Width = 1095
End
Begin VB.CommandButton cmdWrite
Caption = "Write!"
Height = 375
Left = 600
TabIndex = 4
Top = 1800
Width = 1095
End
Begin VB.CommandButton cmdFlashFile
Caption = "FlashFile"
Height = 375
Left = 4560
TabIndex = 1
Top = 240
Width = 1215
End
Begin VB.TextBox txtFile
Height = 375
Left = 600
TabIndex = 0
Text = "感谢OurAVR.com的支持!"
Top = 240
Width = 3855
End
Begin MSCommLib.MSComm MSComm1
Left = 0
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.Label lblMsg
Caption = "状态"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 375
Left = 600
TabIndex = 3
Top = 1200
Width = 5415
End
Begin VB.Label lblChip
Caption = "芯片信息"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 255
Left = 600
TabIndex = 2
Top = 840
Width = 5175
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim FlashBuf() As Byte
Dim lFile As Long
Dim iWaitChar%, iPage%, iErr%, iPageByte%, iPageMax%
Dim dTime As Double
Private Sub Msg(ByVal sMsg$)
lblMsg.Caption = sMsg
End Sub
Private Function ToHex(ByVal data As Byte) As String
ToHex = Hex(data)
If (data < 16) Then ToHex = "0" & ToHex
End Function
Private Function InitComm(ByVal iPort)
On Error GoTo ErrMsg
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.Settings = "19200,n,8,1" '可自己修改Baud
MSComm1.RThreshold = 1
'MSComm1.InputMode = comInputModeBinary
MSComm1.CommPort = iPort
MSComm1.PortOpen = True
InitComm = True
Exit Function
ErrMsg:
MsgBox Err.Description
InitComm = False
End Function
Private Sub cmdExit_Click()
MSComm1.Output = "E"
End Sub
Private Sub cmdFlashFile_Click()
Dim sFile$, I%
dlgFile.Filter = "*.bin|*.bin"
dlgFile.ShowOpen
sFile = dlgFile.FileName
txtFile = sFile
'sFile = App.Path & "\w0.bin"
Open sFile For Binary Access Read As #1
lFile = LOF(1)
ReDim FlashBuf(lFile)
Get #1, , FlashBuf
Close #1
'For I = 0 To 3
' Debug.Print ToHex(FlashBuf(I))
'Next I
Msg "文件已读入!长度:" & lFile
End Sub
Private Sub cmdWrite_Click()
iPageMax = lFile \ iPageByte
If (lFile Mod iPageByte) = 0 Then iPageMax = iPageMax - 1
MSComm1.Output = "W" '开始DownLoad
iPage = -1
iErr = 0
iWaitChar = 1
dTime = Now
End Sub
Private Sub Form_Load()
If InitComm(1) Then
Msg ("串口打开!")
End If
iWaitChar = 1
iPageByte = 64
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive '接收到数据
If (MSComm1.InBufferCount >= iWaitChar) Then '已经有足够数据
RecvCmd (MSComm1.Input) '处理单片机的应答
End If
End Select
End Sub
Private Sub RecvCmd(ByVal sCmd$)
Select Case sCmd
Case ">"
MSComm1.Output = "<"
iWaitChar = 3
Msg "开始连接..."
Case "!" '正确
iWaitChar = 1
iPage = iPage + 1
WriteFlash (iPage)
Msg ("成功写到" & iPage & "页,错误次数" & iErr & ",时间" & (Now - dTime))
Case "@" '错误
iWaitChar = 1
iErr = iErr + 1
WriteFlash (iPage) '再写
Msg ("成功写到" & iPage & "页,错误次数" & iErr)
Case "E" '退出boot
Msg "正确退出"
Case Else
If (iWaitChar = 3) Then
ChipInfo (sCmd)
Else
Msg ("错误指令..." & sCmd)
MSComm1.Output = "E"
End If
iWaitChar = 1
End Select
End Sub
Private Sub ChipInfo(ByVal sInfo$)
Dim sTemp$, sMsg$
On Error Resume Next
sTemp = Left(sInfo, 1)
If (sTemp = "0") Then
sMsg = "Mega8,"
iPageByte = 64
Else
sMsg = "Mega16,"
iPageByte = 128
End If
sTemp = Mid(sInfo, 3, 1)
sTemp = "版本" & Str((Asc(sTemp) - Asc("f")) / 10 + 1)
sMsg = sMsg + sTemp
lblChip.Caption = sMsg
Msg ("连接成功,可下载...")
End Sub
Private Sub WriteFlash(ByVal iPage%)
Dim Crc%, iTemp As Byte, iStart As Long, I%
Dim DD() As Byte
If iPage < 0 Then Exit Sub
ReDim DD(1)
If (iPage > iPageMax) Then
DD(0) = 255
DD(1) = 255
MSComm1.Output = DD()
Exit Sub
Else
iTemp = iPage \ 256 '先高位
DD(0) = iTemp
iTemp = iPage Mod 256
DD(1) = iTemp
MSComm1.Output = DD() '输出Page编号
Crc = DD(0) + DD(1) '考虑Page的效验
If Crc > 255 Then Crc = Crc - 256
End If
Sleep (5)
ReDim DD(iPageByte)
iStart = iPage * iPageByte
For I = 0 To iPageByte - 1
If (iStart + I) < lFile Then
iTemp = FlashBuf(iStart + I)
Else
iTemp = 255
End If
DD(I) = iTemp
Crc = Crc + iTemp
If Crc > 255 Then Crc = Crc - 256
Next I
DD(I) = Crc
MSComm1.Output = DD()
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -