v12345vtm

pehacrc

May 30th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. Function crcberekenen(ByVal hexstring As String) ' hexstring = voorbeeld = "FE 00 06 00 40 01 01"
  2. 'automatisch tabel maken met de bytes
  3. Dim pehacommando = hexstring
  4. Dim tabel() = pehacommando.Split(" ")
  5. ' Dim testje = CByte(Convert.ToString(Convert.ToInt32(tabel(1), 16), 10))
  6. Dim integertabel(tabel.Length - 1) As Integer 'we maken een lege tabel met juist voldoende elementen als dat ons commando lang is
  7. ' bytestabel(0) = CByte(Convert.ToString(Convert.ToInt32(tabel(1), 16), 10))
  8. For i As Integer = 0 To tabel.Length - 1 Step 1
  9. integertabel(i) = Convert.ToInt32(tabel(i), 16)
  10. Next
  11. Dim tempcrc As Integer = &HFFFF 'notatie voor 0xFFFF
  12. Dim count = integertabel.Length
  13. Dim xx = 0
  14. For i As Integer = 0 To count - 1 Step 1 ' word per door te sturen byte doorlust
  15. tempcrc = tempcrc Xor integertabel(i)
  16. 'nu gaan we 8keer schuiven met bytes
  17. For j As Integer = 8 To 1 Step -1
  18. Dim som = tempcrc And &H1
  19. If (som = &H1) Then
  20. '$temp_crc = ($temp_crc >> 1) ^ 0x8408;
  21. tempcrc = tempcrc >> 1 'bug in vb.net als je deelt door 2 !! niet doen dus beter >>1 bit naar rechts opschuiven
  22. tempcrc = tempcrc Xor &H8408
  23. Else
  24. '$temp_crc >>= 1;//bitshift
  25. tempcrc = tempcrc / 2
  26. End If
  27. Next
  28. Next
  29. ' $temp_crc ^= 0xFFFF;// tempcrc xor met 0xFFFF
  30. tempcrc = tempcrc Xor &HFFFF ' 45053 dec = AFFD hec = crc: FD AF
  31.  
  32. Dim result = Convert.ToString(Convert.ToInt32(tempcrc, 10), 16).ToUpper 'berekeing KLAAR CRC karakters gekent , nu nog swappen
  33.  
  34. If result.Length = 3 Then
  35. result = "0" & result
  36. End If
  37.  
  38. Dim crcbyte1 = result(2) + result(3)
  39. Dim crcbyte2 = result(0) + result(1)
  40.  
  41. hexstring = hexstring & " " & crcbyte1 & " " & crcbyte2
  42.  
  43.  
  44.  
  45.  
  46. Return hexstring
  47. End Function
Add Comment
Please, Sign In to add comment