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

📄 comtrade.bas

📁 对于符合国际标准COMTRADE格式的数据进行转换处理
💻 BAS
📖 第 1 页 / 共 5 页
字号:
                        Else
                            ' 秒
                            second1 = Val(Mid$(sLineString, iLastComPos + 1))
                            ' 微秒
                            usec1 = 0 ' 未给出us部分的时间
                        End If
                        
                        GoTo lbCheckAcqZeroTime ' 检查触发对应的绝对时刻的正确性
                    End If
                End If
            End If
        End If
    End If
        
    sErrorMessage = "触发对应的绝对时刻应满足mm/dd/yy, hh:mm:ss.ssssss格式;或配置文件格式不正确。"
    GoTo lbFileContentError ' 配置文件内容错误
        
lbCheckAcqZeroTime: ' 检查触发对应的绝对时刻的正确性
    If year1 < 80 Then
        If IsDate(Format(year1 + 2000, "0000") + "/" + Format(month1, "00") + "/" + Format(day1, "00") + " " + Format(hour1, "00") + ":" + Format(minute1, "00") + ":" + Format(second1, "00")) = True Then
            rComtradeFileHeadInfor.dAcqZeroTime = CDate(Format(year1 + 2000, "0000") + "/" + Format(month1, "00") + "/" + Format(day1, "00") + " " + Format(hour1, "00") + ":" + Format(minute1, "00") + ":" + Format(second1, "00")) ' 触发记录对应的绝对时刻(年/月/日 时:分:秒)
            rComtradeFileHeadInfor.lAcqZeroUsec = usec1 ' 触发记录对应的绝对时刻(us)
            
            GoTo lbFileType ' 读取数据文件类型
        End If
    Else
        If IsDate(Format(year1 + 1900, "0000") + "/" + Format(month1, "00") + "/" + Format(day1, "00") + " " + Format(hour1, "00") + ":" + Format(minute1, "00") + ":" + Format(second1, "00")) = True Then
            rComtradeFileHeadInfor.dAcqZeroTime = CDate(Format(year1 + 1900, "0000") + "/" + Format(month1, "00") + "/" + Format(day1, "00") + " " + Format(hour1, "00") + ":" + Format(minute1, "00") + ":" + Format(second1, "00")) ' 触发记录对应的绝对时刻(年/月/日 时:分:秒)
            rComtradeFileHeadInfor.lAcqZeroUsec = usec1 ' 触发记录对应的绝对时刻(us)
            
            GoTo lbFileType ' 读取数据文件类型
        End If
    End If
    
    sErrorMessage = "触发对应的绝对时刻时间内容错误,应满足mm/dd/yy, hh:mm:ss.ssssss格式;或配置文件格式不正确。"
    GoTo lbFileContentError ' 配置文件内容错误
    
lbFileType:
    ' 7. File Type:
    '      ft <CR,LF>
    '           ft = ASCII for ascii file format, and BINARY for binary file format
    iLineCount = iLineCount + 1 ' 文件行数
    Line Input #filehandle, sLineString
    rComtradeFileHeadInfor.sFileFormat = Trim$(sLineString) ' 文件格式:BINARY-二进制,ASCII-文本
    iChns = 0
    If InStr(rComtradeFileHeadInfor.sFileFormat, "BINARY") > 0 Then
        rComtradeFileHeadInfor.sFileFormat = "BINARY"
        iChns = iChns + 1
    End If
    If InStr(rComtradeFileHeadInfor.sFileFormat, "ASCII") > 0 Then
        rComtradeFileHeadInfor.sFileFormat = "ASCII"
        iChns = iChns + 1
    End If
    If iChns = 0 Or iChns = 2 Then
        sErrorMessage = "数据文件格式错误,必须为ASCII或BINARY之一;或配置文件格式不正确。"
        GoTo lbFileContentError ' 配置文件内容错误
    End If
    
    Close #filehandle ' 关闭配置文件
    Exit Sub
    
lbFileContentError: ' 配置文件内容错误
    If Trim$(sErrorMessage) <> "" Then MsgBox "第" + Trim$(Str$(iLineCount)) + "行:" + sErrorMessage, vbCritical + vbOKOnly, "配置文件错误"
    rComtradeFileHeadInfor.blFileValid = False ' 当前数据文件是否有效
    Close #filehandle ' 关闭配置文件
    Exit Sub
    
lbFileAccessError: ' 配置文件访问错误
    MsgBox "出错位置:第" + Trim$(Str$(iLineCount)) + "行;原因:" + Err.Description + "。", vbCritical + vbOKOnly, "配置文件错误"
    rComtradeFileHeadInfor.blFileValid = False ' 当前数据文件是否有效
    Close #filehandle ' 关闭配置文件
    Exit Sub
End Sub

' 从ASCII格式的数据文件中倒出数据: True-成功;False-失败
Function OutputAnalogFromAsciiDataFile(outfilehandle As Integer) As Boolean
    On Error GoTo lbAsciiAnalogFileError
    
    Dim infilehandle As Integer, sLineString As String
    Dim iComPos As Integer, iLastComPos As Integer
    Dim lLineCount As Long, sErrorMessage As String
    
    infilehandle = FreeFile
    Open rComtradeFileHeadInfor.sDatFileName For Input Access Read As #infilehandle
    
    ' 9. Example Ascii Date File
    '      0000000001, 0000000000, 002090, 001827, 002090, 002044,  0, 0
    '      ----------  ----------  -------------------------------  --------
    '       Sample     Time in us  Analog Data Values for Channels  Status
    '       Number     from begi-              1-4                  Infor
    '                  nning or                                     Channels
    '                  record                                       5 and 6
    
    Dim lPoints As Long, iAnalogChns As Integer, lEndPoints As Long
    
    ' 结束采样点
    lEndPoints = rComtradeAcqSegInfor(Val(frmOutputAnalog.txtEndAcqSeg.Text)).lEndPointOffset
    
    '----------
    ' 输出标题
    '----------
    Print #outfilehandle, ""
    Print #outfilehandle, "采样点序号" + Space(2) + "时间偏移(us)" + Space(2);
    For iAnalogChns = 1 To rComtradeFileHeadInfor.lTotalAnalogChs
        If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
            Print #outfilehandle, "AN" + Format(rComtradeAnalogInfor(iAnalogChns).iChannelID, "00000") + Space(7);  ' 通道号
        End If
    Next iAnalogChns
    Print #outfilehandle, "" ' 换行
    Print #outfilehandle, "----------  --------------  ";
    For iAnalogChns = 1 To rComtradeFileHeadInfor.lTotalAnalogChs
        If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
            Print #outfilehandle, "--------------  ";
        End If
    Next iAnalogChns
    Print #outfilehandle, "" ' 换行
    
    '-------------
    ' 输出采样数据
    '-------------
    lLineCount = 0  ' 行计数
    For lPoints = 1 To lEndPoints
        lLineCount = lLineCount + 1 ' 行计数
        Line Input #infilehandle, sLineString
    
        iComPos = InStr(sLineString, ",")
        If iComPos >= 1 Then ' 读取采样序号
            ' 读取采样序号
            Print #outfilehandle, FormatWithLeftSpace(Val(Mid$(sLineString, 1, iComPos - 1)), "0", 10) + Space(2);
            
            iLastComPos = iComPos
            iComPos = InStr(iLastComPos + 1, sLineString, ",")
            If iComPos >= 1 Then ' 读取采样点的时间偏移
                ' 读取采样点的时间偏移
                Print #outfilehandle, FormatWithLeftSpace(Val(Mid$(sLineString, iLastComPos + 1, iComPos - iLastComPos - 1)), "0.0", 14) + Space(2);
            
                ' 读取模拟量信息
                For iAnalogChns = 1 To rComtradeFileHeadInfor.lTotalAnalogChs
                    If iAnalogChns = rComtradeFileHeadInfor.lTotalAnalogChs Then
                        ' 防止没有开关量时出错
                        If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
                            ' 输出
                            Print #outfilehandle, FormatWithLeftSpace(rComtradeAnalogInfor(iAnalogChns).fOffset + rComtradeAnalogInfor(iAnalogChns).fCoefficient * Val(Mid$(sLineString, iComPos + 1)), "0.000", 12) + Space(2);
                        Else
                            ' 忽略
                        End If
                    Else
                        iLastComPos = iComPos
                        iComPos = InStr(iLastComPos + 1, sLineString, ",")
                
                        If iComPos >= 1 Then
                            If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
                                ' 输出
                                Print #outfilehandle, FormatWithLeftSpace(rComtradeAnalogInfor(iAnalogChns).fOffset + rComtradeAnalogInfor(iAnalogChns).fCoefficient * Val(Mid$(sLineString, iLastComPos + 1, iComPos - iLastComPos - 1)), "0.000", 12) + Space(2);
                            Else
                                ' 忽略
                            End If
                        Else
                            sErrorMessage = "数据文件信息不全或格式不正确"
                            GoTo lbAsciiAnalogInforError ' 文件信息不全/格式不正确
                        End If ' 读取模拟量信息:1个通道
                    End If
                Next iAnalogChns ' 读取模拟量信息
            
                GoTo lbNextAnalogPoints ' 读取下一采样点
            End If ' 读取采样点的时间偏移
            
            ' 忽略开关量信息
            ' nothing
            
        End If ' 读取采样序号
        
        sErrorMessage = "数据文件信息不全或格式不正确"
        GoTo lbAsciiAnalogInforError ' 文件信息不全/格式不正确
lbNextAnalogPoints:
        Print #outfilehandle, "" ' 换行
    Next lPoints
        
    Close #infilehandle
    OutputAnalogFromAsciiDataFile = True ' 成功
    Exit Function

lbAsciiAnalogInforError:
    Close #infilehandle
    MsgBox "第" + lLineCount + "行:" + sErrorMessage, vbCritical + vbOKOnly, "错误"
    OutputAnalogFromAsciiDataFile = False ' 失败
    Exit Function
    
lbAsciiAnalogFileError:
    Close #infilehandle
    MsgBox "文件" + Trim$(rComtradeFileHeadInfor.sDatFileName) + "访问失败;出错位置:第" + Trim$(Str(lLineCount)) + "行;原因:" + Err.Description + "。", vbCritical + vbOKOnly, "错误"
    OutputAnalogFromAsciiDataFile = False ' 失败
    Exit Function
End Function

' 从BINARY格式的数据文件中倒出数据: True-成功,False-失败
Function OutputAnalogFromBinaryDataFile(outfilehandle As Integer) As Boolean
    On Error GoTo lbBinaryAnalogFileError
    
    Dim infilehandle As Integer
    infilehandle = FreeFile
    Open rComtradeFileHeadInfor.sDatFileName For Binary Access Read As #infilehandle

    ' 10. BINARY format Data File:
    '      n tt A1A2......AkD1D2....Dm
    '              n = sample number , in four bytes
    '             tt = sample time in us, in four bytes
    '         A1..Ak = analog sample values in two bytes for each channel
    '         D1..Dm = status values of 16 digital inputs in each 16 bit word
    '
    '      Notes:
    '         (1) The least significant bit of a word is assigned to the smallest input
    '             channel number belonging to that group of 16 channels. Thus, bit 0 of
    '             D1 is the status of digital input number 1, while bit 1 of D2 is the
    '             status of digital input number 18.
    '         (2) In storing a word, the low byte of the word is stored first, then the
    '             high byte.
    '         (3) Missing data in a binary file is represented by FFFF.
    '
    Dim lPoints As Long, iAnalogChns As Integer, lEndPoints As Long
    Dim iTemp As Integer, lTemp As Long, lLineCount As Long, iSwitchWord As Integer
    
    ' 结束采样点
    lEndPoints = rComtradeAcqSegInfor(Val(frmOutputAnalog.txtEndAcqSeg.Text)).lEndPointOffset
    
    '----------
    ' 输出标题
    '----------
    Print #outfilehandle, ""
    Print #outfilehandle, "采样点序号" + Space(2) + "时间偏移(us)" + Space(2);
    For iAnalogChns = 1 To rComtradeFileHeadInfor.lTotalAnalogChs
        If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
            Print #outfilehandle, "AN" + Format(rComtradeAnalogInfor(iAnalogChns).iChannelID, "00000") + Space(7);  ' 通道号
        End If
    Next iAnalogChns
    Print #outfilehandle, "" ' 换行
    Print #outfilehandle, "----------  --------------  ";
    For iAnalogChns = 1 To rComtradeFileHeadInfor.lTotalAnalogChs
        If rComtradeAnalogInfor(iAnalogChns).blSelected Then ' 是否被选择输出:True-是
            Print #outfilehandle, "--------------  ";

⌨️ 快捷键说明

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