Subversion Repositories HomeAutomation

Rev

Rev 434 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 434 Rev 436
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 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;
15
   
15
   
16
    public CanPacket(byte[] raw,uint startIndex) {
16
    public CanPacket(byte[] raw,uint startIndex) {
17
        // 17 bytes, a packet.
17
        // 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
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
19
        /*
19
        /*
20
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
20
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
21
        *
21
        *
22
        * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000    NMT, ...
22
        * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000    NMT, ...
23
        * <TYPE>  = 00000000 11111111 00000000 00000000 = 0x00FF0000    NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...]
23
        * <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
24
        * <SID>   = 00000000 00000000 11111111 00000000 = 0x0000FF00    Sender ID
25
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
25
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
26
        *
26
        *
27
        */
27
        */
28
       
28
       
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
       
42
        for (int i = 0; i < 8; i++) this.data[i] = raw[startIndex + 7 + i];
42
        for (int i = 0; i < 8; i++) this.data[i] = raw[startIndex + 7 + i];
43
    }
43
    }
44
   
44
   
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
   
65
    public override bool  Equals(Object obj) {
65
    public override bool  Equals(Object obj) {
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);
97
        for (int i = 0; i < data_length; i++) {
97
        for (int i = 0; i < data_length; i++) {
98
            returnstring = returnstring + " " + String.Format("{0:x2}", data[i]);
98
            returnstring = returnstring + " " + String.Format("{0:x2}", data[i]);
99
        }
99
        }
100
        //returnstring = returnstring + "\n";
100
        //returnstring = returnstring + "\n";
101
        return returnstring;
101
        return returnstring;
102
    }
102
    }
103
   
103
   
104
    public byte[] getBytes() {
104
    public byte[] getBytes() {
105
        byte[] bytes = new byte[15];
105
        byte[] bytes = new byte[15];
106
       
106
       
107
        // 17 bytes, a packet.
107
        // 17 bytes, a packet.
108
        // 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
108
        // 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
109
        /*
109
        /*
110
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
110
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
111
        *
111
        *
112
        * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000    NMT, ...
112
        * <CLASS> = 00011110 00000000 00000000 00000000 = 0x1E000000    NMT, ...
113
        * <TYPE>  = 00000000 11111111 00000000 00000000 = 0x00FF0000    NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...]
113
        * <TYPE>  = 00000000 11111111 00000000 00000000 = 0x00FF0000    NMT[CAN_NMT_PGM_START, CAN_ID_NMT_PGM_ACK, ...]
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
    }
132
   
131
   
133
}
132
}
134
 
133