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

📄 fileaccess.bas

📁 PIC24FJ32GA002单片机bootloader rs485通信移植
💻 BAS
📖 第 1 页 / 共 2 页
字号:
    
    
    Loop


    InFile.Close
    OutFile.Close
End Function



Public Function ImportP24HEXFile(InHEXFile As String) As Integer
    
    Dim LineAddrHigh As Long
    Dim LineDataCount As Byte
    Dim LineAddr As Long
    Dim LineCode As Byte
    
    Dim fileArray() As String
    Dim tempStr As String
    Dim fileIndex As Long
    Dim hexArray() As HEXDATA
    Dim RowData(255) As Byte
    Dim RowIndex As Integer
    Dim j As Integer
    Dim i As Integer
    Dim emptyRow As Boolean
    Dim OutLine As String
          
    Dim OverflowArray(16) As Byte
    Dim OverflowLen As Byte

    'Examine the HEX file for incompatability
    ImportP24HEXFile = ValidateHEXFile(InHEXFile)
    If ImportP24HEXFile < 0 Then
        MsgBox (ImportP24HEXFile)
        Exit Function
    End If
    
    'Create files for each memory type & open input hex file
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set InFile = fs.OpenTextFile(InHEXFile, 1, False, 0)
    Set PMOut = fs.CreateTextFile(VB.App.Path & "\" & PicBootS.ProgMemFile, True)
    Set EEOut = fs.CreateTextFile(VB.App.Path & "\" & PicBootS.EEDataFile, True)
    Set CFGOut = fs.CreateTextFile(VB.App.Path & "\" & PicBootS.ConfigFile, True)

    'Read in hex file & create array of file lines then close file
    tempStr = InFile.ReadAll()
    fileArray = Split(Left(tempStr, Len(tempStr) - 2), vbCrLf) 'remove last CrLf
    InFile.Close
    
    'Create array of hex data from input file strings
    ReDim hexArray(UBound(fileArray)) As HEXDATA
    j = 0
    For i = 0 To UBound(fileArray)
        DoEvents
            
        LineDataCount = Val("&H" & Mid(fileArray(i), 2, 2))
        LineAddr = Val("&H" & Mid(fileArray(i), 4, 4) & "&") And LOWER_WORD_MASK
        LineCode = Val("&H" & Mid(fileArray(i), 8, 2))
        LineData = Mid(fileArray(i), 10, (LineDataCount * 2))
        
        Select Case LineCode
            Case 0  'data
                hexArray(j).address = (LineAddrHigh * 65536) + (LineAddr And LOWER_WORD_MASK)
                hexArray(j).data = Mid(fileArray(i), 10, (LineDataCount * 2))
                hexArray(j).datLen = LineDataCount
                
                j = j + 1
            Case 1  'eof
                Exit For
            Case 4  'extended address
                LineAddrHigh = (Val("&H" & Mid(fileArray(i), 10, 4) & "&") And LOWER_WORD_MASK)
        End Select
    Next i
    
    'Resize hex data array to save memory
    ReDim Preserve hexArray(j - 1) As HEXDATA
    
    'Sort hex data array by address
    QuickSortHex hexArray, LBound(hexArray), UBound(hexArray)
    
    'Init data array with 0xFF (init phantom byte to 0)
    For j = 0 To 255
        If ((j + 1) Mod 4 = 0) Then
            RowData(j) = 0
        Else
            RowData(j) = 255
        End If
    Next j

    'Loop through hex data writing it to appropriate file
    i = 0
    RowStartAddr = hexArray(1).address And ROW_START_MASK
    emptyRow = True
    Do While i < UBound(hexArray) + 1
        DoEvents
          
        LineAddrHigh = (hexArray(i).address And UPPER_BYTE_MASK2) / 65536
        Select Case LineAddrHigh
            Case 0 To 253           ' regular program memory
                
                'Is all of current data in new row?
                If (hexArray(i).address - RowStartAddr) > (255) Then
                
                    'Next data is in new Row, so write old row to file if not empty
                    OutAddr = RowStartAddr
                    j = 0
                    If Not emptyRow Then
                        Do While j < 256
                            
                            'Built a formatted line of data
                            If j Mod 16 = 0 Then
                                OutLine = Dec2Hex(OutAddr, 6) & " "
                            End If
                            
                            OutLine = OutLine & Dec2Hex(RowData(j), 2) & " "
                            OutAddr = OutAddr + 1
                            j = j + 1
                                                        
                            If j Mod 16 = 0 And j <> 0 Then
                                PMOut.WriteLine (OutLine)
                            End If
                        Loop
                        
                        emptyRow = True
                    End If
                    
                    'start new row
                    RowStartAddr = hexArray(i).address And ROW_START_MASK
                    For j = 0 To 255
                        If ((j + 1) Mod 4 = 0) Then
                            RowData(j) = 0
                        Else
                            RowData(j) = 255
                        End If
                    Next j
                    
                    'initialize row with overflow of last line, if any
                    For j = 0 To OverflowLen - 1
                        RowData(j) = OverflowArray(j)
                            
                        'If non-erased data is present, row is not empty
                        If (j + 1) Mod 4 <> 0 Then
                            If OverflowArray(j) <> 255 Then
                                emptyRow = False
                            End If
                        End If
                    Next j
                    
                    OverflowLen = 0

                End If
                
                'Is a portion of current data in new row?
                If (hexArray(i).address + hexArray(i).datLen - RowStartAddr) > 255 Then

                    'Store data in temporary array to use in next row
                    OverflowLen = (LOWER_BYTE_MASK And hexArray(i).address) + hexArray(i).datLen - 256
                    hexArray(i).datLen = hexArray(i).datLen - OverflowLen
                    
                    For j = 0 To OverflowLen - 1
                        OverflowArray(j) = Val("&H" & Mid(hexArray(i).data, j * 2 + 1 + hexArray(i).datLen * 2, 2))
                    Next j
                    
                End If
                
                
                'get current data
                RowIndex = hexArray(i).address And LOWER_BYTE_MASK
                For j = 0 To hexArray(i).datLen - 1
                    RowData(RowIndex) = Val("&H" & Mid(hexArray(i).data, j * 2 + 1, 2))
                    RowIndex = RowIndex + 1
                    
                    'If non-erased data is present, row is not empty
                    If (j + 1) Mod 4 <> 0 Then
                        If Val("&H" & Mid(hexArray(i).data, j * 2 + 1, 2)) <> 255 Then
                            emptyRow = False
                        End If
                    End If
                Next j
                
               
            Case 254 To 255         ' EEDATA memory
                                            
                'Built a formatted line of data
                OutLine = Dec2Hex(hexArray(i).address, 6) & " "
                
                j = 1
                TmpStr = Mid(hexArray(i).data, j, 2)
                Do While TmpStr <> ""
                    OutLine = OutLine & TmpStr & " "
                    TmpStr = Mid(hexArray(i).data, j * 2 + 1, 2)
                
                    j = j + 1
                Loop
                
                EEOut.WriteLine (OutLine)
                    
                    
            Case 496                ' config memory
            
                'Built a formatted line of data
                OutLine = Dec2Hex(hexArray(i).address, 7) & " "
                
                j = 1
                TmpStr = Mid(hexArray(i).data, j, 2)
                Do While TmpStr <> ""
                    OutLine = OutLine & TmpStr & " "
                    TmpStr = Mid(hexArray(i).data, j * 2 + 1, 2)
                
                    j = j + 1
                Loop
                
                CFGOut.WriteLine (OutLine)
                
                   
        End Select
        
        i = i + 1
                 
    Loop
    
    'Write last row of PM if it isn't empty
    OutAddr = RowStartAddr
    j = 0
    If Not emptyRow Then
        Do While j < 256
            
            'Built a formatted line of data
            If j Mod 16 = 0 Then
                OutLine = Dec2Hex(OutAddr, 6) & " "
            End If
            
            OutLine = OutLine & Dec2Hex(RowData(j), 2) & " "
            OutAddr = OutAddr + 1
            j = j + 1
                                        
            If j Mod 16 = 0 And j <> 0 Then
                PMOut.WriteLine (OutLine)
            End If
        Loop
        
    End If
                    
    PMOut.Close
    EEOut.Close
    CFGOut.Close
    
End Function

Public Sub QuickSortHex(hexArray() As HEXDATA, inLow As Long, inHi As Long)
      
   Dim pivot   As String
   Dim tmpSwap As HEXDATA
   Dim tmpLow  As Long
   Dim tmpHi   As Long
    
   tmpLow = inLow
   tmpHi = inHi
   
   pivot = hexArray((inLow + inHi) \ 2).address
  
   While (tmpLow <= tmpHi)
      While (hexArray(tmpLow).address < pivot And tmpLow < inHi)
         tmpLow = tmpLow + 1
      Wend
      
      While (pivot < hexArray(tmpHi).address And tmpHi > inLow)
         tmpHi = tmpHi - 1
      Wend

      If (tmpLow <= tmpHi) Then
         tmpSwap = hexArray(tmpLow)
         hexArray(tmpLow) = hexArray(tmpHi)
         hexArray(tmpHi) = tmpSwap
         tmpLow = tmpLow + 1
         tmpHi = tmpHi - 1
      End If
   
   Wend
  
   If (inLow < tmpHi) Then QuickSortHex hexArray, inLow, tmpHi
   If (tmpLow < inHi) Then QuickSortHex hexArray, tmpLow, inHi
  
End Sub

⌨️ 快捷键说明

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