sicpaconnexions / SICPA_Connexions / Model / CRC.cs @ 35f65dee
Historique | Voir | Annoter | Télécharger (1,908 ko)
1 | 03682d21 | ajournaux | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Text; |
||
4 | |||
5 | namespace SICPA_Connexions.Model |
||
6 | { |
||
7 | public class CRC |
||
8 | { |
||
9 | private List<byte> data = new List<byte>(); |
||
10 | public CRC() { } |
||
11 | public CRC(byte[] din) //trame entrée à la main |
||
12 | { |
||
13 | foreach (byte b in din) { data.Add(b); } |
||
14 | } |
||
15 | public void putBytes(byte[] din) //ajout trame depuis fonction |
||
16 | { |
||
17 | foreach (byte b in din) { data.Add(b); } |
||
18 | } |
||
19 | public byte getCRC() |
||
20 | { |
||
21 | byte thatCRC = 0, current_byte = 0, count, i; |
||
22 | for (i = 0; i < data.Count; i++) //calcul du CRC sur toute la trame excepté le CRC lui même et le ETX -> (frame.length-2) |
||
23 | { |
||
24 | current_byte = data[i]; |
||
25 | //Console.WriteLine("CRC byte calc : {0:X}", current_byte); |
||
26 | for (count = 0; count < 8; count++) |
||
27 | { |
||
28 | |||
29 | if (((thatCRC & 0x01) ^ (current_byte & 0x01)) != 0)//=1? |
||
30 | { |
||
31 | //XOR based to abr200 polygon (X^8 + X^4 + X^3 + X^2 + X^1) |
||
32 | thatCRC ^= 0x70; |
||
33 | thatCRC >>= 1; //bit shifting on the right |
||
34 | thatCRC |= 0x80; //set 1 to msb bit |
||
35 | } |
||
36 | else |
||
37 | { |
||
38 | thatCRC >>= 1; //bit shifting on the right again :) |
||
39 | thatCRC &= 0x7F; //set 0 to msb bit |
||
40 | } |
||
41 | //Console.WriteLine("current_crc : {0:X}", thatCRC); |
||
42 | //bit shifting on the current byte... |
||
43 | current_byte >>= 1; |
||
44 | } |
||
45 | } |
||
46 | //Console.WriteLine(data.Count); |
||
47 | /*for(i = 0; i < data.Count; i++) |
||
48 | { |
||
49 | data.Remove(i); |
||
50 | }*/ |
||
51 | return thatCRC; |
||
52 | } |
||
53 | } |
||
54 | } |