Hi Steve, i need help #16
joaoavila1987
started this conversation in
General
Replies: 2 comments
-
Hey João, doing well... the crc16 api specifies which algorithm you need to use as the first parameter since there are a couple of them it supports... i've thought about rewriting the sdk as this kind of seems confusing but... generally its exactly the same as the others: using NullFX.CRC;
using System;
using System.Text;
var someMessage = UTF8Encoding.UTF8.GetBytes("Some Serial Message");
var someNumber = BitConverter.GetBytes(0x123456);
Console.WriteLine ( $"X: { someMessage.ToHex ( ) }\tY: { someNumber.ToHex ( ) }\r\n" );
// info on different implementations at https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Implementations
// uses a x^16 + x^15 + x^2 + 1 polynomial
var messageStardardCrc = Crc16.ComputeChecksum ( Crc16Algorithm.Standard, someMessage );
var someNumberStandardCrc = Crc16.ComputeChecksum(Crc16Algorithm.Standard, someNumber);
Console.WriteLine ( $"A: 0x{messageStardardCrc:X2},\tB: 0x{someNumberStandardCrc:X2}" );
// uses a x^16 + x^12 + x^5 + 1 polynomial with an initial value of 0 for the CRC
var messageCcittCrc = Crc16.ComputeChecksum(Crc16Algorithm.Ccitt, someMessage);
var someNumberCcittCrc = Crc16.ComputeChecksum(Crc16Algorithm.Ccitt, someNumber);
Console.WriteLine ( $"A: 0x{messageCcittCrc:X2},\tB: 0x{someNumberCcittCrc:X2}" );
// uses a x^16 + x^12 + x^5 + 1 polynomial but uses an initial value of 0x1D0F for the CRC
var messageCcitt_0x1D0F_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittInitialValue0x1D0F, someMessage);
var someNumberCcitt_0x1D0F_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittInitialValue0x1D0F, someNumber);
Console.WriteLine ( $"A: 0x{messageCcitt_0x1D0F_Crc:X2},\tB: 0x{someNumberCcitt_0x1D0F_Crc:X2}" );
// uses a x^16 + x^12 + x^5 + 1 polynomial but uses an initial value of 0xFFFF for the CRC
var messageCcitt_0xFFFF_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittInitialValue0xFFFF, someMessage);
var someNumberCcitt_0xFFFF_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittInitialValue0xFFFF, someNumber);
Console.WriteLine ( $"A: 0x{messageCcitt_0xFFFF_Crc:X2},\tB: 0x{someNumberCcitt_0xFFFF_Crc:X2}" );
// uses a x^16 + x^12 + x^5 + 1 reversed polynomial and initial value of 0 for the CRC
var messageCcitt_Kermit_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittKermit, someMessage);
var someNumberCcitt_Kermit_Crc = Crc16.ComputeChecksum(Crc16Algorithm.CcittKermit, someNumber);
Console.WriteLine ( $"A: 0x{messageCcitt_Kermit_Crc:X2},\tB: 0x{someNumberCcitt_Kermit_Crc:X2}" );
// uses a x^16 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^5 + x^2 + 1 polynomial and initial value of 0 for the CRC
var messageDnpCrc = Crc16.ComputeChecksum(Crc16Algorithm.Dnp, someMessage);
var someNumberDnpCrc = Crc16.ComputeChecksum(Crc16Algorithm.Dnp, someNumber);
Console.WriteLine ( $"A: 0x{messageDnpCrc:X2},\tB: 0x{someNumberDnpCrc:X2}" );
Console.ReadLine ( );
public static class ByteExtensions {
public static string ToHex ( this byte[] payload ) {
var sb = new StringBuilder();
if ( payload?.Length > 0 ) {
foreach ( var bite in payload ) {
sb.AppendFormat ( "{0:X2}", bite );
}
}
return sb.ToString ( );
}
} output would look something similar to: X: 536F6D652053657269616C204D657373616765 Y: 56341200
A: 0x66E4, B: 0xE65C
A: 0x3336, B: 0x2ED6
A: 0x04EF, B: 0x20C6
A: 0x8057, B: 0xAA16
A: 0x21C9, B: 0x62DD
A: 0x818B, B: 0x6BDE |
Beta Was this translation helpful? Give feedback.
0 replies
-
thank you so much |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi Steve, how are you?
see you put examples in the documentation to get crc32, could you give me examples of what it would look like to get crc16 ?
I loved your work
João Ávila
Beta Was this translation helpful? Give feedback.
All reactions