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