Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function crcberekenen(ByVal hexstring As String) ' hexstring = voorbeeld = "FE 00 06 00 40 01 01"
- 'automatisch tabel maken met de bytes
- Dim pehacommando = hexstring
- Dim tabel() = pehacommando.Split(" ")
- ' Dim testje = CByte(Convert.ToString(Convert.ToInt32(tabel(1), 16), 10))
- Dim integertabel(tabel.Length - 1) As Integer 'we maken een lege tabel met juist voldoende elementen als dat ons commando lang is
- ' bytestabel(0) = CByte(Convert.ToString(Convert.ToInt32(tabel(1), 16), 10))
- For i As Integer = 0 To tabel.Length - 1 Step 1
- integertabel(i) = Convert.ToInt32(tabel(i), 16)
- Next
- Dim tempcrc As Integer = &HFFFF 'notatie voor 0xFFFF
- Dim count = integertabel.Length
- Dim xx = 0
- For i As Integer = 0 To count - 1 Step 1 ' word per door te sturen byte doorlust
- tempcrc = tempcrc Xor integertabel(i)
- 'nu gaan we 8keer schuiven met bytes
- For j As Integer = 8 To 1 Step -1
- Dim som = tempcrc And &H1
- If (som = &H1) Then
- '$temp_crc = ($temp_crc >> 1) ^ 0x8408;
- tempcrc = tempcrc >> 1 'bug in vb.net als je deelt door 2 !! niet doen dus beter >>1 bit naar rechts opschuiven
- tempcrc = tempcrc Xor &H8408
- Else
- '$temp_crc >>= 1;//bitshift
- tempcrc = tempcrc / 2
- End If
- Next
- Next
- ' $temp_crc ^= 0xFFFF;// tempcrc xor met 0xFFFF
- tempcrc = tempcrc Xor &HFFFF ' 45053 dec = AFFD hec = crc: FD AF
- Dim result = Convert.ToString(Convert.ToInt32(tempcrc, 10), 16).ToUpper 'berekeing KLAAR CRC karakters gekent , nu nog swappen
- If result.Length = 3 Then
- result = "0" & result
- End If
- Dim crcbyte1 = result(2) + result(3)
- Dim crcbyte2 = result(0) + result(1)
- hexstring = hexstring & " " & crcbyte1 & " " & crcbyte2
- Return hexstring
- End Function
Add Comment
Please, Sign In to add comment