May-18-2017, 02:07 PM
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 SubIf someone can do it, thanks in advance!!!