Subversion Repositories HomeAutomation

Rev

Rev 434 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 434 Rev 436
Line 2... Line 2...
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 ulong id = 0;
11
    private ulong id = 0;
12
    private byte[] data = new byte[8];
12
    private byte[] data = new byte[8];
13
    private byte ext = 0;
13
    private byte ext = 0;
14
    private byte rtr = 0;
14
    private byte rtr = 0;
Line 29... Line 29...
29
       
29
       
30
        ulong addr = (((ulong)raw[startIndex + 3]) << 24) + (((ulong)raw[startIndex + 2]) << 16) + (((ulong)raw[startIndex + 1]) << 8) + (((ulong)raw[startIndex + 0]));
30
        ulong addr = (((ulong)raw[startIndex + 3]) << 24) + (((ulong)raw[startIndex + 2]) << 16) + (((ulong)raw[startIndex + 1]) << 8) + (((ulong)raw[startIndex + 0]));
31
       
31
       
32
        this.id = addr;
32
        this.id = addr;
33
        this.pktclass = (byte)((addr & 0x1E000000) >> 25);
33
        this.pktclass = (byte)((addr & 0x1E000000) >> 25);
34
        this.type =  (byte)((addr & 0x00FF0000) >> 16);
34
//      this.type =  (byte)((addr & 0x00FF0000) >> 16);
35
        this.sid =    (byte)((addr & 0x0000FF00) >> 8);
35
//      this.sid =    (byte)((addr & 0x0000FF00) >> 8);
36
        this.rid =    (byte)((addr & 0x000000FF));
36
//      this.rid =    (byte)((addr & 0x000000FF));
37
        this.data_length = raw[startIndex + 6];
37
        this.data_length = raw[startIndex + 6];
38
       
38
       
39
        ext = (byte)(0x0f & raw[startIndex + 4]);
39
        ext = (byte)(0x0f & raw[startIndex + 4]);
40
        rtr = (byte)(0x0f & raw[startIndex + 5]);
40
        rtr = (byte)(0x0f & raw[startIndex + 5]);
41
       
41
       
Line 45... Line 45...
45
    public CanPacket(string raw) {
45
    public CanPacket(string raw) {
46
       
46
       
47
       
47
       
48
    }
48
    }
49
   
49
   
50
    public CanPacket(byte pktclass, byte type, byte sid, byte rid, byte data_length, byte[] data, byte rtr, byte ext) {
50
//  public CanPacket(byte pktclass, byte type, byte sid, byte rid, byte data_length, byte[] data, byte rtr, byte ext) { 
51
        this.pktclass=pktclass;
51
//      this.pktclass=pktclass;
52
        this.type=type;
52
//      this.type=type;
53
        this.sid=sid;
53
//      this.sid=sid;
54
        this.rid=rid;
54
//      this.rid=rid;
55
        this.data_length=data_length;
55
//      this.data_length=data_length;
56
        for(int i=0;i<8;i++) this.data[i]=data[i];
56
//      for(int i=0;i<8;i++) this.data[i]=data[i];
57
        this.ext = (byte)(0x0f & ext);
57
//      this.ext = (byte)(0x0f & ext);
58
        this.rtr = (byte)(0x0f & rtr);
58
//      this.rtr = (byte)(0x0f & rtr);
59
    }
59
//  }
60
   
60
   
61
    public override int GetHashCode() {
61
    public override int GetHashCode() {
62
        return base.GetHashCode();
62
        return base.GetHashCode();
63
    }
63
    }
64
   
64
   
Line 66... Line 66...
66
        CanPacket cpm = (CanPacket)obj;
66
        CanPacket cpm = (CanPacket)obj;
67
       
67
       
68
        byte[] bytes = cpm.getData();
68
        byte[] bytes = cpm.getData();
69
       
69
       
70
        for(int i=0;i<8;i++) if (this.data[i]!=bytes[i]) return false;
70
        for(int i=0;i<8;i++) if (this.data[i]!=bytes[i]) return false;
71
       
71
       
72
        return (this.pktclass==cpm.getPktClass() && this.type==cpm.getType() && this.sid==cpm.getSid() && this.sid==cpm.getRid() && this.data_length==cpm.getDataLength());
72
        return (this.id==cpm.getId() && this.data_length==cpm.getDataLength());
73
       
73
       
74
    }
74
    }
75
   
75
   
76
    public ulong getId() { return this.id; }
76
    public ulong getId() { return this.id; }
77
    public byte getPktClass(){ return this.pktclass; }
77
    public byte getPktClass(){ return this.pktclass; }
78
    public uint getType(){ return this.type; }
78
//  public uint getType(){ return this.type; }
79
    public uint getSid(){ return this.sid; }
79
//  public uint getSid(){ return this.sid; }
80
    public uint getRid(){ return this.sid; }
80
//  public uint getRid(){ return this.sid; }
81
    public byte getDataLength(){ return this.data_length; }
81
    public byte getDataLength(){ return this.data_length; }
82
    public byte[] getData(){ return this.data; }
82
    public byte[] getData(){ return this.data; }
83
   
83
   
84
    public override string ToString() {
84
//  public override string ToString() {
85
        string str = "";
85
//      string str = "";
-
 
86
       
-
 
87
//      str = "pktclass: "+pktclass.ToString()+", type: "+type.ToString()+", sid: "+sid.ToString()+", rid: "+rid.ToString()+",data_length: "+data_length.ToString()+", data: ";
-
 
88
//      for (int i = 0; i < 8; i++) str+=data[i].ToString()+" ";
86
       
89
       
87
        str = "pktclass: "+pktclass.ToString()+", type: "+type.ToString()+", sid: "+sid.ToString()+", rid: "+rid.ToString()+",data_length: "+data_length.ToString()+", data: ";
-
 
88
        for (int i = 0; i < 8; i++) str+=data[i].ToString()+" ";
-
 
89
       
-
 
90
        return str;
90
//      return str;
91
    }
91
//  }
92
   
92
   
93
    public string toRawString() {
93
    public string toRawString() {
94
 
94
 
95
        string returnstring = "PKT " + String.Format("{0:x8}", id);
95
        string returnstring = "PKT " + String.Format("{0:x8}", id);
96
        returnstring = returnstring + " " + String.Format("{0:x1}", ext) + " " + String.Format("{0:x1}", rtr);
96
        returnstring = returnstring + " " + String.Format("{0:x1}", ext) + " " + String.Format("{0:x1}", rtr);
Line 114... Line 114...
114
        * <SID>   = 00000000 00000000 11111111 00000000 = 0x0000FF00    Sender ID
114
        * <SID>   = 00000000 00000000 11111111 00000000 = 0x0000FF00    Sender ID
115
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
115
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
116
        *
116
        *
117
        */
117
        */
118
       
118
       
119
        ulong addr = (((ulong)this.pktclass)<<25)+(((ulong)this.type)<<16)+(((ulong)this.sid)<<8)+((ulong)this.rid);
-
 
120
       
119
       
121
        bytes[3] = (byte)((addr & 0xFF000000) >> 24);
120
        bytes[3] = (byte)((id & 0xFF000000) >> 24);
122
        bytes[2] = (byte)((addr & 0x00FF0000) >> 16);
121
        bytes[2] = (byte)((id & 0x00FF0000) >> 16);
123
        bytes[1] = (byte)((addr & 0x0000FF00) >> 8);
122
        bytes[1] = (byte)((id & 0x0000FF00) >> 8);
124
        bytes[0] = (byte)((addr & 0x000000FF));
123
        bytes[0] = (byte)((id & 0x000000FF));
125
        bytes[4]=1;
124
        bytes[4]=ext;
126
        bytes[5]=0;
125
        bytes[5]=rtr;
127
        bytes[6]=this.data_length;
126
        bytes[6]=this.data_length;
128
        for(int i=0;i<8;i++) bytes[i+7]=this.data[i];
127
        for(int i=0;i<8;i++) bytes[i+7]=this.data[i];
129
       
128
       
130
        return bytes;
129
        return bytes;
131
    }
130
    }