Python Forum
Please help me to convert a function from vb to python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me to convert a function from vb to python
#1
Hello everyone! So far I have always programmed with visual basic, but I happened to have to convert my code into python. I would need if somebody can convert this function from vb to python (2.7):




Sub Modbus_CRC(strInput As String) As String 
    Dim RetCRC As String
    Dim RetStr As String
   
    Dim CRCREG As Int
    Dim MVAL As Int
   
    Dim R As Short
    Dim I As Short
    Dim A As Int
    Dim B As Int
   
    CRCREG = 0xFFFF

    For R = 0 To strInput.Length - 1 Step 2
        RetStr = strInput.SubString2(R, R + 2)
        MVAL = Bit.ParseInt(RetStr, 16)
       
        CRCREG = Bit.Xor(CRCREG, MVAL)
        CRCREG = Bit.And(CRCREG, 0xFFFF)

        For I = 1 To 8 Step 1
            If Bit.And(CRCREG, 0x01) = 1 Then
                CRCREG = Bit.Xor(Bit.UnsignedShiftRight(CRCREG, 1), 0xA001)
                CRCREG = Bit.And(CRCREG, 0xFFFF)
            Else
                CRCREG = Bit.UnsignedShiftRight(CRCREG, 1)
                CRCREG = Bit.And(CRCREG, 0xFFFF)
            End If
        Next
    Next

    A = Bit.And(CRCREG, 0xFF)
    B = Bit.And(CRCREG, 0xFF00)
    A = A * 256
    B = B / 256

    If (A + B) < 16 Then
        RetCRC = "000" & (A + B)
    Else If (A + B) < 256 Then
        RetCRC = "00" & Bit.ToHexString(A + B)
    Else If (A + B) < 4096 Then
        RetCRC = "0" & Bit.ToHexString(A + B)
    Else
        RetCRC = Bit.ToHexString(A + B)
    End If
   
    Return RetCRC.ToUpperCase
End Sub
If someone can do it, thanks in advance!!!
Reply


Messages In This Thread
Please help me to convert a function from vb to python - by Bokka - May-18-2017, 02:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert a string to a function mikepy 8 2,676 May-13-2022, 07:28 PM
Last Post: mikepy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020