非常复杂。文件格式也未必与你用的相同。 标准模块1Option ExplicitPublic Type ObjectByte Tag As Byte Length As Byte Value As Byte End TypePublic Type ObjectInteger Tag As Byte Length As Byte Value As Integer End TypePublic Type ObjectLong Tag As Byte Length As Byte Value As Long End TypePublic Type ObjectSingle Tag As Byte Length As Byte Value As Single End TypePublic Type ObjectString Tag As Byte Length() As Byte Value As String End TypePublic Type ObjectNull Tag As Byte Length As Byte End TypePublic Type typTraceFileHeader NT As ObjectLong 'Number of Traces NS As ObjectLong 'Number of Samples SC As ObjectByte 'Sample Coding: single type, 4 bytes per sample DS As ObjectInteger 'Data Space length TS As ObjectByte 'Title Space per sample GT As ObjectString 'Globe Title DC As ObjectString 'Description XO As ObjectLong 'Offset in X-axis for trace representation XL As ObjectString 'X-axis Lable YL As ObjectString 'Y-axis Lable XS As ObjectSingle 'X-axis Scale YS As ObjectSingle 'Y-axis Scale TO As ObjectLong 'Trace offset for displaying trace numbers LS As ObjectByte 'Logarithmic scale TB As ObjectNull 'Trace Block er End TypePublic Enum ObjectTag tagNT = &H41 tagNS = &H42 tagSC = &H43 tagDS = &H44 tagTS = &H45 tagGT = &H46 tagDC = &H47 tagXO = &H48 tagXL = &H49 tagYL = &H4A tagXS = &H4B tagYS = &H4C tagTO = &H4D tagLS = &H4E tagTB = &H5F End EnumPublic Type typTraceBlock TraceTitle As String CryptData() As Byte Samples() As Single End TypePublic Function Open_Trace_File_for_Read(ByVal strFileName As String) As Integer Dim f As Integer On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
If Dir(strFileName) = "" Then Exit Function
f = FreeFile() Open strFileName For Binary As #f
Open_Trace_File_for_Read = f Exit Function
errexit: End FunctionPublic Function Open_Trace_File_for_Write(ByVal strFileName As String) As Integer Dim f As Integer On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
' Backup the old file If Dir(strFileName) > "" Then If Dir(strFileName & ".bak") > "" Then Kill strFileName & ".bak" FileCopy strFileName, strFileName & ".bak" End If
f = FreeFile() Open strFileName For Binary As #f
Open_Trace_File_for_Write = f Exit Function
errexit: End FunctionPublic Function Create_Trace_File(ByVal strFileName As String) As Integer Dim f As Integer On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
If Dir(strFileName) > "" Then 'Backup the old file If Dir(strFileName & ".bak") > "" Then Kill strFileName & ".bak" Name strFileName As strFileName & ".bak" End If
f = FreeFile() Open strFileName For Binary As #f
Create_Trace_File = f Exit Function
errexit: End Function
标准模块1 续1'read & write .trs filePublic Function Read_Trace_File_Header(ByVal inputFile As Integer, FileHeader As typTraceFileHeader) As Boolean Dim bytTag As Byte, bytLen As Byte, lngLengthBytes As Long, lngLen As Long Dim bytBuffer() As Byte, bytLenTmp() As Byte On Error GoTo errexit With FileHeader
' Read Object Tag Get #inputFile, 1, bytTag If EOF(inputFile) Then GoTo errexit
Do Until EOF(inputFile)
' Read Object length Get #inputFile, , bytLen If EOF(inputFile) Then GoTo errexit
If bytLen And &H80 Then lngLengthBytes = bytLen And &H7F ReDim bytBuffer(lngLengthBytes - 1) Get #inputFile, , bytBuffer If EOF(inputFile) Then GoTo errexit
Case ObjectTag.tagXS '= &H4B If bytLen <> 4 Then GoTo errexit .XS.Tag = bytTag .XS.Length = 4 Get #inputFile, , .XS.Value
Case ObjectTag.tagYS '= &H4C If bytLen <> 4 Then GoTo errexit .YS.Tag = bytTag .YS.Length = 4 Get #inputFile, , .YS.Value
Case ObjectTag.tagTO '= &H4D If bytLen <> 4 Then GoTo errexit .TO.Tag = bytTag .TO.Length = 4 Get #inputFile, , .TO.Value
Case ObjectTag.tagLS '= &H4E If lngLen <> 1 Then GoTo errexit .LS.Tag = bytTag .LS.Length = 1 Get #inputFile, , .LS.Value
Case ObjectTag.tagTB '= &H5F If lngLen <> 0 Then GoTo errexit .TB.Tag = bytTag .TB.Length = 0 Exit Do Case Else 'Unknown Object Tag ReDim bytBuffer(lngLen - 1) Get #inputFile, , bytBuffer
End Select
Get #inputFile, , bytTag 'If bytTag = tagTB Then Get #inputFile, , bytLen Loop End With
If EOF(inputFile) Then GoTo errexit
Read_Trace_File_Header = True Exit Function
errexit: End Function
标准模块1 续2 Public Function Read_Trace_File_Data(ByVal inputFile As Integer, ByRef TraceData As typTraceBlock) As Boolean Dim n As Long, bytBuffer() As Byte On Error GoTo errexit If inputFile Then Get #inputFile, , TraceData.TraceTitle Get #inputFile, , TraceData.CryptData
n = (UBound(TraceData.Samples) + 1) * 4
ReDim bytBuffer(n - 1)
Get #inputFile, , bytBuffer
CopyMemory TraceData.Samples(0), bytBuffer(0), n End If Read_Trace_File_Data = True Exit Function
errexit:End FunctionPublic Function Write_Trace_File_Header(ByVal outputFile As Integer, ByRef FileHeader As typTraceFileHeader) As Boolean On Error GoTo errexit With FileHeader
If Not ((.NT.Tag > 0) And (.NS.Tag > 0) And (.SC.Tag > 0) And (.TB.Tag > 0)) Then GoTo errexit
Put #outputFile, , .NT
Put #outputFile, , .NS
Put #outputFile, , .SC
If .DS.Tag Then Put #outputFile, , .DS
If .TS.Tag Then Put #outputFile, , .TS
If .GT.Tag Then Put #outputFile, , .GT.Tag Put #outputFile, , .GT.Length Put #outputFile, , .GT.Value End If
If .DC.Tag Then Put #outputFile, , .DC.Tag Put #outputFile, , .DC.Length Put #outputFile, , .DC.Value End If
If .XO.Tag Then Put #outputFile, , .XO
If .XL.Tag Then Put #outputFile, , .XL.Tag Put #outputFile, , .XL.Length Put #outputFile, , .XL.Value End If
If .YL.Tag Then Put #outputFile, , .YL.Tag Put #outputFile, , .YL.Length Put #outputFile, , .YL.Value End If
If .XS.Tag Then Put #outputFile, , .XS
If .YS.Tag Then Put #outputFile, , .YS
If .TO.Tag Then Put #outputFile, , .TO
If .LS.Tag Then Put #outputFile, , .LS
Put #outputFile, , .TB
End With
Write_Trace_File_Header = True
Exit Function
errexit: End FunctionPublic Function Browse_Trace_File_for_End_Header(ByVal traceFile As Integer) As Long Dim bytTmp As Byte, bytBuffer() As Byte, lngLen As Long, n As Long On Error GoTo errexit Seek #traceFile, 1
' get tag field Get #traceFile, , bytTmp
Do Until (bytTmp = tagTB) Or EOF(traceFile) ' get length field Get #traceFile, , bytTmp
If bytTmp And &H80 Then 'multi-byte length field n = bytTmp And &H7F ReDim bytBuffer(n - 1) Get #traceFile, , bytBuffer
lngLen = 0 CopyMemory lngLen, bytBuffer(0), n Else lngLen = bytTmp End If
' get value field ReDim bytBuffer(lngLen - 1) Get #traceFile, , bytBuffer
Get #traceFile, , bytTmp Loop
If EOF(traceFile) Then Exit Function
' get length field of TB object Get #traceFile, , bytTmp
Browse_Trace_File_for_End_Header = Seek(traceFile) errexit: End FunctionPublic Function Write_Trace_Block(ByVal outputFile As Integer, ByRef traceBlock As typTraceBlock) As Boolean On Error GoTo errexit If outputFile = 0 Then Exit Function If (traceBlock.TraceTitle = "") Then Exit Function If UBound(traceBlock.CryptData) < 0 Then Exit Function 'if is not assigned couse a error If UBound(traceBlock.Samples) < 0 Then Exit Function 'if is not assigned couse a error
Put #outputFile, , traceBlock.TraceTitle Put #outputFile, , traceBlock.CryptData Put #outputFile, , traceBlock.Samples
Write_Trace_Block = True errexit: End Function Public Function Update_Trace_File_Number_of_Traces(ByVal outputFile As Integer, ByVal lngNumber As Long) As Boolean Dim bytTag As Byte, bytLen As Byte, bytBuffer() As Byte Dim lngTmp As Long, n As Long
On Error GoTo errexit
'outputFile = FreeFile() 'Open strFileName For Binary As #outputFile
Get #outputFile, 1, bytTag If EOF(outputFile) Then GoTo errexit
Do Until bytTag = ObjectTag.tagNT Get #outputFile, , bytLen If EOF(outputFile) Then GoTo errexit
Select Case bytTag Case tagGT, tagDC, tagXL, tagYL If bytLen And &H80 Then n = (bytLen And &H7F) ReDim bytBuffer(n - 1) Get #outputFile, , bytBuffer If EOF(outputFile) Then GoTo errexit
lngTmp = 0 CopyMemory lngTmp, bytBuffer(0), n ReDim bytBuffer(lngTmp - 1) Else ReDim bytBuffer(bytLen - 1) End If Case Else ReDim bytBuffer(bytLen - 1) End Select Get #outputFile, , bytBuffer If EOF(outputFile) Then GoTo errexit
Get #outputFile, , bytTag If EOF(outputFile) Then GoTo errexit Loop
Get #outputFile, , bytLen If EOF(outputFile) Then GoTo errexit
If bytLen <> 4 Then Exit Function
n = Seek(outputFile)
Get #outputFile, n, lngTmp lngNumber = lngNumber + lngTmp
Put #outputFile, n, lngNumber
Get #outputFile, n, lngTmp
If lngTmp = lngNumber Then Update_Trace_File_Number_of_Traces = True errexit: End Function
标准模块1Option ExplicitPublic Type ObjectByte
Tag As Byte
Length As Byte
Value As Byte
End TypePublic Type ObjectInteger
Tag As Byte
Length As Byte
Value As Integer
End TypePublic Type ObjectLong
Tag As Byte
Length As Byte
Value As Long
End TypePublic Type ObjectSingle
Tag As Byte
Length As Byte
Value As Single
End TypePublic Type ObjectString
Tag As Byte
Length() As Byte
Value As String
End TypePublic Type ObjectNull
Tag As Byte
Length As Byte
End TypePublic Type typTraceFileHeader
NT As ObjectLong 'Number of Traces
NS As ObjectLong 'Number of Samples
SC As ObjectByte 'Sample Coding: single type, 4 bytes per sample
DS As ObjectInteger 'Data Space length
TS As ObjectByte 'Title Space per sample
GT As ObjectString 'Globe Title
DC As ObjectString 'Description
XO As ObjectLong 'Offset in X-axis for trace representation
XL As ObjectString 'X-axis Lable
YL As ObjectString 'Y-axis Lable
XS As ObjectSingle 'X-axis Scale
YS As ObjectSingle 'Y-axis Scale
TO As ObjectLong 'Trace offset for displaying trace numbers
LS As ObjectByte 'Logarithmic scale
TB As ObjectNull 'Trace Block er
End TypePublic Enum ObjectTag
tagNT = &H41
tagNS = &H42
tagSC = &H43
tagDS = &H44
tagTS = &H45
tagGT = &H46
tagDC = &H47
tagXO = &H48
tagXL = &H49
tagYL = &H4A
tagXS = &H4B
tagYS = &H4C
tagTO = &H4D
tagLS = &H4E
tagTB = &H5F
End EnumPublic Type typTraceBlock
TraceTitle As String
CryptData() As Byte
Samples() As Single
End TypePublic Function Open_Trace_File_for_Read(ByVal strFileName As String) As Integer
Dim f As Integer
On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
If Dir(strFileName) = "" Then Exit Function
f = FreeFile()
Open strFileName For Binary As #f
Open_Trace_File_for_Read = f
Exit Function
errexit:
End FunctionPublic Function Open_Trace_File_for_Write(ByVal strFileName As String) As Integer
Dim f As Integer
On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
' Backup the old file
If Dir(strFileName) > "" Then
If Dir(strFileName & ".bak") > "" Then Kill strFileName & ".bak"
FileCopy strFileName, strFileName & ".bak"
End If
f = FreeFile()
Open strFileName For Binary As #f
Open_Trace_File_for_Write = f
Exit Function
errexit:
End FunctionPublic Function Create_Trace_File(ByVal strFileName As String) As Integer
Dim f As Integer
On Error GoTo errexit
If LCase(Right(strFileName, 4)) <> ".trs" Then Exit Function
If Dir(strFileName) > "" Then
'Backup the old file
If Dir(strFileName & ".bak") > "" Then Kill strFileName & ".bak"
Name strFileName As strFileName & ".bak"
End If
f = FreeFile()
Open strFileName For Binary As #f
Create_Trace_File = f
Exit Function
errexit:
End Function
Dim bytTag As Byte, bytLen As Byte, lngLengthBytes As Long, lngLen As Long
Dim bytBuffer() As Byte, bytLenTmp() As Byte On Error GoTo errexit With FileHeader
' Read Object Tag
Get #inputFile, 1, bytTag
If EOF(inputFile) Then GoTo errexit
Do Until EOF(inputFile)
' Read Object length
Get #inputFile, , bytLen
If EOF(inputFile) Then GoTo errexit
If bytLen And &H80 Then
lngLengthBytes = bytLen And &H7F
ReDim bytBuffer(lngLengthBytes - 1)
Get #inputFile, , bytBuffer
If EOF(inputFile) Then GoTo errexit
ReDim bytLenTmp(lngLengthBytes)
CopyMemory bytLenTmp(1), bytBuffer(0), lngLengthBytes
lngLen = 0
CopyMemory lngLen, bytBuffer(0), lngLengthBytes
Else
lngLengthBytes = 0
ReDim bytLenTmp(0)
lngLen = bytLen
End If
bytLenTmp(0) = bytLen Select Case bytTag
Case ObjectTag.tagNT '= &H41
If lngLen <> 4 Then GoTo errexit
.NT.Tag = bytTag
.NT.Length = 4
Get #inputFile, , .NT.Value
Case ObjectTag.tagNS '= &H42
If lngLen <> 4 Then GoTo errexit
.NS.Tag = bytTag
.NS.Length = 4
Get #inputFile, , .NS.Value
Case ObjectTag.tagSC '= &H43
If lngLen <> 1 Then GoTo errexit
.SC.Tag = bytTag
.SC.Length = 1
Get #inputFile, , .SC.Value '14h: float type, 4 bytes
Case ObjectTag.tagDS '= &H44
If lngLen <> 2 Then GoTo errexit
.DS.Tag = bytTag
.DS.Length = 2
Get #inputFile, , .DS.Value
Case ObjectTag.tagTS '= &H45
If lngLen <> 1 Then GoTo errexit
.TS.Tag = bytTag
.TS.Length = 1
Get #inputFile, , .TS.Value
Case ObjectTag.tagGT '= &H46
.GT.Tag = bytTag
ReDim .GT.Length(lngLengthBytes)
CopyMemory .GT.Length(0), bytLenTmp(0), lngLengthBytes + 1
.GT.Value = Space(lngLen)
Get #inputFile, , .GT.Value
Case ObjectTag.tagDC '= &H47
.DC.Tag = bytTag
ReDim .DC.Length(lngLengthBytes)
CopyMemory .DC.Length(0), bytLenTmp(0), lngLengthBytes + 1
.DC.Value = Space(lngLen)
Get #inputFile, , .DC.Value
Case ObjectTag.tagXO '= &H48
If bytTag <> 4 Then GoTo errexit
.XO.Tag = bytTag
.XO.Length = 4
Get #inputFile, , .XO.Value
Case tagXL '= &H49
.XL.Tag = bytTag
ReDim .XL.Length(lngLengthBytes)
CopyMemory .XL.Length(0), bytLenTmp(0), lngLengthBytes + 1
.XL.Value = Space(lngLen)
Get #inputFile, , .XL.Value
Case ObjectTag.tagYL '= &H4A
.YL.Tag = bytTag
ReDim .YL.Length(lngLengthBytes)
CopyMemory .YL.Length(0), bytLenTmp(0), lngLengthBytes + 1
.YL.Value = Space(lngLen)
Get #inputFile, , .YL.Value
Case ObjectTag.tagXS '= &H4B
If bytLen <> 4 Then GoTo errexit
.XS.Tag = bytTag
.XS.Length = 4
Get #inputFile, , .XS.Value
Case ObjectTag.tagYS '= &H4C
If bytLen <> 4 Then GoTo errexit
.YS.Tag = bytTag
.YS.Length = 4
Get #inputFile, , .YS.Value
Case ObjectTag.tagTO '= &H4D
If bytLen <> 4 Then GoTo errexit
.TO.Tag = bytTag
.TO.Length = 4
Get #inputFile, , .TO.Value
Case ObjectTag.tagLS '= &H4E
If lngLen <> 1 Then GoTo errexit
.LS.Tag = bytTag
.LS.Length = 1
Get #inputFile, , .LS.Value
Case ObjectTag.tagTB '= &H5F
If lngLen <> 0 Then GoTo errexit
.TB.Tag = bytTag
.TB.Length = 0
Exit Do Case Else 'Unknown Object Tag
ReDim bytBuffer(lngLen - 1)
Get #inputFile, , bytBuffer
End Select
Get #inputFile, , bytTag
'If bytTag = tagTB Then Get #inputFile, , bytLen
Loop
End With
If EOF(inputFile) Then GoTo errexit
Read_Trace_File_Header = True
Exit Function
errexit:
End Function
Public Function Read_Trace_File_Data(ByVal inputFile As Integer, ByRef TraceData As typTraceBlock) As Boolean
Dim n As Long, bytBuffer() As Byte
On Error GoTo errexit If inputFile Then
Get #inputFile, , TraceData.TraceTitle
Get #inputFile, , TraceData.CryptData
n = (UBound(TraceData.Samples) + 1) * 4
ReDim bytBuffer(n - 1)
Get #inputFile, , bytBuffer
CopyMemory TraceData.Samples(0), bytBuffer(0), n End If
Read_Trace_File_Data = True
Exit Function
errexit:End FunctionPublic Function Write_Trace_File_Header(ByVal outputFile As Integer, ByRef FileHeader As typTraceFileHeader) As Boolean On Error GoTo errexit
With FileHeader
If Not ((.NT.Tag > 0) And (.NS.Tag > 0) And (.SC.Tag > 0) And (.TB.Tag > 0)) Then GoTo errexit
Put #outputFile, , .NT
Put #outputFile, , .NS
Put #outputFile, , .SC
If .DS.Tag Then Put #outputFile, , .DS
If .TS.Tag Then Put #outputFile, , .TS
If .GT.Tag Then
Put #outputFile, , .GT.Tag
Put #outputFile, , .GT.Length
Put #outputFile, , .GT.Value
End If
If .DC.Tag Then
Put #outputFile, , .DC.Tag
Put #outputFile, , .DC.Length
Put #outputFile, , .DC.Value
End If
If .XO.Tag Then Put #outputFile, , .XO
If .XL.Tag Then
Put #outputFile, , .XL.Tag
Put #outputFile, , .XL.Length
Put #outputFile, , .XL.Value
End If
If .YL.Tag Then
Put #outputFile, , .YL.Tag
Put #outputFile, , .YL.Length
Put #outputFile, , .YL.Value
End If
If .XS.Tag Then Put #outputFile, , .XS
If .YS.Tag Then Put #outputFile, , .YS
If .TO.Tag Then Put #outputFile, , .TO
If .LS.Tag Then Put #outputFile, , .LS
Put #outputFile, , .TB
End With
Write_Trace_File_Header = True
Exit Function
errexit:
End FunctionPublic Function Browse_Trace_File_for_End_Header(ByVal traceFile As Integer) As Long
Dim bytTmp As Byte, bytBuffer() As Byte, lngLen As Long, n As Long On Error GoTo errexit
Seek #traceFile, 1
' get tag field
Get #traceFile, , bytTmp
Do Until (bytTmp = tagTB) Or EOF(traceFile)
' get length field
Get #traceFile, , bytTmp
If bytTmp And &H80 Then
'multi-byte length field
n = bytTmp And &H7F
ReDim bytBuffer(n - 1)
Get #traceFile, , bytBuffer
lngLen = 0
CopyMemory lngLen, bytBuffer(0), n
Else
lngLen = bytTmp
End If
' get value field
ReDim bytBuffer(lngLen - 1)
Get #traceFile, , bytBuffer
Get #traceFile, , bytTmp
Loop
If EOF(traceFile) Then Exit Function
' get length field of TB object
Get #traceFile, , bytTmp
Browse_Trace_File_for_End_Header = Seek(traceFile)
errexit:
End FunctionPublic Function Write_Trace_Block(ByVal outputFile As Integer, ByRef traceBlock As typTraceBlock) As Boolean On Error GoTo errexit
If outputFile = 0 Then Exit Function
If (traceBlock.TraceTitle = "") Then Exit Function
If UBound(traceBlock.CryptData) < 0 Then Exit Function 'if is not assigned couse a error
If UBound(traceBlock.Samples) < 0 Then Exit Function 'if is not assigned couse a error
Put #outputFile, , traceBlock.TraceTitle
Put #outputFile, , traceBlock.CryptData
Put #outputFile, , traceBlock.Samples
Write_Trace_Block = True
errexit:
End Function
Public Function Update_Trace_File_Number_of_Traces(ByVal outputFile As Integer, ByVal lngNumber As Long) As Boolean
Dim bytTag As Byte, bytLen As Byte, bytBuffer() As Byte
Dim lngTmp As Long, n As Long
On Error GoTo errexit
'outputFile = FreeFile()
'Open strFileName For Binary As #outputFile
Get #outputFile, 1, bytTag
If EOF(outputFile) Then GoTo errexit
Do Until bytTag = ObjectTag.tagNT
Get #outputFile, , bytLen
If EOF(outputFile) Then GoTo errexit
Select Case bytTag
Case tagGT, tagDC, tagXL, tagYL
If bytLen And &H80 Then
n = (bytLen And &H7F)
ReDim bytBuffer(n - 1)
Get #outputFile, , bytBuffer
If EOF(outputFile) Then GoTo errexit
lngTmp = 0
CopyMemory lngTmp, bytBuffer(0), n
ReDim bytBuffer(lngTmp - 1)
Else
ReDim bytBuffer(bytLen - 1)
End If
Case Else
ReDim bytBuffer(bytLen - 1)
End Select
Get #outputFile, , bytBuffer
If EOF(outputFile) Then GoTo errexit
Get #outputFile, , bytTag
If EOF(outputFile) Then GoTo errexit
Loop
Get #outputFile, , bytLen
If EOF(outputFile) Then GoTo errexit
If bytLen <> 4 Then Exit Function
n = Seek(outputFile)
Get #outputFile, n, lngTmp
lngNumber = lngNumber + lngTmp
Put #outputFile, n, lngNumber
Get #outputFile, n, lngTmp
If lngTmp = lngNumber Then Update_Trace_File_Number_of_Traces = True
errexit:
End Function
http://download.csdn.net/detail/SupermanKing/427319