I've taken the codes in the opening post, dumped some dummy data in there and get the same result for both Arduino and C#.
So not sure what the problem is.
Arduino
byte bytes1ToSend[] = {0x00, 0x01, 0x00, 0x01};
void setup()
{
Serial.begin(57600);
byte crc = CRC8(sizeof(bytes1ToSend));
if (crc < 0x10)
Serial.print("0");
Serial.println(crc, HEX);
}
void loop()
{
// put your main code here, to run repeatedly:
}
byte CRC8(byte len)
{
// This function calculate and return CRC8 of data bytes
// CRC-8 - based on the CRC8 formulas by Dallas/Maxim
// http://www.maxim-ic.com/appnotes.cfm/appnote_number/27
byte crc = 0x00;
for (byte i = 0; i < len; i++)
{
byte extract = bytes1ToSend[i]++;
for (byte n = 8; n > 1; n--)
{
byte sum = (byte)((crc ^ extract) & 0x01);
crc >>= 1;
if ((int)sum == 1)
{
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
C#
using System;
namespace CRC8_Maxim
{
class Program
{
static byte[] bytesToSend = new byte[] { 0x00, 0x01, 0x00, 0x01 };
static void Main(string[] args)
{
Console.WriteLine("{0:X2}", CRC8((byte)bytesToSend.Length));
Console.ReadLine();
}
static byte CRC8(byte len)
{
// This function calculate and return CRC8 of data bytes
// CRC-8 - based on the CRC8 formulas by Dallas/Maxim
// http://www.maxim-ic.com/appnotes.cfm/appnote_number/27
byte crc = 0x00;
for (byte i = 0; i < len; i++)
{
byte extract = bytesToSend[i]++;
for (byte n = 8; n > 1; n--)
{
byte sum = (byte)((crc ^ extract) & 0x01);
crc >>= 1;
if ((int)sum == 1)
{
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
}
}