Rev 457 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 457 | Rev 1397 | ||
|---|---|---|---|
| 1 | using System; |
1 | using System; |
| 2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
| 3 | using System.Text; |
3 | using System.Text; |
| 4 | 4 | ||
| 5 | public class CanPacket { |
5 | public class CanPacket { |
| 6 | private byte pktclass = 0; |
6 | private byte pktclass = 0; |
| 7 | // private byte type = 0; |
7 | // private byte type = 0; |
| 8 | // private byte sid = 0; |
8 | // private byte sid = 0; |
| 9 | // private byte rid = 0; |
9 | // private byte rid = 0; |
| 10 | private byte data_length = 0; |
10 | private byte data_length = 0; |
| 11 | private uint id = 0; |
11 | private uint id = 0; |
| 12 | private byte[] data = new byte[8]; |
12 | private byte[] data = new byte[8]; |
| 13 | private byte ext = 1; |
13 | private byte ext = 1; |
| 14 | private byte rtr = 0; |
14 | private byte rtr = 0; |
| 15 | 15 | ||
| 16 | public CanPacket() { |
16 | public CanPacket() { |
| 17 | 17 | ||
| 18 | } |
18 | } |
| 19 | 19 | ||
| 20 | public CanPacket(byte[] raw,uint startIndex) { |
20 | public CanPacket(byte[] raw,uint startIndex) { |
| 21 | // 17 bytes, a packet. |
21 | // 17 bytes, a packet. |
| 22 | // UART_START_BYTE id[0] id[1] id[2] id[3] extended remote_request data_length d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] UART_END_BYTE |
22 | // UART_START_BYTE id[0] id[1] id[2] id[3] extended remote_request data_length d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] UART_END_BYTE |
| 23 | /* |
23 | /* |
| 24 | * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR |
24 | * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR |
| 25 | * |
25 | * |
| 26 | * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000 NMT, ... |
26 | * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000 NMT, ... |
| 27 | * <TYPE> = 00000000 11111111 00000000 00000000 = 0x00FF0000 NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...] |
27 | * <TYPE> = 00000000 11111111 00000000 00000000 = 0x00FF0000 NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...] |
| 28 | * <SID> = 00000000 00000000 11111111 00000000 = 0x0000FF00 Sender ID |
28 | * <SID> = 00000000 00000000 11111111 00000000 = 0x0000FF00 Sender ID |
| 29 | * <RID> = 00000000 00000000 00000000 11111111 = 0x000000FF Receiver ID |
29 | * <RID> = 00000000 00000000 00000000 11111111 = 0x000000FF Receiver ID |
| 30 | * |
30 | * |
| 31 | */ |
31 | */ |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | uint addr = (((uint)raw[startIndex + 3]) << 24) + (((uint)raw[startIndex + 2]) << 16) + (((uint)raw[startIndex + 1]) << 8) + (((uint)raw[startIndex + 0])); |
34 | uint addr = (((uint)raw[startIndex + 3]) << 24) + (((uint)raw[startIndex + 2]) << 16) + (((uint)raw[startIndex + 1]) << 8) + (((uint)raw[startIndex + 0])); |
| 35 | 35 | ||
| 36 | this.id = addr; |
36 | this.id = addr; |
| 37 | this.pktclass = (byte)((addr & 0x1E000000) >> 25); |
37 | this.pktclass = (byte)((addr & 0x1E000000) >> 25); |
| 38 | // this.type = (byte)((addr & 0x00FF0000) >> 16); |
38 | // this.type = (byte)((addr & 0x00FF0000) >> 16); |
| 39 | // this.sid = (byte)((addr & 0x0000FF00) >> 8); |
39 | // this.sid = (byte)((addr & 0x0000FF00) >> 8); |
| 40 | // this.rid = (byte)((addr & 0x000000FF)); |
40 | // this.rid = (byte)((addr & 0x000000FF)); |
| 41 | this.data_length = raw[startIndex + 6]; |
41 | this.data_length = raw[startIndex + 6]; |
| 42 | 42 | ||
| 43 | ext = (byte)(0x0f & raw[startIndex + 4]); |
43 | ext = (byte)(0x0f & raw[startIndex + 4]); |
| 44 | rtr = (byte)(0x0f & raw[startIndex + 5]); |
44 | rtr = (byte)(0x0f & raw[startIndex + 5]); |
| 45 | 45 | ||
| 46 | for (int i = 0; i < 8; i++) this.data[i] = raw[startIndex + 7 + i]; |
46 | for (int i = 0; i < 8; i++) this.data[i] = raw[startIndex + 7 + i]; |
| 47 | } |
47 | } |
| 48 | 48 | ||
| 49 | public CanPacket(string raw) { |
49 | public CanPacket(string raw) { |
| 50 | //denna funktion fungerar sådär, se till att bygga en bättre parser |
50 | //denna funktion fungerar sådär, se till att bygga en bättre parser |
| 51 | 51 | ||
| 52 | //1e00007f 1 0 04 03 02 01 96 3f 76 15 |
52 | //1e00007f 1 0 04 03 02 01 96 3f 76 15 |
| 53 | string [] split = null; |
53 | string [] split = null; |
| 54 | split |
54 | //split = raw.Split( new Char [] {' '} ); |
| - | 55 | split = raw.Split( ' ' ); |
|
| 55 | if ((split.Length >= 3) && (split.Length <= 11)) { |
56 | if ((split.Length >= 3) && (split.Length <= 11)) { |
| 56 | try { |
57 | try { |
| 57 | id = System.Convert.ToUInt32(split[0].Trim(), 16); |
58 | id = System.Convert.ToUInt32(split[0].Trim(), 16); |
| 58 | } |
59 | } |
| 59 | catch { |
60 | catch { |
| 60 | Console.WriteLine("overflow 1"); |
61 | Console.WriteLine("\noverflow 1 <"+split[0].Trim()+">\n"); |
| 61 | } |
62 | } |
| 62 | try { |
63 | try { |
| 63 | ext = (byte)(System.Convert.ToUInt32(split[1].Trim(), 16)&0xFF); |
64 | ext = (byte)(System.Convert.ToUInt32(split[1].Trim(), 16)&0xFF); |
| 64 | } |
65 | } |
| 65 | catch { |
66 | catch { |
| 66 | Console.WriteLine("overflow 2"); |
67 | Console.WriteLine("\noverflow 2 <"+split[1].Trim()+">\n"); |
| 67 | } |
68 | } |
| 68 | try { |
69 | try { |
| 69 | rtr = (byte)(System.Convert.ToUInt32(split[2].Trim(), 16)&0xFF); |
70 | rtr = (byte)(System.Convert.ToUInt32(split[2].Trim(), 16)&0xFF); |
| 70 | } |
71 | } |
| 71 | catch { |
72 | catch { |
| 72 | Console.WriteLine("overflow 3"); |
73 | Console.WriteLine("\noverflow 3 <"+split[2].Trim()+">\n"); |
| 73 | } |
74 | } |
| 74 | try { |
- | |
| 75 | //testa ext och rtr! |
75 | //testa ext och rtr! |
| - | 76 | string dummy=""; |
|
| - | 77 | try { |
|
| 76 | 78 | ||
| 77 | for (int i = 0; i < split.Length-3; i++) { |
79 | for (int i = 0; i < split.Length-3; i++) { |
| 78 |
|
80 | dummy = split[i+3].Trim(); |
| 79 | if (dummy.Length == 0) break; |
81 | if (dummy.Length == 0) break; |
| 80 | data_length = (byte)((i+1)&0xFF); |
82 | data_length = (byte)((i+1)&0xFF); |
| 81 | //Console.WriteLine("i: " + i + " i+3: " + (i+3) + " split[i+3]: " +split[i+3]); |
83 | //Console.WriteLine("i: " + i + " i+3: " + (i+3) + " split[i+3]: " +split[i+3]); |
| 82 | data[i] = (byte)(System.Convert.ToUInt32(dummy, 16)&0xFF); |
84 | data[i] = (byte)(System.Convert.ToUInt32(dummy, 16)&0xFF); |
| 83 | } |
85 | } |
| 84 | } |
86 | } |
| 85 | catch { |
87 | catch { |
| 86 | Console.WriteLine("overflow 4"); |
88 | Console.WriteLine("\noverflow 4 <"+dummy+">\n"); |
| 87 | } |
89 | } |
| 88 | } |
90 | } |
| 89 | 91 | ||
| 90 | } |
92 | } |
| 91 | 93 | ||
| 92 | public override int GetHashCode() { |
94 | public override int GetHashCode() { |
| 93 | return base.GetHashCode(); |
95 | return base.GetHashCode(); |
| 94 | } |
96 | } |
| 95 | 97 | ||
| 96 | public override bool Equals(Object obj) { |
98 | public override bool Equals(Object obj) { |
| 97 | CanPacket cpm = (CanPacket)obj; |
99 | CanPacket cpm = (CanPacket)obj; |
| 98 | 100 | ||
| 99 | byte[] bytes = cpm.getData(); |
101 | byte[] bytes = cpm.getData(); |
| 100 | 102 | ||
| 101 | for(int i=0;i<8;i++) if (this.data[i]!=bytes[i]) return false; |
103 | for(int i=0;i<8;i++) if (this.data[i]!=bytes[i]) return false; |
| 102 | 104 | ||
| 103 | return (this.id==cpm.getId() && this.data_length==cpm.getDataLength()); |
105 | return (this.id==cpm.getId() && this.data_length==cpm.getDataLength()); |
| 104 | 106 | ||
| 105 | } |
107 | } |
| 106 | 108 | ||
| 107 | public uint getId() { return this.id; } |
109 | public uint getId() { return this.id; } |
| 108 | public void setId(uint id) { this.id = id; } |
110 | public void setId(uint id) { this.id = id; } |
| 109 | public byte getPktClass(){ return this.pktclass; } |
111 | public byte getPktClass(){ return this.pktclass; } |
| 110 | public void setDataLength(byte datalength){ this.data_length=datalength; } |
112 | public void setDataLength(byte datalength){ this.data_length=datalength; } |
| 111 | public byte getDataLength(){ return this.data_length; } |
113 | public byte getDataLength(){ return this.data_length; } |
| 112 | public byte[] getData(){ return this.data; } |
114 | public byte[] getData(){ return this.data; } |
| 113 | public void setExt(byte ext) { this.ext = ext; } |
115 | public void setExt(byte ext) { this.ext = ext; } |
| 114 | 116 | ||
| 115 | public void setData(byte[] data){ this.data = data; } |
117 | public void setData(byte[] data){ this.data = data; } |
| 116 | 118 | ||
| 117 | // public override string ToString() { |
119 | // public override string ToString() { |
| 118 | // string str = ""; |
120 | // string str = ""; |
| 119 | 121 | ||
| 120 | // str = "pktclass: "+pktclass.ToString()+", type: "+type.ToString()+", sid: "+sid.ToString()+", rid: "+rid.ToString()+",data_length: "+data_length.ToString()+", data: "; |
122 | // str = "pktclass: "+pktclass.ToString()+", type: "+type.ToString()+", sid: "+sid.ToString()+", rid: "+rid.ToString()+",data_length: "+data_length.ToString()+", data: "; |
| 121 | // for (int i = 0; i < 8; i++) str+=data[i].ToString()+" "; |
123 | // for (int i = 0; i < 8; i++) str+=data[i].ToString()+" "; |
| 122 | 124 | ||
| 123 | // return str; |
125 | // return str; |
| 124 | // } |
126 | // } |
| 125 | 127 | ||
| 126 | public string toRawString() { |
128 | public string toRawString() { |
| 127 | 129 | ||
| 128 | string returnstring = "PKT " + String.Format("{0:x8}", id); |
130 | string returnstring = "PKT " + String.Format("{0:x8}", id); |
| 129 | returnstring = returnstring + " " + String.Format("{0:x1}", ext) + " " + String.Format("{0:x1}", rtr); |
131 | returnstring = returnstring + " " + String.Format("{0:x1}", ext) + " " + String.Format("{0:x1}", rtr); |
| 130 | for (int i = 0; i < data_length; i++) { |
132 | for (int i = 0; i < data_length; i++) { |
| 131 | returnstring = returnstring + " " + String.Format("{0:x2}", data[i]); |
133 | returnstring = returnstring + " " + String.Format("{0:x2}", data[i]); |
| 132 | } |
134 | } |
| 133 | //returnstring = returnstring + "\n"; |
135 | //returnstring = returnstring + "\n"; |
| 134 | return returnstring; |
136 | return returnstring; |
| 135 | } |
137 | } |
| 136 | 138 | ||
| 137 | public byte[] getBytes() { |
139 | public byte[] getBytes() { |
| 138 | byte[] bytes = new byte[15]; |
140 | byte[] bytes = new byte[15]; |
| 139 | 141 | ||
| 140 | // 17 bytes, a packet. |
142 | // 17 bytes, a packet. |
| 141 | // UART_START_BYTE id[0] id[1] id[2] id[3] extended remote_request data_length d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] UART_END_BYTE |
143 | // UART_START_BYTE id[0] id[1] id[2] id[3] extended remote_request data_length d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] UART_END_BYTE |
| 142 | /* |
144 | /* |
| 143 | * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR |
145 | * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR |
| 144 | * |
146 | * |
| 145 | * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000 NMT, ... |
147 | * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000 NMT, ... |
| 146 | * <TYPE> = 00000000 11111111 00000000 00000000 = 0x00FF0000 NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...] |
148 | * <TYPE> = 00000000 11111111 00000000 00000000 = 0x00FF0000 NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...] |
| 147 | * <SID> = 00000000 00000000 11111111 00000000 = 0x0000FF00 Sender ID |
149 | * <SID> = 00000000 00000000 11111111 00000000 = 0x0000FF00 Sender ID |
| 148 | * <RID> = 00000000 00000000 00000000 11111111 = 0x000000FF Receiver ID |
150 | * <RID> = 00000000 00000000 00000000 11111111 = 0x000000FF Receiver ID |
| 149 | * |
151 | * |
| 150 | */ |
152 | */ |
| 151 | 153 | ||
| 152 | 154 | ||
| 153 | bytes[3] = (byte)((id & 0xFF000000) >> 24); |
155 | bytes[3] = (byte)((id & 0xFF000000) >> 24); |
| 154 | bytes[2] = (byte)((id & 0x00FF0000) >> 16); |
156 | bytes[2] = (byte)((id & 0x00FF0000) >> 16); |
| 155 | bytes[1] = (byte)((id & 0x0000FF00) >> 8); |
157 | bytes[1] = (byte)((id & 0x0000FF00) >> 8); |
| 156 | bytes[0] = (byte)((id & 0x000000FF)); |
158 | bytes[0] = (byte)((id & 0x000000FF)); |
| 157 | bytes[4]=ext; |
159 | bytes[4]=ext; |
| 158 | bytes[5]=rtr; |
160 | bytes[5]=rtr; |
| 159 | bytes[6]=this.data_length; |
161 | bytes[6]=this.data_length; |
| 160 | for(int i=0;i<8;i++) bytes[i+7]=this.data[i]; |
162 | for(int i=0;i<8;i++) bytes[i+7]=this.data[i]; |
| 161 | 163 | ||
| 162 | return bytes; |
164 | return bytes; |
| 163 | } |
165 | } |
| 164 | 166 | ||
| 165 | } |
167 | } |
| 166 | 168 | ||