public static ushort Crc16Ccitt(string str) { byte[] bytes = NRF24Helpers.GetBytesFromHexString(str); const ushort poly = 4129; ushort[] table = new ushort[256]; ushort initialValue = 0xffff; ushort temp, a; ushort crc = initialValue; for (int i = 0; i < table.Length; ++i) { temp = 0; a = (ushort)(i << 8); for (int j = 0; j < 8; ++j) { if (((temp ^ a) & 0x8000) != 0) { temp = (ushort)((temp << 1) ^ poly); } else { temp <<= 1; } a <<= 1; } table[i] = temp; } for (int i = 0; i < bytes.Length; ++i) { crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]); } return(crc); }
private void UploadConfiguration(SerialPort serialPort) { var configuration = new byte[15]; configuration[0] = 0x4E; //lenAndType configuration[1] = (byte)_generalSettings.RfChannel; configuration[2] = (byte)_generalSettings.DataRate; configuration[3] = (byte)_generalSettings.AddressLength; configuration[4] = 0x04; //addressPromiscLen var baseAddress = NRF24Helpers.GetBytesFromHexString(_generalSettings.BaseAddress); configuration[5] = baseAddress[7]; configuration[6] = baseAddress[6]; configuration[7] = baseAddress[5]; configuration[8] = baseAddress[4]; configuration[9] = baseAddress[3]; configuration[10] = baseAddress[2]; configuration[11] = baseAddress[1]; configuration[12] = baseAddress[0]; configuration[13] = (byte)_generalSettings.CrcLength; configuration[14] = (byte)_generalSettings.MaximumPayloadSize; serialPort.BaseStream.Write(configuration, 0, configuration.Length); }
public static string CalcCRC16(string strInput) { ushort crc = 0xFFFF; byte[] data = NRF24Helpers.GetBytesFromHexString(strInput); for (int i = 0; i < data.Length; i++) { crc ^= (ushort)(data[i] << 8); for (int j = 0; j < 8; j++) { if ((crc & 0x8000) > 0) { crc = (ushort)((crc << 1) ^ 0x1021); } else { crc <<= 1; } } } return(crc.ToString("X4")); }