⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bear.frm

📁 VB写的上位机
💻 FRM
📖 第 1 页 / 共 2 页
字号:
                Disconnect = 0
                Busy = False
                Exit Sub
            End If
        ElseIf Opration = "Signature" Then                          '识别芯片型号
            SignatureCode(SigAddr) = AscB(Command)
            SigAddr = SigAddr + 1
            If SigAddr = 2 Then
                SetType
                Timer1.Enabled = False
                Com1.InputMode = comInputModeText
            Else:
                Com1.Output = Trim("S")
            End If
            Exit Sub
        ElseIf Opration = "SetMcuType" Then                         '手动设定芯片型号
            If Command = "T" Then
                TypeProcedue = TypeProcedue + 1
                If TypeProcedue = 1 Then
                    Sendbuffer(1) = SignatureCode(1)
                    Com1.Output = Sendbuffer
                ElseIf TypeProcedue = 2 Then
                    Status.Caption = "芯片已设定!"
                    Timer1.Enabled = False
                    TypeProcedue = 0
                    SetType
                End If
            Else
                Status.Caption = "芯片设定失败!"
                Timer1.Enabled = False
                Code_Max = 0
            End If
        End If
    Else                                                            '无回应则换端口
        Status.Caption = "正在连接,请稍候!"
        If Com1.CommPort = 1 Then
            Com1.PortOpen = False
            Com1.CommPort = 1
            Com1.PortOpen = True
        ElseIf Com1.CommPort = 2 Then
            Com1.PortOpen = False
            Com1.CommPort = 1
            Com1.PortOpen = True
        End If
        Com1.Output = Trim("C")
        Disconnect = Disconnect + 1
        Progress.Max = 50
        Progress.Value = Disconnect
        Status.Caption = "正在连接,请稍候!"
        If Disconnect = 50 Then
            Status.Caption = "设备连接失败!"
            Busy = False
            Disconnect = 0
            Progress.Value = 0
            Timer1.Enabled = False
        End If
    End If
    
End Sub


'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'写入代码,已经确定

Private Sub Write_Click()                                       '写入代码
    
    If Busy = True Then Exit Sub
    If Code_Max = 0 Then
        Status.Caption = "请先设定芯片型号!"
        Exit Sub
    End If
    If Code_all = 0 Then
        Status.Caption = "请先载入有效文件!"
        Exit Sub
    End If
    Busy = True
    Opration = "Write"
    Status.Caption = "正在写入,请稍候!"
    If Com1.PortOpen = False Then
        Com1.PortOpen = True
    End If
    Com1.Output = Trim("W")
    WriteTimer.Enabled = True
    
End Sub

Private Sub WriteTimer_Timer()
    
    If Com1.InBufferCount > 0 Then                                  '收到应答
        Disconnect = 0
        Codes = Com1.Input
        If Codes = "W" Then                                         '应答为就绪“Ready”
            WriteProcedue = WriteProcedue + 1
            If WriteProcedue = 1 Then
                Sendbuffer(1) = (Code_all + 1) \ 256                '最后一位代码的高位地址
                Com1.Output = Sendbuffer
            ElseIf WriteProcedue = 2 Then
                Sendbuffer(1) = (Code_all + 1) Mod 256              '最后一位代码的低位地址
                Com1.Output = Sendbuffer
                Addr = 0
                Progress.Max = Code_all + 1
                Progress.Value = 0
            Else
                If Addr <= Code_all Then                            '判断是否是最后一位代码
                    Sendbuffer(1) = Code(Addr)
                    Com1.Output = Sendbuffer
                    Progress.Value = Progress.Value + 1
                    Addr = Addr + 1
                    Exit Sub
                End If
                Status.Caption = "写入完成!"
                WriteProcedue = 0
                WriteTimer.Enabled = False
                Progress.Value = 0
                Busy = False                                        '写入完成
                Exit Sub
            End If
        End If
    Else                                                            '无应答
        Disconnect = Disconnect + 1
        If Disconnect < 1000 Then Exit Sub
        Status.Caption = "连接超时,写入失败!"                     '连接超时
        Opration = "Failed"
        Disconnect = 0
        WriteTimer.Enabled = False
        Busy = False
    End If
    
End Sub


'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'擦除代码,已经确定
Private Sub Eraze_Click()                                           '擦除
    
    If Busy = True Then Exit Sub
    If Code_Max = 0 Then
        Status.Caption = "请先设定芯片型号!"
        Exit Sub
    End If
    Busy = True
    Opration = "Eraze"
    Status.Caption = "正在擦除,请稍候!"
    If Com1.PortOpen = False Then
        Com1.PortOpen = True
    End If
    Com1.Output = Trim("E")                                         '发送擦除命令
    ErazeTimer.Enabled = True
    
End Sub

    
Private Sub WriteLockBits_Click()
    
    If Busy = True Then Exit Sub
    If Code_Max = 0 Then
        Status.Caption = "请先设定芯片型号!"
        Exit Sub
    End If
    Com1.Output = Trim("L")
    Busy = True
    Opration = "WriteLockBits"
    ErazeTimer.Enabled = True
    
End Sub

Private Sub ErazeTimer_Timer()

    If Com1.InBufferCount > 0 Then                                  '收到应答
        Disconnect = 0
        Codes = Com1.Input
        If Opration = "Eraze" Then
            If Codes = "E" Then
                Status.Caption = "擦除完成!"
            ElseIf Codes = "F" Then
                Status.Caption = "擦除失败!"
                Opration = "Failed"
            End If
        ElseIf Opration = "WriteLockBits" Then
            If Codes = "L" Then
                Status.Caption = "加密完成!"
            Else
                Status.Caption = "加密失败!"
                Opration = "Failed"
            End If
        End If
        ErazeTimer.Enabled = False
        Progress.Value = 0
        Busy = False                                            '擦除完成
        Exit Sub
    Else                                                            '无应答
        Disconnect = Disconnect + 1
        Progress.Max = 100
        Progress.Value = Disconnect
        If Disconnect < 100 Then Exit Sub
        Status.Caption = "连接超时!"
        Disconnect = 0
        Progress.Value = 0
        ErazeTimer.Enabled = False
        Busy = False
    End If

End Sub


'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'读出代码和校验,已经确定
Private Sub Read_Click(Index As Integer)
    
    If Busy = True Then Exit Sub
    
    If Code_Max = 0 Then
        Status.Caption = "请先设定芯片型号!"
        Exit Sub
    End If
    
    Com1.InputMode = comInputModeBinary
    If Index = 0 Then
        Opration = "Read"                                           '读代码
        Status.Caption = "正在读出,请稍候!"
        Progress.Max = Code_Max
    ElseIf Index = 1 Then
        If Code_all = 0 Then
            Status.Caption = "请先载入有效文件!"
            Exit Sub
        End If
        Opration = "Verify"                                         '校验
        Status.Caption = "正在校验,请稍候!"
        Progress.Max = Code_all
    Else
        Busy = False
        Exit Sub
    End If
    Addr = 0
    If Com1.PortOpen = False Then
        Com1.PortOpen = True
    End If
    Busy = True
    Com1.Output = Trim("R")
    Progress.Value = 0
    ReadTimer.Enabled = True
    
End Sub


Private Sub ReadTimer_Timer()
    
    If Com1.InBufferCount > 0 Then                                  '收到应答
        Disconnect = 0
        Codes = Com1.Input
        If Opration = "Verify" Then                                 '当前操作为校验
            If AscB(Codes) = Code(Addr) Then
                If Addr < Code_all Then
                    Com1.Output = Trim("R")
                    Addr = Addr + 1
                    Progress.Value = Progress.Value + 1
                    Exit Sub
                End If
                Com1.Output = Trim("O")
                Status.Caption = "校验正确!"
            Else
                Com1.Output = Trim("O")                             '校验错误,中途退出读出状态
                Status.Caption = "校验错误!"
                Opration = "Failed"
            End If
        ElseIf Opration = "Read" Then                               '当前操作为读出
            Code(Addr) = AscB(Codes)
            If Addr < Code_Max - 1 Then
                Com1.Output = Trim("R")
                Addr = Addr + 1
                Progress.Value = Progress.Value + 1
                Exit Sub
            End If
            Com1.Output = Trim("C")
            Status.Caption = "读出完成!"
            Code_all = Addr
            SaveCode
        End If
    Else                                                            '无应答
        Disconnect = Disconnect + 1
        If Disconnect < 100 Then Exit Sub
        Status.Caption = "连接超时,操作失败!"                     '连接超时
        Disconnect = 0
    End If
    Com1.InputMode = comInputModeText
    ReadTimer.Enabled = False
    Progress.Value = 0
    Busy = False                                                    '读出或者校验完成
    
End Sub


'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'打开和保存 Intel Hex 文件,已经确定

Private Sub LoadFile_Click()                                    '载入文件
    
    If Busy = True Then Exit Sub
    On Error GoTo Failed
    Busy = True
    Cmd1.Filter = "Intel Hex Files|*.hex"
    Cmd1.ShowOpen
    If Cmd1.FileName = "" Then
        Busy = False
        Exit Sub
    End If
    Open Cmd1.FileName For Input As #1
    
    Status.Caption = "正在载入文件"
    i = 0
    Progress.Value = 0
    Progress.Max = 100
    Code_all = 0
    
    Do Until EOF(1)
        i = i + 1
        If Progress.Value < 100 Then Progress.Value = i
        Codes = 0
        Do Until Codes = ":"
            Codes = Input(1, #1)                                '读入行起始位
        Loop
        Verify_Code = 0
        Code_Num = Val("&H" + Input(2, #1))
        Verify_Code = Verify_Code + Code_Num
        If Code_Num = 0 Then Exit Do                            '本行代码数
        AddrH = Val("&H" + Input(2, #1))
        AddrL = Val("&H" + Input(2, #1))
        Address(i) = AddrH * 256 + AddrL                        '起始地址
        Verify_Code = Verify_Code + AddrH + AddrL
        Codes = Input(2, #1)                                    '去掉“00”
        For a = Address(i) To Address(i) + Code_Num - 1
            Code(a) = Val("&H" + Input(2, #1))
            Verify_Code = Verify_Code + Code(a)                 '有效代码
        Next a
        If Code_all < a - 1 Then Code_all = a - 1
        Codes = Val("&H" + Input(2, #1))
        Verify_Code = Verify_Code + Codes
        If Verify_Code Mod 256 > 0 Then GoTo Failed             '校验位错误
    Loop
    i = 0
    Close #1
    Status.Caption = CStr(Code_all + 1) + " Bytes" '"文件载入成功!" +
    Progress.Value = 0
    Busy = False
    Exit Sub
    
Failed:
    Close #1
    Status.Caption = "格式非法或超出容量!"
    Progress.Value = 0
    Busy = False

End Sub

Private Sub SaveCode()                                              '保存代码为 HEX 文件
    
    Busy = True
    Cmd1.Filter = "Intel Hex Files|*.hex"
    Cmd1.ShowSave
    If Cmd1.FileName = "" Then
        Busy = False
        Exit Sub
    End If
    Open Cmd1.FileName For Output As #1
    For i = 0 To Code_all \ 16
        For j = 0 To 15
            Codes = CInt(Code(16 * i + j))
            If Codes < 255 Then Exit For
            If j = 15 Then GoTo NextLine                            '检查是否为空行(一行全部为 FF)
        Next j
        Print #1, ":10";                                            '行开始以及本行代码数
        Codes = CInt((i * 16) \ 256)
        If Codes < 16 Then
            Print #1, "0";
        End If
        Print #1, Hex((i * 16) \ 256);                              '高位地址
        Codes = CInt((i * 16) Mod 256)
        If Codes < 16 Then
            Print #1, "0";
        End If
        Print #1, Hex((i * 16) Mod 256);                            '低位地址
        Print #1, "00";                                             '有效代码
        Verify_Code = 16 + (i * 16) Mod 256 + (i * 16) \ 256        '校验位初始化
        For j = 0 To 15
            Codes = CInt(Code(16 * i + j))
            If Codes < 16 Then
                Print #1, "0";
            End If
            Print #1, Hex(Code(16 * i + j));                        '代码
            Verify_Code = Verify_Code + Code(16 * i + j)
        Next j
        Verify_Code = 256 - Verify_Code Mod 256
        If Verify_Code = 256 Then Verify_Code = 0
        If Verify_Code < 16 Then
            Print #1, "0";
        End If
        Print #1, Hex(Verify_Code)                                  '校验位计算
NextLine:
    Next i
    Print #1, ":00000001FF"                                         '文件结束
    Print #1, ";Created By Bear Programmer V1.0"
    Close #1
    Busy = False

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -