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); }
public static ushort TestCrc(string data, int test) { var combinedBitArray = NRF24Helpers.GetBitArrayFromHexString(data); ushort testCrc = 0xFFFF; for (var offsetFront = 0; offsetFront <= combinedBitArray.Length; offsetFront++) { for (var i = combinedBitArray.Length - offsetFront; i >= 0; i--) { //backwards stripping var subBitArray = NRF24Helpers.GetPartOfBitArray(combinedBitArray, offsetFront, i); testCrc = NRF24Helpers.GetCrc16(subBitArray); if (testCrc == 0xB4CC && test == 1) { var b = "a"; } if (testCrc == 0x52F7 && test == 2) { var b = "a"; } if (testCrc == 0x354E && test == 3) { var b = "a"; } } } return(testCrc); }
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 GetHexStringFromBitArray(BitArray bitArray) { if (bitArray.Length != 8) { return(string.Empty); } var b = NRF24Helpers.GetByteFromBitArray(bitArray, 0); return(b.ToString("X2")); }
private NRF24Data CreateNRF24Data(NRF24Structure nrf24Structure) { if (nrf24Structure.NRF24Header == null || nrf24Structure.Data.Length < 5) { return(null); } // Set the NRF24 data part var nrf24Data = new NRF24Data { NodeAddress = $"{nrf24Structure.NRF24Header.Address}{nrf24Structure.Data.Substring(0, 2)}" }; var controlFieldString = nrf24Structure.Data.Substring(2, 4); var controlFieldBitArray = NRF24Helpers.GetBitArrayFromHexString(controlFieldString); nrf24Data.PayloadLength = NRF24Helpers.GetPartOfBitArray(controlFieldBitArray, 0, 6); nrf24Data.Pid = NRF24Helpers.GetPartOfBitArray(controlFieldBitArray, 6, 2); nrf24Data.NoAck = controlFieldBitArray[9]; nrf24Data.Payload = nrf24Structure.Data.Substring(4, nrf24Structure.Data.Length - 4); var payloadFieldBitArray = NRF24Helpers.GetBitArrayFromHexString(nrf24Data.Payload); // For last byte only the MSbit has value; rest will be cleared payloadFieldBitArray = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, 0, payloadFieldBitArray.Length - 7); // Get the packet crc (the crc is located in last two bytes of the packet) var crcOffset = payloadFieldBitArray.Length - 16; nrf24Data.PacketCrc = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, crcOffset, 16); // Remove the crc bits from the bitarray before it is processed further nrf24Data.PayloadBitArray = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, 0, payloadFieldBitArray.Length - 16); //TODO what about the string payload?? // **** // Calculate the crc ourselfs // Get the payload in bits, as the bitArray is already stripped of all unneeded stuff, the length is the amount of bits //var payloadLengthBits = payloadFieldBitArray.Length; //var packetLengthBits = 40 + 9 + payloadLengthBits; // As the crc is calculated over the address, packet control field and payload, attach the two fields //var combinedString = $"{this.NRF24Header.Address}{this.Data}"; //var combinedString = $"{this.Data}"; // Create the NRF24Mysensor part nrf24Data.NRF24Mysensor = CreateNRF24Mysensor(nrf24Data); return(nrf24Data); }
public static byte[] GetBytesFromBitArray(BitArray bitArray) { if (bitArray.Length % 8 != 0) { return(new byte[0]); } var count = bitArray.Length / 8; var bytes = new byte[count]; for (var i = 0; i < count; i++) { bytes[i] = NRF24Helpers.GetByteFromBitArray(bitArray, i * 8); } return(bytes); }
public static ushort GetCrc16(BitArray bitArray) { ushort crc = 0xFFFF; ushort data; byte workingByte; int shift; ushort bitoffs = 0; workingByte = NRF24Helpers.GetByteFromBitArray(bitArray, bitoffs >> 3); while (bitoffs < bitArray.Length) { shift = bitoffs & 7; // Shift the active bit to the position of bit 15 data = (ushort)(workingByte << (8 + shift)); // Assure all other bits are 0 data &= 0x8000; crc ^= data; if ((crc & 0x8000) > 0) //if ((crc & 0x8000) != 0) //if ((crc & 0x8000) == 0) { crc = (ushort)((crc << 1) ^ 0x8408); // 0x1021 = (1) 0001 0000 0010 0001 = x^16+x^12+x^5+1 } else { crc = (ushort)(crc << 1); //crc <<= 1; } ++bitoffs; if (0 == (bitoffs & 7)) { // Get a new byte for the next 8 bits. workingByte = NRF24Helpers.GetByteFromBitArray(bitArray, bitoffs >> 3); } } return(crc); }
public static BitArray GetBitArrayFromHexString(string str) { // Get a list of hex values from the input string var strings = NRF24Helpers.GetSplittedParts(str, 2); // Create a new bitarray var bitArray = new BitArray(strings.Count * 8); var count = 0; foreach (var chunk in strings) { var byteValue = Convert.ToByte(chunk, 16); var array = new BitArray(new byte[] { byteValue }); for (var i = 7; i >= 0; i--) { bitArray[count++] = array[i]; } } return(bitArray); }
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")); }
private NRF24Mysensor CreateNRF24Mysensor(NRF24Data nrf24Data) { // Check if the bitArray contains enough information to be processed if (nrf24Data.PayloadBitArray.Length < 57) { return(null); } // We should start at 1 as the string is not byte aligned var bitOffset = 1; // Set the MySensors data part var nrf24Mysensor = new NRF24Mysensor(); nrf24Mysensor.Last = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset); bitOffset += 8; nrf24Mysensor.Sender = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset); bitOffset += 8; nrf24Mysensor.Destination = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset); bitOffset += 8; nrf24Mysensor.Length = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 5); bitOffset += 5; nrf24Mysensor.Version = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3); bitOffset += 3; nrf24Mysensor.DataType = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3); bitOffset += 3; nrf24Mysensor.IsAck = nrf24Data.PayloadBitArray[bitOffset]; bitOffset++; nrf24Mysensor.ReqAck = nrf24Data.PayloadBitArray[bitOffset]; bitOffset++; nrf24Mysensor.CommandType = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3); bitOffset += 3; nrf24Mysensor.Type = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset); bitOffset += 8; nrf24Mysensor.Sensor = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset); bitOffset += 8; nrf24Mysensor.PayloadBitArray = NRF24Helpers.GetPartOfBitArray( nrf24Data.PayloadBitArray, bitOffset, nrf24Data.PayloadBitArray.Length - bitOffset); // Find, is enabled, the VeraDevices corresponding to the package if (_generalSettings.LookupMysensorsNodeViaVera) { // Check if it is the gateway that sends the message if (nrf24Mysensor.Sender == 0) { nrf24Mysensor.SenderVeraDevice = _veraSettings.VeraDevices .SingleOrDefault(a_item => a_item.IsGateway); } else { nrf24Mysensor.SenderVeraDevice = _veraSettings.VeraDevices .SingleOrDefault( a_item => a_item.VeraDeviceAltID != null && a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Sender && a_item.VeraDeviceAltID.ChildID == 255); } // Check if it is the gateway that receives the message if (nrf24Mysensor.Destination == 0) { nrf24Mysensor.DestinationVeraDevice = _veraSettings.VeraDevices .SingleOrDefault(a_item => a_item.IsGateway); } else { nrf24Mysensor.DestinationVeraDevice = _veraSettings.VeraDevices .Where(a_item => a_item.VeraDeviceAltID != null) .SingleOrDefault(a_item => a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Destination && a_item.VeraDeviceAltID.ChildID == 255); } nrf24Mysensor.SensorVeraDevice = _veraSettings.VeraDevices .Where(a_item => a_item.VeraDeviceAltID != null) .SingleOrDefault( a_item => a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Sender && a_item.VeraDeviceAltID.ChildID == nrf24Mysensor.Sensor); } return(nrf24Mysensor); }