Subversion Repositories HomeAutomation

Rev

Rev 450 | Rev 457 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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