A
A
Albert2016-08-01 07:27:59
C++ / C#
Albert, 2016-08-01 07:27:59

C# checksum?

I ask for help in solving the issue that has already tortured me, with the calculation of the checksum of the package. I do not work as a programmer, but I help to solve some issues related to the work of the department.
In general, the problem is as follows, there is a device of domestic development
. I turned to them for the accompanying documentation, but my knowledge did not help to understand the issue of generating the checksum of the package, if at the receiving stage I can still somehow bypass this side, then in case of writing the parameters in The device is nowhere for me without it.
The program ended with one unresolved issue with the setting of the scanned frequency, unfortunately there is no place without it.
I listened to the program that comes with the device, and what I got:
There is an array of bytesmass1, the package contains information that is written to the device:

byte[] mass1 = new byte[]{75,0,25,0,3,88,2,0,126,37,0,188,138,169,53,66,15,52,115,203,112,103,220,16,92,237,76,80,254};

At the end of the packet, 2 bytes are added, which is the checksum of this packet mass2: The complete message written to the device should look like the full packet looks like :
byte[] mass2 = new byte[]{241,9};
mass3
byte[] mass3 = new byte[]{75,0,25,0,3,88,2,0,126,37,0,188,138,169,53,66,15,52,115,203,112,103,220,16,92,237,76,80,254,241,9};

The package above mass3, sets the values ​​to the instrument. The documentation doesn't say anything about it.
I also listened to the package that the device sends with the settings already in it, so to speak, identification mass4:
byte[] mass4 = new byte[] { 0x40, 0x00, 0x03, 0x58, 0x02, 0x00, 0x7E, 0x25, 0x00, 0xBC, 0x8A, 0xA9, 0x35, 0x42, 0x0F, 0x34, 0x73, 0xCB, 0x70, 0x67, 0xDC, 0x10, 0x5C, 0xED, 0x4C, 0x50, 0xFE, 0x00, 0x45, 0x29, 0x51, 0x09, 0x11, 0x03, 0x28, 0x30, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x16, 0x20, 0x50, 0x33, 0x34, 0x31, 0x2D, 0x34, 0x32, 0x36, 0x00, 0x32, 0x36, 0x2E, 0x31, 0x30, 0x2E, 0x30, 0x36, 0x2D, 0x31, 0x33, 0x00, 0x06, 0x0F };

It has a description in the documentation, the principle is the same, the last 2 bytes of the packet is a checksum, everything else is the information part.
Please help solve the issue with writing the checksum calculation code, in any form, the main thing is that the correct checksum be present in the returned value. I just don’t understand what I’m doing wrong anymore, CRC-16 was there, but I didn’t get the desired result.
If someone decides to help, to check the mass packages are similar to mass3, only with changed parameters for writing to the device, the last 2 bytes of the checksum:
byte[] mass = new byte[]{75,0,25,0,3,48,117,0,126,37,0,188,138,169,53,66,15,52,115,203,112,103,220,16,92,237,76,80,254,60,10};
byte[] mass = new byte[]{75,0,25,0,3,64,156,0,126,37,0,188,138,169,245,165,14,52,115,203,96,64,220,16,92,237,76,80,254,94,11};
byte[] mass = new byte[]{75,0,25,0,3,80,195,0,126,37,0,88,138,169,181,9,14,52,115,203,120,60,220,32,131,237,236,214,255,199,11};
byte[] mass = new byte[]{75,0,25,0,3,112,17,1,126,37,0,88,138,169,181,9,14,52,115,203,120,60,220,32,131,237,236,214,255,54,11};
byte[] mass = new byte[]{75,0,25,0,3,60,134,1,126,37,0,88,138,169,181,9,14,52,115,203,120,60,220,32,131,237,236,214,255,119,11};

Documentation sent to me by the manufacturer
e8824a0ebb8f464eba2b86085818d278.jpg
На картинке ниже формат пакета mass4, чтение установок прибора:
4d47b69db4734bd6886954de2e20caeb.jpg13ce8b5dba5843dfb95e9303d1b4a4a0.jpgf3ac11224e764c97aafef93c87f9f90c.jpg2ab86287cbcb46618b47c09be5f9afcf.jpg8e80d5b8cd2043dc8c6be5c0df4400fb.jpg7d6b98787e594ea583059a2abdc3bf74.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2016-08-01
@6ETuK

For the first set of bytes, this simple code is suitable:

static byte[] crc(byte[] data) {
    ushort sum = 0;
    foreach (byte b in data)
        sum += b;
    return new byte[] {(byte)(sum & 255), (byte)(sum >> 8)};
}

That is the sum of all bytes of the message including the opcode and message length, but without the checksum. That is, in the documentation, the checksum is considered from 0 to 4 + n without -1, n is the length of the data for the command. But it says there that the checksum is “the sum of the bytes of the packet”.
And here's the full test:
static ushort crc(byte[] data, ushort sum = 0) {
    foreach (byte b in data) sum += b;
    return sum;
}
static void Main(string[] args)
{
    // пакеты без чексумм
    byte[] mass1 = new byte[] { 75, 0, 25, 0, 3, 48, 117, 0, 126, 37, 0, 188, 138, 169, 53, 66, 15, 52, 115, 203, 112, 103, 220, 16, 92, 237, 76, 80, 254};
    byte[] mass2 = new byte[] { 75, 0, 25, 0, 3, 64, 156, 0, 126, 37, 0, 188, 138, 169, 245, 165, 14, 52, 115, 203, 96, 64, 220, 16, 92, 237, 76, 80, 254};
    byte[] mass3 = new byte[] { 75, 0, 25, 0, 3, 80, 195, 0, 126, 37, 0, 88, 138, 169, 181, 9, 14, 52, 115, 203, 120, 60, 220, 32, 131, 237, 236, 214, 255};
    byte[] mass4 = new byte[] { 75, 0, 25, 0, 3, 112, 17, 1, 126, 37, 0, 88, 138, 169, 181, 9, 14, 52, 115, 203, 120, 60, 220, 32, 131, 237, 236, 214, 255};
    byte[] mass5 = new byte[] { 75, 0, 25, 0, 3, 60, 134, 1, 126, 37, 0, 88, 138, 169, 181, 9, 14, 52, 115, 203, 120, 60, 220, 32, 131, 237, 236, 214, 255};

    foreach(var mass in new byte[][] {mass1, mass2, mass3, mass4, mass5}) {
        bool first = true;
        Console.Write("{");
        foreach (var b in mass)  {
            if (!first) Console.Write(", ");
            else first = false;

            Console.Write(b);
        }
        var sum = BitConverter.GetBytes(crc(mass));
        // вывод чексуммы
        Console.WriteLine(", {0}, {1}}}", sum[0], sum[1]);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question