Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
434 arune 1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
 
5
public class CanPacket {
6
    private byte pktclass = 0;
436 arune 7
//  private byte type = 0;
8
//  private byte sid = 0;
9
//  private byte rid = 0;
434 arune 10
    private byte data_length = 0;
11
    private ulong id = 0;
12
    private byte[] data = new byte[8];
13
    private byte ext = 0;
14
    private byte rtr = 0;
15
 
16
    public CanPacket(byte[] raw,uint startIndex) {
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
19
        /*
20
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
21
        *
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, ...]
24
        * <SID>   = 00000000 00000000 11111111 00000000 = 0x0000FF00    Sender ID
25
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
26
        *
27
        */
28
 
29
 
30
        ulong addr = (((ulong)raw[startIndex + 3]) << 24) + (((ulong)raw[startIndex + 2]) << 16) + (((ulong)raw[startIndex + 1]) << 8) + (((ulong)raw[startIndex + 0]));
31
 
32
        this.id = addr;
33
        this.pktclass = (byte)((addr & 0x1E000000) >> 25);
436 arune 34
//      this.type =  (byte)((addr & 0x00FF0000) >> 16);
35
//      this.sid =    (byte)((addr & 0x0000FF00) >> 8);
36
//      this.rid =    (byte)((addr & 0x000000FF));
434 arune 37
        this.data_length = raw[startIndex + 6];
38
 
39
        ext = (byte)(0x0f & raw[startIndex + 4]);
40
        rtr = (byte)(0x0f & raw[startIndex + 5]);
41
 
42
        for (int i = 0; i < 8; i++) this.data[i] = raw[startIndex + 7 + i];
43
    }
44
 
45
    public CanPacket(string raw) {
46
 
47
 
48
    }
49
 
436 arune 50
//  public CanPacket(byte pktclass, byte type, byte sid, byte rid, byte data_length, byte[] data, byte rtr, byte ext) { 
51
//      this.pktclass=pktclass;
52
//      this.type=type;
53
//      this.sid=sid;
54
//      this.rid=rid;
55
//      this.data_length=data_length;
56
//      for(int i=0;i<8;i++) this.data[i]=data[i];
57
//      this.ext = (byte)(0x0f & ext);
58
//      this.rtr = (byte)(0x0f & rtr);
59
//  }
434 arune 60
 
61
    public override int GetHashCode() {
62
        return base.GetHashCode();
63
    }
64
 
65
    public override bool  Equals(Object obj) {
66
        CanPacket cpm = (CanPacket)obj;
67
 
68
        byte[] bytes = cpm.getData();
69
 
70
        for(int i=0;i<8;i++) if (this.data[i]!=bytes[i]) return false;
71
 
436 arune 72
        return (this.id==cpm.getId() && this.data_length==cpm.getDataLength());
434 arune 73
 
74
    }
75
 
76
    public ulong getId() { return this.id; }
77
    public byte getPktClass(){ return this.pktclass; }
436 arune 78
//  public uint getType(){ return this.type; }
79
//  public uint getSid(){ return this.sid; }
80
//  public uint getRid(){ return this.sid; }
434 arune 81
    public byte getDataLength(){ return this.data_length; }
82
    public byte[] getData(){ return this.data; }
83
 
436 arune 84
//  public override string ToString() {
85
//      string str = "";
434 arune 86
 
436 arune 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()+" ";
434 arune 89
 
436 arune 90
//      return str;
91
//  }
434 arune 92
 
93
    public string toRawString() {
94
 
95
        string returnstring = "PKT " + String.Format("{0:x8}", id);
96
        returnstring = returnstring + " " + String.Format("{0:x1}", ext) + " " + String.Format("{0:x1}", rtr);
97
        for (int i = 0; i < data_length; i++) {
98
            returnstring = returnstring + " " + String.Format("{0:x2}", data[i]);
99
        }
100
        //returnstring = returnstring + "\n";
101
        return returnstring;
102
    }
103
 
104
    public byte[] getBytes() {
105
        byte[] bytes = new byte[15];
106
 
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
109
        /*
110
        * 000CCCCx TTTTTTTT SSSSSSSS RRRRRRRR
111
        *
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, ...]
114
        * <SID>   = 00000000 00000000 11111111 00000000 = 0x0000FF00    Sender ID
115
        * <RID>   = 00000000 00000000 00000000 11111111 = 0x000000FF    Receiver ID
116
        *
117
        */
118
 
119
 
436 arune 120
        bytes[3] = (byte)((id & 0xFF000000) >> 24);
121
        bytes[2] = (byte)((id & 0x00FF0000) >> 16);
122
        bytes[1] = (byte)((id & 0x0000FF00) >> 8);
123
        bytes[0] = (byte)((id & 0x000000FF));
124
        bytes[4]=ext;
125
        bytes[5]=rtr;
434 arune 126
        bytes[6]=this.data_length;
127
        for(int i=0;i<8;i++) bytes[i+7]=this.data[i];
128
 
129
        return bytes;
130
    }
131
 
132
}