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

📄 module1.bas

📁 This is Modbus RTU Protocl that i have written by Visual Basic to control Inverter N100 (Hyndai-Kor
💻 BAS
字号:
Attribute VB_Name = "Module1"
'luu tru form setting

Public portsettingMulti, portsettingInver
Public saveCom
Public saveParityMulti, saveParityInver
Public saveBps, saveStop, saveFlow, saveData


Public multiAdd As Integer
Public inverAdd As Integer

Const CRCHIP = &HA0              ' 1010 0000 the upper half of the poly
Const CRCLOP = 1                ' 0000 0001 the lower half of the poly

Public strTXBUFF As String

Dim CRCHI                 'The CRC Hi result
Dim CRCLO                 'The CRC Lo result
Dim CRCTP                 'Temp
 

Public Sub CRCTX(ByVal strSend As String)
Dim mi As Integer
Dim ni As Integer
Dim items As Integer

    CRCHI = 255
    CRCLO = 255
    items = Len(strSend)
    
    For mi = 1 To items
        CRCLO = CRCLO Xor Asc(Mid(strSend, mi, 1))
        For ni = 1 To 8
                CRCTP = CRCLO
                CRCLO = CRCLO \ 2
                If (CRCHI And 1) Then
                        CRCLO = CRCLO Or &H80
                End If
                CRCHI = CRCHI \ 2
            If (CRCTP And 1) Then
                CRCLO = CRCLO Xor CRCLOP
                CRCHI = CRCHI Xor CRCHIP
            End If
        Next ni
    Next mi
    
    strTXBUFF = strSend & Chr$(CRCLO) & Chr$(CRCHI)

End Sub

Public Function IEEEtoSingle(hexIEEE As String) As Single
    If Len(hexIEEE) <> 4 Then Err.Raise Err.Number
    Dim bits As String
    Dim signBit As Integer
    Dim exponent As Integer
    Dim fraction As Double
    
    For j = 4 To 1 Step -1
        For i = 0 To 7
            bits = bits & LTrim(Str(bit(Mid(hexIEEE, j, 1), 7 - i)))
        Next
    Next
    
    signBit = Mid(bits, 1, 1)
    exponent = Bin2Single(Mid(bits, 2, 8))
    fraction = Bin2Single("1." & Mid(bits, 10, 23))
    
    If signBit = 1 Then fraction = -fraction
    IEEEtoSingle = fraction * 2 ^ (exponent - 127)
End Function

Private Function bit(strValue As String, nIndex As Integer) As Integer
    bit = -((2 ^ nIndex And Asc(strValue)) <> 0)
End Function

Private Function Bin2Single(ByVal strValue As String) As Single
    Dim i As Integer, newSingle As Single
    Dim dotPos As Integer, curExp As Integer
    
    If InStr(1, strValue, ".") = 0 Then strValue = strValue & "."
    dotPos = InStr(1, strValue, ".")
        
    newLong = 0
    For i = 1 To Len(strValue)
        If Mid(strValue, i, 1) = "1" Then
            If i > dotPos Then curExp = dotPos - i Else curExp = dotPos - i - 1
            newSingle = newSingle + 2 ^ curExp
        End If
    Next
    Bin2Single = newSingle
End Function

⌨️ 快捷键说明

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