public void Calculate_WithEmptyInput_ReturnsInit() { var calcualtor = new Crc16(0x1021, 0x0000); var crc1 = calcualtor.Calculate(new byte[0]); var crc2 = calcualtor.Calculate(null); Assert.Equal <UInt16>(0, crc1); Assert.Equal <UInt16>(0, crc2); }
public uint GetCounter(int counterPage) { // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Data buffer to send over the network var data = new byte[30]; // How many bytes of data to send short dataCount = 0; // Set the "read memory and counter" command into the data array data[dataCount++] = 0xA5; // Calculate the position of the last byte in the page var lastByte = (counterPage << 5) + 31; // Copy the lower byte of the last byte into the data array data[dataCount++] = (byte)(lastByte & 0xFF); // Copy the upper byte of the last byte into the data array data[dataCount++] = (byte)(lastByte >> 8); // Add byte for the data byate, counter, zero bits, and CRC16 result for (var i = 0; i < 11; i++) { data[dataCount++] = 0xFF; } // Send the block of data to the device Adapter.SendBlock(data, dataCount); // Calculate the CRC based on the data var crcResult = Crc16.Calculate(data, 0, 11); // Assemble the CRC provided by the device var matchCrc = data[13] << 8; matchCrc |= data[12]; matchCrc ^= 0xFFFF; // Make sure the CRC values match if (crcResult != matchCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } uint counter = 0; // Assemble the counter data from the bytes retrieved for (var i = dataCount - 7; i >= dataCount - 10; i--) { counter <<= 8; counter |= data[i]; } return(counter); }
public void Calculate_Works() { var calcualtor = new Crc16(0x1021, 0xFFFF); var data = new byte[] { 0xC2 }; var crc = calcualtor.Calculate(data); Assert.Equal <UInt16>(0x18FE, crc); }
public override string ToString() { string strFormat = string.Format("{0}/{1}/{2}", this.Created.Ticks / 0xc92a69c000, this.UserId, this.FileId); if (this.HistoryInfoId > 0) { strFormat += string.Format("/{0}", this.HistoryInfoId.ToString()); } byte[] buffer = System.Text.Encoding.ASCII.GetBytes(strFormat); int crc16 = Crc16.Calculate(buffer, 0, buffer.Length); strFormat += "/" + crc16.ToString("D5"); return(Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(strFormat))); }
public void WriteDevice(byte[] state) { // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Data buffer to send over the network var data = new byte[30]; // How many bytes of data to send short dataCount = 0; // Set the commmand to execute data[dataCount++] = WriteStatusCommand; // Set the address data[dataCount++] = 0x07; data[dataCount++] = 0x00; // Add the state data[dataCount++] = state[1]; // Add bytes for the CRC result data[dataCount++] = 0xFF; data[dataCount++] = 0xFF; // Send the data Adapter.SendBlock(data, dataCount); // Calculate the CRC var crcResult = Crc16.Calculate(data, 0, 3); // Assemble the CRC provided by the device var matchCrc = data[5] << 8; matchCrc |= data[4]; matchCrc ^= 0xFFFF; // Make sure the CRC values match if (crcResult != matchCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } }
public ushort Calculate(byte[] bytes) { _validManager.ValidByteArray(bytes); return(_crc16.Calculate(bytes)); }
public void Initialize() { const int startAddress = 0x8; // Starting data page address const int endAddress = 0x11; // Ending data page address // Setup the control page for (var index = 0; index < 8; index += 2) { _control[index] = (byte)Resolution.EightBits; _control[index + 1] = (byte)Range.Range512; } // Clear the alarm page for (var index = 8; index < 16; index++) { _control[index] = 0; } // Data buffer to send over the network var data = new byte[30]; // How many bytes of data to send short dataCount = 0; // Set the command into the data array data[dataCount++] = 0x55; // Set the starting address of the data to write data[dataCount++] = startAddress & 0xFF; data[dataCount++] = (startAddress >> 8) & 0xFF; // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Write to the data pages specified for (var index = startAddress; index <= endAddress; index++) { // Copy the control data into our output buffer data[dataCount++] = _control[index - startAddress]; // Add two bytes for the CRC results data[dataCount++] = 0xFF; data[dataCount++] = 0xFF; // Add a byte for the control byte echo data[dataCount++] = 0xFF; // Send the block Adapter.SendBlock(data, dataCount); // If the check byte doesn't match then throw an exception if (data[dataCount - 1] != _control[index - startAddress]) { // Throw an exception throw new OneWireException(OneWireException.ExceptionFunction.SendBlock, Id); } int calculatedCrc; // CRC we calculated from sent data int sentCrc; // CRC retrieved from the device // Calculate the CRC values if (index == startAddress) { // Calculate the CRC16 of the data sent calculatedCrc = Crc16.Calculate(data, 0, 3); // Reconstruct the CRC sent by the device sentCrc = data[dataCount - 2] << 8; sentCrc |= data[dataCount - 3]; sentCrc ^= 0xFFFF; } else { // Calculate the CRC16 of the data sent calculatedCrc = Crc16.Calculate(_control[index - startAddress], index); // Reconstruct the CRC sent by the device sentCrc = data[dataCount - 2] << 8; sentCrc |= data[dataCount - 3]; sentCrc ^= 0xFFFF; } // If the CRC doesn't match then throw an exception if (calculatedCrc != sentCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Reset the byte count dataCount = 0; } }
public double[] GetVoltages() { // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Data buffer to send over the network var data = new byte[30]; // How many bytes of data to send short dataCount = 0; // Set the convert command into the transmit buffer data[dataCount++] = 0x3C; // Set the input mask to get all channels data[dataCount++] = 0x0F; // Set the read-out control to leave things as they are data[dataCount++] = 0x00; // Add two bytes for the CRC results data[dataCount++] = 0xFF; data[dataCount++] = 0xFF; // Send the data block Adapter.SendBlock(data, dataCount); // Calculate the CRC based on the transmit buffer var calculatedCrc = Crc16.Calculate(data, 0, 2); // Reconstruct the CRC sent by the device var sentCrc = data[4] << 8; sentCrc |= data[3]; sentCrc ^= 0xFFFF; // If the CRC doesn't match then throw an exception if (calculatedCrc != sentCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Setup for for power delivery after the next byte Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte); var transmitByte = (short)((dataCount - 1) & 0x1F); try { // Send the byte and start strong pullup Adapter.SendByte(transmitByte); } catch { // Stop the strong pullup Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate); // Re-throw the exception throw; } // Sleep while the data is transfered System.Threading.Thread.Sleep(6); // Stop the strong pullup Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate); // Read data to see if the conversion is over Adapter.ReadByte(); // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Reinitialize the data count dataCount = 0; // Set the read command into the transmit buffer data[dataCount++] = 0xAA; // Set the address to get the conversion results data[dataCount++] = 0x00; data[dataCount++] = 0x00; // Add 10 bytes to be read - 8 for the data and 2 for the CRC for (var index = 0; index < 10; index++) { data[dataCount++] = 0xFF; } // Send the block to the device Adapter.SendBlock(data, dataCount); // Calculate the CRC of the transmitted data calculatedCrc = Crc16.Calculate(data, 0, 10); // Reconstruct the CRC sent by the device sentCrc = data[dataCount - 1] << 8; sentCrc |= data[dataCount - 2]; sentCrc ^= 0xFFFF; // If the CRC doesn't match then throw an exception if (calculatedCrc != sentCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Voltage values to return var voltages = new double[4]; // Convert the data into a double for (var index = 3; index < 11; index += 2) { // Reconstruct the two bytes into the 16-bit values var iVoltageReadout = ((data[index + 1] << 8) | data[index]) & 0x0000FFFF; // Figure out the percentage of the top voltage is present voltages[(index - 3) / 2] = iVoltageReadout / 65535.0; // Apply the percentage to the maximum voltage range voltages[(index - 3) / 2] *= ((_control[(index - 3) + 1] & 0x01) == 0x01 ? 5.12 : 2.56); } return(voltages); }
public byte[] ReadDevice() { // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Data buffer to send over the network var data = new byte[30]; // How many bytes of data to send short dataCount = 0; // Set the commmand to execute data[dataCount++] = ChannelAccessCommand; // Set the data data[dataCount++] = 0x55; data[dataCount++] = 0xFF; // Read the info, dummy data and CRC16 for (var i = 3; i < 7; i++) { data[dataCount++] = 0xFF; } // Send the data Adapter.SendBlock(data, dataCount); // Calculate the CRC var crcResult = Crc16.Calculate(data, 0, 4); // Assemble the CRC provided by the device var matchCrc = data[6] << 8; matchCrc |= data[5]; matchCrc ^= 0xFFFF; // Make sure the CRC values match if (crcResult != matchCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } var state = new byte[2]; // Store the state data state[0] = data[3]; // Reset the data count dataCount = 0; // Set the command data[dataCount++] = ReadStatusCommand; // Set the address to read data[dataCount++] = 7; data[dataCount++] = 0; // Add data for the CRC for (var i = 3; i < 6; i++) { data[dataCount++] = 0xFF; } // Select and access the ID of the device we want to talk to Adapter.Select(Id); // Send the data Adapter.SendBlock(data, dataCount); // Calculate the CRC crcResult = Crc16.Calculate(data, 0, 3); // Assemble the CRC provided by the device matchCrc = data[5] << 8; matchCrc |= data[4]; matchCrc ^= 0xFFFF; // Make sure the CRC values match if (crcResult != matchCrc) { // Throw a CRC exception throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Store the state data state[1] = data[3]; return(state); }