Answer the question
In order to leave comments, you need to log in
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};
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};
mass3
, sets the values to the instrument. The documentation doesn't say anything about it. 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 };
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};
Answer the question
In order to leave comments, you need to log in
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)};
}
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 questionAsk a Question
731 491 924 answers to any question