Subversion Repositories HomeAutomation

Rev

Rev 876 | Rev 878 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 876 Rev 877
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
using System.Threading;
4
using System.Threading;
5
 
5
 
6
//this class should probably be integrated into canNMT instead
6
//this class should probably be integrated into canNMT instead
7
 
7
 
8
public class Downloader {
8
public class Downloader {
9
    private HexFile hf;
9
    private HexFile hf;
10
    private DaemonConnection dc;
10
    private DaemonConnection dc;
11
    private bool isBiosUpdate;
11
    private bool isBiosUpdate;
12
    private ulong dlOffset = 0;
12
    private ulong dlOffset = 0;
13
    //private Thread down;
13
    //private Thread down;
14
    private byte receiveID;
14
    private byte receiveID;
15
    private uint hwid;
15
    private uint hwid;
16
    private bool HWID_INUSE;
16
    private bool HWID_INUSE;
17
   
17
   
18
    private enum dState { SEND_START, WAIT_ACK_PRG, SEND_PGM_DATA, WAIT_ACK_DATA, RESEND_ADDR, WAIT_DONE, SEND_DONE, SEND_BIOS_UPDATE, SEND_RESET, DONE, DEBUG_STATE2, WAIT_BOOT, SEND_EMPTY_START, WAIT_ACK_PRG_EMPTY, SEND_EMPTY_DATA, WAIT_ACK_EMPTY_DATA, SEND_DONE_EMPTY, WAIT_DONE_EMPTY};
18
    private enum dState { SEND_START, WAIT_ACK_PRG, SEND_PGM_DATA, WAIT_ACK_DATA, RESEND_ADDR, WAIT_DONE, SEND_DONE, SEND_BIOS_UPDATE, SEND_RESET, DONE, DEBUG_STATE2, WAIT_BOOT, SEND_EMPTY_START, WAIT_ACK_PRG_EMPTY, SEND_EMPTY_DATA, WAIT_ACK_EMPTY_DATA, SEND_DONE_EMPTY, WAIT_DONE_EMPTY};
19
   
19
   
20
    private const byte CAN_NMT              = 0x00;
20
    private const byte CAN_NMT              = 0x00;
21
   
21
   
22
    private const byte CAN_NMT_RESET        = 0x04;
22
    private const byte CAN_NMT_RESET        = 0x04;
23
    private const byte CAN_NMT_BIOS_START   = 0x08;
23
    private const byte CAN_NMT_BIOS_START   = 0x08;
24
    private const byte CAN_NMT_PGM_START    = 0x0C;
24
    private const byte CAN_NMT_PGM_START    = 0x0C;
25
    private const byte CAN_NMT_PGM_DATA     = 0x10;
25
    private const byte CAN_NMT_PGM_DATA     = 0x10;
26
    private const byte CAN_NMT_PGM_END      = 0x14;
26
    private const byte CAN_NMT_PGM_END      = 0x14;
27
    private const byte CAN_NMT_PGM_COPY     = 0x18;
27
    private const byte CAN_NMT_PGM_COPY     = 0x18;
28
    private const byte CAN_NMT_PGM_ACK      = 0x1C;
28
    private const byte CAN_NMT_PGM_ACK      = 0x1C;
29
    private const byte CAN_NMT_PGM_NACK     = 0x20;
29
    private const byte CAN_NMT_PGM_NACK     = 0x20;
30
    private const byte CAN_NMT_START_APP    = 0x24;
30
    private const byte CAN_NMT_START_APP    = 0x24;
31
    private const byte CAN_NMT_APP_START    = 0x28;
31
    private const byte CAN_NMT_APP_START    = 0x28;
32
    private const byte CAN_NMT_HEARTBEAT    = 0x2C;
32
    private const byte CAN_NMT_HEARTBEAT    = 0x2C;
33
   
33
   
34
    private const byte MY_ID = 0x00;
34
    private const byte MY_ID = 0x00;
35
    //private const byte TARGET_ID = 0x78;
35
    //private const byte TARGET_ID = 0x78;
36
    private const byte MY_NID = 0x0;
36
    private const byte MY_NID = 0x0;
37
    private const byte ADDR0_INDEX = 0;
37
    private const byte ADDR0_INDEX = 0;
38
    private const byte ADDR1_INDEX = 1;
38
    private const byte ADDR1_INDEX = 1;
39
    private const byte ADDR2_INDEX = 2;
39
    private const byte ADDR2_INDEX = 2;
40
    private const byte ADDR3_INDEX = 3;
40
    private const byte ADDR3_INDEX = 3;
41
 
41
 
42
    private const byte HWID0_INDEX = 4;
42
    private const byte HWID0_INDEX = 4;
43
    private const byte HWID1_INDEX = 5;
43
    private const byte HWID1_INDEX = 5;
44
    private const byte HWID2_INDEX = 6;
44
    private const byte HWID2_INDEX = 6;
45
    private const byte HWID3_INDEX = 7;
45
    private const byte HWID3_INDEX = 7;
46
   
46
   
47
    private const int TIMEOUT_MS = 1000;
47
    private const int TIMEOUT_MS = 1000;
48
    private const int TIMEOUT_SHORT_MS = 200;
48
    private const int TIMEOUT_SHORT_MS = 200;
49
   
49
   
50
    private long timeStart = 0;
50
    private long timeStart = 0;
51
   
51
   
52
    public Downloader(HexFile hf, DaemonConnection dc, byte receiveID, bool isBiosUpdate, bool HWID_INUSE, uint hwid) {
52
    public Downloader(HexFile hf, DaemonConnection dc, byte receiveID, bool isBiosUpdate, bool HWID_INUSE, uint hwid) {
53
        this.hf = hf;
53
        this.hf = hf;
54
        this.dc = dc;
54
        this.dc = dc;
55
        this.receiveID = receiveID;
55
        this.receiveID = receiveID;
56
        this.isBiosUpdate = isBiosUpdate;
56
        this.isBiosUpdate = isBiosUpdate;
57
        this.hwid = hwid;
57
        this.hwid = hwid;
58
        this.HWID_INUSE = HWID_INUSE;
58
        this.HWID_INUSE = HWID_INUSE;
59
    }
59
    }
60
   
60
   
61
    public int calcCRC(HexFile hf) {
61
    public int calcCRC(HexFile hf) {
62
        int crc=0;
62
        int crc=0;
63
        for (ulong i=hf.getAddrLower(); i <= hf.getAddrUpper(); i++) {
63
        for (ulong i=hf.getAddrLower(); i <= hf.getAddrUpper(); i++) {
64
            crc = crc16_update(crc, hf.getByte(i));
64
            crc = crc16_update(crc, hf.getByte(i));
65
        }
65
        }
66
        return crc;
66
        return crc;
67
    }
67
    }
68
   
68
   
69
    public int crc16_update(int crc, byte a) {
69
    public int crc16_update(int crc, byte a) {
70
        int i;
70
        int i;
71
        crc ^= a;
71
        crc ^= a;
72
        for (i = 0; i < 8; ++i) {
72
        for (i = 0; i < 8; ++i) {
73
            if ((crc & 1) != 0)
73
            if ((crc & 1) != 0)
74
                crc = (crc >> 1) ^ 0xA001;
74
                crc = (crc >> 1) ^ 0xA001;
75
            else
75
            else
76
                crc = (crc >> 1);
76
                crc = (crc >> 1);
77
        }
77
        }
78
        return crc;
78
        return crc;
79
    }
79
    }
80
   
80
   
81
    public bool go() {
81
    public bool go() {
82
        if (dc==null || hf==null || hf.getLength()==0) return false;
82
        if (dc==null || hf==null || hf.getLength()==0) return false;
83
        return downloader();
83
        return downloader();
84
    }
84
    }
85
   
85
   
86
    public void abort() {
86
    public void abort() {
87
        //if (down != null && down.IsAlive) down.Abort();
87
        //if (down != null && down.IsAlive) down.Abort();
88
    }
88
    }
89
   
89
   
90
    public bool downloader() {
90
    public bool downloader() {
91
        dState pgs = dState.SEND_START;
91
        dState pgs = dState.SEND_START;
92
        int t = 0;
92
        int t = 0;
93
        int t2 = 0;
93
        int t2 = 0;
94
        CanPacket outCm = null;
94
        CanPacket outCm = null;
95
        CanPacket cm = null;
95
        CanPacket cm = null;
96
        byte[] data = new byte[8];
96
        byte[] data = new byte[8];
97
        uint byteSent = 0;
97
        uint byteSent = 0;
98
        ulong currentAddress = 0;
98
        ulong currentAddress = 0;
99
        bool done = false;
99
        bool done = false;
100
        bool errorOccured = false;
100
        bool errorOccured = false;
101
       
101
       
102
        //int rotstate=0;
102
        //int rotstate=0;
103
        int dldisplaystate=0;
103
        int dldisplaystate=0;
104
       
104
       
105
        try {
105
        try {
106
            if (isBiosUpdate) {
106
            if (isBiosUpdate) {
107
                dlOffset = hf.getAddrLower();
107
                dlOffset = hf.getAddrLower();
108
                //Console.WriteLine(dlOffset);
108
                //Console.WriteLine(dlOffset);
109
                //dlOffset = 200;
109
                //dlOffset = 200;
110
            }
110
            }
111
 
111
 
112
            CanNMT cpn = new CanNMT(HWID_INUSE);
112
            CanNMT cpn = new CanNMT(HWID_INUSE);
113
            cpn.setReceiver(receiveID);
113
            cpn.setReceiver(receiveID);
114
            bool hasMessage = false;
114
            bool hasMessage = false;
115
           
115
           
116
            while (!done) {
116
            while (!done) {
117
                Thread.Sleep(1);
117
                Thread.Sleep(1);
118
               
118
               
119
                switch (pgs) {
119
                switch (pgs) {
120
                    case dState.SEND_START:
120
                    case dState.SEND_START:
121
                        // Send boot start packet to target.
121
                        // Send boot start packet to target.
122
                        // and wait for ack.
122
                        // and wait for ack.
123
                        currentAddress = hf.getAddrLower();
123
                        currentAddress = hf.getAddrLower();
124
                        //data[RID_INDEX] = TARGET_ID;
124
                        //data[RID_INDEX] = TARGET_ID;
125
                        data[ADDR0_INDEX] = (byte)((currentAddress-dlOffset) & 0x0000FF);
125
                        data[ADDR0_INDEX] = (byte)((currentAddress-dlOffset) & 0x0000FF);
126
                        data[ADDR1_INDEX] = (byte)(((currentAddress-dlOffset) & 0x00FF00) >> 8);
126
                        data[ADDR1_INDEX] = (byte)(((currentAddress-dlOffset) & 0x00FF00) >> 8);
127
                        data[ADDR2_INDEX] = (byte)(((currentAddress-dlOffset) & 0xFF0000) >> 16);
127
                        data[ADDR2_INDEX] = (byte)(((currentAddress-dlOffset) & 0xFF0000) >> 16);
128
                        data[ADDR3_INDEX] = 0;
128
                        data[ADDR3_INDEX] = 0;
129
                        if (HWID_INUSE) {
129
                        if (HWID_INUSE) {
130
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
130
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
131
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
131
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
132
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
132
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
133
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
133
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
134
                        }
134
                        }
135
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
135
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
136
                        outCm = cpn.getPgmStartPacket(data);
136
                        outCm = cpn.getPgmStartPacket(data);
137
                        dc.sendCanPacket(outCm);
137
                        dc.sendCanPacket(outCm);
138
                        t = Environment.TickCount;
138
                        t = Environment.TickCount;
139
                        t2 = Environment.TickCount;
139
                        t2 = Environment.TickCount;
140
                        pgs = dState.WAIT_ACK_PRG;
140
                        pgs = dState.WAIT_ACK_PRG;
141
                        timeStart = Environment.TickCount;
141
                        timeStart = Environment.TickCount;
142
                        Console.WriteLine("Downloading...");
142
                        Console.WriteLine("Downloading...");
-
 
143
                        Console.Write("  0%");
143
                        break;
144
                        break;
144
                   
145
                   
145
                   
146
                   
146
                    case dState.WAIT_ACK_PRG:
147
                    case dState.WAIT_ACK_PRG:
147
                        // Check for timeout, resend start packet in that case..
148
                        // Check for timeout, resend start packet in that case..
148
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
149
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
149
                            Console.WriteLine("Timeout while waiting for start prg ack.");
150
                            Console.WriteLine("Timeout while waiting for start prg ack.");
150
                            errorOccured = true;
151
                            errorOccured = true;
151
                            pgs = dState.SEND_RESET;
152
                            pgs = dState.SEND_RESET;
152
                        }
153
                        }
153
                        //Thread.Sleep(1);
154
                        //Thread.Sleep(1);
154
                        // If received ack.
155
                        // If received ack.
155
                        hasMessage = dc.getData(out cm);
156
                        hasMessage = dc.getData(out cm);
156
                        if (hasMessage) {
157
                        if (hasMessage) {
157
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
158
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
158
                            if (cpnin.isNMTPacket()) {
159
                            if (cpnin.isNMTPacket()) {
159
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
160
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
160
                                // If no error
161
                                // If no error
161
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
162
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
162
                                    // Start sending program data..
163
                                    // Start sending program data..
163
                                    byteSent = 0;
164
                                    byteSent = 0;
164
                                    pgs = dState.SEND_PGM_DATA;
165
                                    pgs = dState.SEND_PGM_DATA;
165
                                }
166
                                }
166
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
167
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
167
                                    // else ..
168
                                    // else ..
168
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
169
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
169
                                    errorOccured = true;
170
                                    errorOccured = true;
170
                                    pgs = dState.SEND_RESET;
171
                                    pgs = dState.SEND_RESET;
171
                                }
172
                                }
172
                            }
173
                            }
173
                        }
174
                        }
174
                        break;
175
                        break;
175
                       
176
                       
176
                    case dState.SEND_PGM_DATA:
177
                    case dState.SEND_PGM_DATA:
177
                        //Console.WriteLine("byteSent = "+byteSent);
178
                        //Console.WriteLine("byteSent = "+byteSent);
178
//disable                       Console.Write(".");
179
//disable                       Console.Write(".");
179
//Console.Write("\b");
180
//Console.Write("\b");
180
//int curleft = Console.CursorLeft;
181
//int curleft = Console.CursorLeft;
181
//int curtop = Console.CursorTop;
182
//int curtop = Console.CursorTop;
182
//Console.SetCursorPosition(curleft, curtop-1);
183
//Console.SetCursorPosition(curleft, curtop-1);
183
 
184
 
184
//if (rotstate==0) {
185
//if (rotstate==0) {
185
//   Console.Write("-");
186
//   Console.Write("-");
186
//   rotstate=1;
187
//   rotstate=1;
187
//} else if (rotstate==1) {
188
//} else if (rotstate==1) {
188
//   Console.Write("\\");
189
//   Console.Write("\\");
189
//   rotstate=2;
190
//   rotstate=2;
190
//} else if (rotstate==2) {
191
//} else if (rotstate==2) {
191
//   Console.Write("|");
192
//   Console.Write("|");
192
//   rotstate=3;
193
//   rotstate=3;
193
//} else if (rotstate==3) {
194
//} else if (rotstate==3) {
194
//   Console.Write("/");
195
//   Console.Write("/");
195
//   rotstate=0;
196
//   rotstate=0;
196
//}
197
//}
197
//Console.Write("\r");
198
//Console.Write("\r");
198
//dldisplaystate
199
//dldisplaystate
199
 
200
 
200
//FULHACK deulx
201
//FULHACK deulx
201
                        ulong percent = byteSent*100/(hf.getAddrUpper()-currentAddress);
202
                        ulong percent = byteSent*100/(hf.getAddrUpper()-currentAddress);
202
                       
203
                       
203
                        if (percent>9 && dldisplaystate==0) {
204
                        if (percent>9 && dldisplaystate==0) {
204
                           Console.Write("\r");
205
                           Console.Write("\r");
205
                           Console.Write(percent+"%");
206
                           Console.Write(" "+percent+"%");
206
                           dldisplaystate=1;
207
                           dldisplaystate=1;
207
                        } else if (percent>19 && dldisplaystate==1) {
208
                        } else if (percent>19 && dldisplaystate==1) {
208
                           Console.Write("\r");
209
                           Console.Write("\r");
209
                           Console.Write(percent+"%");
210
                           Console.Write(" "+percent+"%");
210
                           dldisplaystate=2;
211
                           dldisplaystate=2;
211
                        } else if (percent>29 && dldisplaystate==2) {
212
                        } else if (percent>29 && dldisplaystate==2) {
212
                           Console.Write("\r");
213
                           Console.Write("\r");
213
                           Console.Write(percent+"%");
214
                           Console.Write(" "+percent+"%");
214
                           dldisplaystate=3;
215
                           dldisplaystate=3;
215
                        } else if (percent>39 && dldisplaystate==3) {
216
                        } else if (percent>39 && dldisplaystate==3) {
216
                           Console.Write("\r");
217
                           Console.Write("\r");
217
                           Console.Write(percent+"%");
218
                           Console.Write(" "+percent+"%");
218
                           dldisplaystate=4;
219
                           dldisplaystate=4;
219
                        } else if (percent>49 && dldisplaystate==4) {
220
                        } else if (percent>49 && dldisplaystate==4) {
220
                           Console.Write("\r");
221
                           Console.Write("\r");
221
                           Console.Write(percent+"%");
222
                           Console.Write(" "+percent+"%");
222
                           dldisplaystate=5;
223
                           dldisplaystate=5;
223
                        } else if (percent>59 && dldisplaystate==5) {
224
                        } else if (percent>59 && dldisplaystate==5) {
224
                           Console.Write("\r");
225
                           Console.Write("\r");
225
                           Console.Write(percent+"%");
226
                           Console.Write(" "+percent+"%");
226
                           dldisplaystate=6;
227
                           dldisplaystate=6;
227
                        } else if (percent>69 && dldisplaystate==6) {
228
                        } else if (percent>69 && dldisplaystate==6) {
228
                           Console.Write("\r");
229
                           Console.Write("\r");
229
                           Console.Write(percent+"%");
230
                           Console.Write(" "+percent+"%");
230
                           dldisplaystate=7;
231
                           dldisplaystate=7;
231
                        } else if (percent>79 && dldisplaystate==7) {
232
                        } else if (percent>79 && dldisplaystate==7) {
232
                           Console.Write("\r");
233
                           Console.Write("\r");
233
                           Console.Write(percent+"%");
234
                           Console.Write(" "+percent+"%");
234
                           dldisplaystate=8;
235
                           dldisplaystate=8;
235
                        } else if (percent>89 && dldisplaystate==8) {
236
                        } else if (percent>89 && dldisplaystate==8) {
236
                           Console.Write("\r");
237
                           Console.Write("\r");
237
                           Console.Write(percent+"%");
238
                           Console.Write(" "+percent+"%");
238
                           dldisplaystate=9;
239
                           dldisplaystate=9;
239
                        } else if (percent>99 && dldisplaystate==9) {
240
                        } else if (percent>99 && dldisplaystate==9) {
240
                           Console.Write("\r");
241
                           Console.Write("\r");
241
                           Console.Write(percent+"%");
242
                           Console.Write(" "+percent+"%");
242
                           dldisplaystate=10;
243
                           dldisplaystate=10;
243
                        }
244
                        }
-
 
245
                        Console.Write(".");
244
 
246
 
245
                        // Send program data.
247
                        // Send program data.
246
                        byte datalength = 2;
248
                        byte datalength = 2;
247
                        for (ulong i = 0; i < 6; i++) {
249
                        for (ulong i = 0; i < 6; i++) {
248
                            // Populate data.
250
                            // Populate data.
249
                            data[i+2] = hf.getByte(currentAddress + i + byteSent);
251
                            data[i+2] = hf.getByte(currentAddress + i + byteSent);
250
                            if (currentAddress + i + byteSent > hf.getAddrUpper()) {
252
                            if (currentAddress + i + byteSent > hf.getAddrUpper()) {
251
                                break;  //then done
253
                                break;  //then done
252
                            }
254
                            }
253
                            datalength++;
255
                            datalength++;
254
                        }
256
                        }
255
                        data[0] = (byte)(byteSent & 0x00FF);
257
                        data[0] = (byte)(byteSent & 0x00FF);
256
                        data[1] = (byte)((byteSent & 0xFF00) >> 8);
258
                        data[1] = (byte)((byteSent & 0xFF00) >> 8);
257
 
259
 
258
                        outCm = cpn.getPgmDataPacket(data, datalength);
260
                        outCm = cpn.getPgmDataPacket(data, datalength);
259
                        dc.sendCanPacket(outCm);
261
                        dc.sendCanPacket(outCm);
260
                       
262
                       
261
                        t = Environment.TickCount;
263
                        t = Environment.TickCount;
262
                        t2 = Environment.TickCount;
264
                        t2 = Environment.TickCount;
263
                        byteSent += 6;
265
                        byteSent += 6;
264
                        pgs = dState.WAIT_ACK_DATA;
266
                        pgs = dState.WAIT_ACK_DATA;
265
                        break;
267
                        break;
266
                       
268
                       
267
                       
269
                       
268
                    case dState.WAIT_ACK_DATA:
270
                    case dState.WAIT_ACK_DATA:
269
                        // Wait for pgm ack.
271
                        // Wait for pgm ack.
270
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
272
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
271
                            // Woops, error. 
273
                            // Woops, error. 
272
                            Console.WriteLine("Timeout while waiting for data ack.");
274
                            Console.WriteLine("Timeout while waiting for data ack.");
273
                            errorOccured = true;
275
                            errorOccured = true;
274
                            pgs = dState.SEND_RESET;
276
                            pgs = dState.SEND_RESET;
275
                        }
277
                        }
276
                        else if ((Environment.TickCount - t2) > TIMEOUT_SHORT_MS) {
278
                        else if ((Environment.TickCount - t2) > TIMEOUT_SHORT_MS) {
277
                            // Woops, error. 
279
                            // Woops, error. 
278
//disable                           Console.Write(":");
280
//disable                           Console.Write(":");
279
                            byteSent -= 6;
281
                            byteSent -= 6;
280
                            pgs = dState.SEND_PGM_DATA;
282
                            pgs = dState.SEND_PGM_DATA;
281
                            break;
283
                            break;
282
                        }
284
                        }
283
                        //Thread.Sleep(1);
285
                        //Thread.Sleep(1);
284
                        // If received ack.
286
                        // If received ack.
285
                        hasMessage = dc.getData(out cm);
287
                        hasMessage = dc.getData(out cm);
286
                        if (hasMessage) {
288
                        if (hasMessage) {
287
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
289
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
288
                            if (cpnin.isNMTPacket()) {
290
                            if (cpnin.isNMTPacket()) {
289
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
291
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
290
                                // If no error
292
                                // If no error
291
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
293
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
292
                                    //currentAddress += 2;
294
                                    //currentAddress += 2;
293
                                    // Check if end.
295
                                    // Check if end.
294
                                    if (currentAddress + byteSent > hf.getAddrUpper()) {
296
                                    if (currentAddress + byteSent > hf.getAddrUpper()) {
295
                                        // Yes, we are done, send done.
297
                                        // Yes, we are done, send done.
296
                                        pgs = dState.SEND_DONE;
298
                                        pgs = dState.SEND_DONE;
297
                                    }
299
                                    }
298
                                    else {
300
                                    else {
299
                                        // More to write.
301
                                        // More to write.
300
                                        //byteSent = 0;
302
                                        //byteSent = 0;
301
                                        pgs = dState.SEND_PGM_DATA;
303
                                        pgs = dState.SEND_PGM_DATA;
302
                                    }
304
                                    }
303
                                }
305
                                }
304
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
306
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
305
                                    // else ..
307
                                    // else ..
306
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
308
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
307
                                    errorOccured = true;
309
                                    errorOccured = true;
308
                                    pgs = dState.SEND_RESET;
310
                                    pgs = dState.SEND_RESET;
309
                                }
311
                                }
310
                            }
312
                            }
311
                        }
313
                        }
312
                        break;
314
                        break;
313
                       
315
                       
314
                    case dState.SEND_DONE:
316
                    case dState.SEND_DONE:
315
                       Console.Write("\r");
317
                       Console.Write("\r");
316
                       Console.Write("100%");
318
                       Console.Write("100%");
317
                        // Send done
319
                        // Send done
318
                        outCm = cpn.getPgmEndPacket();
320
                        outCm = cpn.getPgmEndPacket();
319
                        dc.sendCanPacket(outCm);
321
                        dc.sendCanPacket(outCm);
320
 
322
 
321
                        t = Environment.TickCount;
323
                        t = Environment.TickCount;
322
                        t2 = Environment.TickCount;
324
                        t2 = Environment.TickCount;
323
                        pgs = dState.WAIT_DONE;
325
                        pgs = dState.WAIT_DONE;
324
                        break;
326
                        break;
325
                       
327
                       
326
                       
328
                       
327
                    case dState.WAIT_DONE:
329
                    case dState.WAIT_DONE:
328
                        // Check for timeout, resend done packet in that case..
330
                        // Check for timeout, resend done packet in that case..
329
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
331
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
330
                            Console.WriteLine("Timeout while waiting for end prg ack.");
332
                            Console.WriteLine("Timeout while waiting for end prg ack.");
331
                            errorOccured = true;
333
                            errorOccured = true;
332
                            pgs = dState.SEND_RESET;
334
                            pgs = dState.SEND_RESET;
333
                        }
335
                        }
334
                        //Thread.Sleep(1);
336
                        //Thread.Sleep(1);
335
                       
337
                       
336
                        hasMessage = dc.getData(out cm);
338
                        hasMessage = dc.getData(out cm);
337
                        if (hasMessage) {
339
                        if (hasMessage) {
338
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
340
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
339
                            if (cpnin.isNMTPacket()) {
341
                            if (cpnin.isNMTPacket()) {
340
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
342
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
341
                                // If no error
343
                                // If no error
342
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
344
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
343
                                    Console.WriteLine("");
345
                                    Console.WriteLine("");
344
                                    //
346
                                    //
345
                                    int crc = calcCRC(hf);
347
                                    int crc = calcCRC(hf);
346
                               
348
                               
347
                                    if (cm.getDataLength() == 2) {
349
                                    if (cm.getDataLength() == 2) {
348
                                        byte[] cmdata = cm.getData();
350
                                        byte[] cmdata = cm.getData();
349
                                        if ((cmdata[0] == (crc & 0xFF)) && (cmdata[1] == (crc>>8))) {
351
                                        if ((cmdata[0] == (crc & 0xFF)) && (cmdata[1] == (crc>>8))) {
350
                                            if (!isBiosUpdate) {
352
                                            if (!isBiosUpdate) {
351
                                                Console.WriteLine("Done, successfully programmed application.");
353
                                                Console.WriteLine("Done, successfully programmed application.");
352
                                                pgs = dState.SEND_RESET;
354
                                                pgs = dState.SEND_RESET;
353
                                            } else {
355
                                            } else {
354
                                                Console.WriteLine("Done, successfully programmed bios to application space.");
356
                                                Console.WriteLine("Done, successfully programmed bios to application space.");
355
                                                pgs = dState.SEND_BIOS_UPDATE;
357
                                                pgs = dState.SEND_BIOS_UPDATE;
356
                                            }
358
                                            }
357
                                        } else {
359
                                        } else {
358
                                            string nodecrc = String.Format("{0:x4}", (int)cmdata[0] + (int)(cmdata[1]<<8));
360
                                            string nodecrc = String.Format("{0:x4}", (int)cmdata[0] + (int)(cmdata[1]<<8));
359
                                            nodecrc = "0x"+nodecrc;
361
                                            nodecrc = "0x"+nodecrc;
360
                                            string realcrc = String.Format("{0:x4}", (int)(crc & 0xFF) + (int)(crc & 0xFF00));
362
                                            string realcrc = String.Format("{0:x4}", (int)(crc & 0xFF) + (int)(crc & 0xFF00));
361
                                            realcrc = "0x"+realcrc;
363
                                            realcrc = "0x"+realcrc;
362
                                            Console.WriteLine("CRC failed. Node sent CRC "+nodecrc+" but should be "+realcrc+".");
364
                                            Console.WriteLine("CRC failed. Node sent CRC "+nodecrc+" but should be "+realcrc+".");
363
                                            errorOccured = true;
365
                                            errorOccured = true;
364
                                            pgs = dState.SEND_RESET;
366
                                            pgs = dState.SEND_RESET;
365
                                        }
367
                                        }
366
                                       
368
                                       
367
                                        byteSent = 0;
369
                                        byteSent = 0;
368
                                    }
370
                                    }
369
                                    //Console.WriteLine("crc: " + crc + " " + (crc>>8) + " " + (crc & 0xFF));
371
                                    //Console.WriteLine("crc: " + crc + " " + (crc>>8) + " " + (crc & 0xFF));
370
                                    //Console.WriteLine("ACKpkg with CRC: " + cm.ToString());
372
                                    //Console.WriteLine("ACKpkg with CRC: " + cm.ToString());
371
                                    // 
373
                                    // 
372
                                }
374
                                }
373
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
375
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
374
                                    // else resend addr..
376
                                    // else resend addr..
375
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
377
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
376
                                    errorOccured = true;
378
                                    errorOccured = true;
377
                                    pgs = dState.SEND_RESET;
379
                                    pgs = dState.SEND_RESET;
378
                                }
380
                                }
379
                            }
381
                            }
380
                        }
382
                        }
381
                        break;
383
                        break;
382
                       
384
                       
383
                    case dState.SEND_BIOS_UPDATE:
385
                    case dState.SEND_BIOS_UPDATE:
384
                        data[0] = (byte)(0 & 0x00FF);
386
                        data[0] = (byte)(0 & 0x00FF);
385
                        data[1] = (byte)((0 & 0xFF00) >> 8);
387
                        data[1] = (byte)((0 & 0xFF00) >> 8);
386
                        data[2] = (byte)(dlOffset & 0x00FF);
388
                        data[2] = (byte)(dlOffset & 0x00FF);
387
                        data[3] = (byte)((dlOffset & 0xFF00) >> 8);
389
                        data[3] = (byte)((dlOffset & 0xFF00) >> 8);
388
                        data[4] = (byte)((hf.getAddrUpper()-hf.getAddrLower()+1) & 0x00FF);
390
                        data[4] = (byte)((hf.getAddrUpper()-hf.getAddrLower()+1) & 0x00FF);
389
                        data[5] = (byte)(((hf.getAddrUpper()-hf.getAddrLower()+1) & 0xFF00) >> 8);
391
                        data[5] = (byte)(((hf.getAddrUpper()-hf.getAddrLower()+1) & 0xFF00) >> 8);
390
                       
392
                       
391
                        outCm = cpn.getPgmCopyPacket(data);
393
                        outCm = cpn.getPgmCopyPacket(data);
392
                        Console.WriteLine("Now say a prayer, moving bios");
394
                        Console.WriteLine("Now say a prayer, moving bios");
393
                        dc.sendCanPacket(outCm);
395
                        dc.sendCanPacket(outCm);
394
                        t = Environment.TickCount;
396
                        t = Environment.TickCount;
395
                        t2 = Environment.TickCount;
397
                        t2 = Environment.TickCount;
396
                        pgs = dState.WAIT_BOOT;
398
                        pgs = dState.WAIT_BOOT;
397
                        //pgs = dState.DONE;
399
                        //pgs = dState.DONE;
398
                        break;
400
                        break;
399
 
401
 
400
                    case dState.WAIT_BOOT:
402
                    case dState.WAIT_BOOT:
401
                        // Wait for node reset answer (boot msg).
403
                        // Wait for node reset answer (boot msg).
402
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
404
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
403
                            // Woops, error. 
405
                            // Woops, error. 
404
                            Console.WriteLine("Timeout while waiting for node to boot.");
406
                            Console.WriteLine("Timeout while waiting for node to boot.");
405
                            errorOccured = true;
407
                            errorOccured = true;
406
                            pgs = dState.SEND_DONE;
408
                            pgs = dState.SEND_DONE;
407
                        }
409
                        }
408
                        //Thread.Sleep(1);
410
                        //Thread.Sleep(1);
409
                        // If received boot msg.
411
                        // If received boot msg.
410
                        hasMessage = dc.getData(out cm);
412
                        hasMessage = dc.getData(out cm);
411
                        if (hasMessage) {
413
                        if (hasMessage) {
412
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
414
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
413
                            if (cpnin.isNMTPacket()) {
415
                            if (cpnin.isNMTPacket()) {
414
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
416
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
415
                                // If no error
417
                                // If no error
416
                                if (cpnin.getType() == CAN_NMT_BIOS_START) {
418
                                if (cpnin.getType() == CAN_NMT_BIOS_START) {
417
                                    pgs = dState.SEND_EMPTY_START;
419
                                    pgs = dState.SEND_EMPTY_START;
418
                                }
420
                                }
419
                                else {
421
                                else {
420
                                    // else ..
422
                                    // else ..
421
                                }
423
                                }
422
                            }
424
                            }
423
                        }
425
                        }
424
                        break;
426
                        break;
425
                       
427
                       
426
                    case dState.SEND_EMPTY_START:
428
                    case dState.SEND_EMPTY_START:
427
                        // Send boot start packet to target.
429
                        // Send boot start packet to target.
428
                        // and wait for ack.
430
                        // and wait for ack.
429
                       
431
                       
430
                        //data[RID_INDEX] = TARGET_ID;
432
                        //data[RID_INDEX] = TARGET_ID;
431
                        data[ADDR0_INDEX] = (byte)((0) & 0x0000FF);
433
                        data[ADDR0_INDEX] = (byte)((0) & 0x0000FF);
432
                        data[ADDR1_INDEX] = (byte)(((0) & 0x00FF00) >> 8);
434
                        data[ADDR1_INDEX] = (byte)(((0) & 0x00FF00) >> 8);
433
                        data[ADDR2_INDEX] = (byte)(((0) & 0xFF0000) >> 16);
435
                        data[ADDR2_INDEX] = (byte)(((0) & 0xFF0000) >> 16);
434
                        data[ADDR3_INDEX] = 0;
436
                        data[ADDR3_INDEX] = 0;
435
                        if (HWID_INUSE) {
437
                        if (HWID_INUSE) {
436
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
438
                            data[HWID0_INDEX] = (byte)((hwid) & 0xFF);
437
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
439
                            data[HWID1_INDEX] = (byte)((hwid>>8) & 0xFF);
438
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
440
                            data[HWID2_INDEX] = (byte)((hwid>>16) & 0xFF);
439
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
441
                            data[HWID3_INDEX] = (byte)((hwid>>24) & 0xFF);
440
                        }
442
                        }
441
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
443
                        //outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
442
                        outCm = cpn.getPgmStartPacket(data);
444
                        outCm = cpn.getPgmStartPacket(data);
443
                        dc.sendCanPacket(outCm);
445
                        dc.sendCanPacket(outCm);
444
                        t = Environment.TickCount;
446
                        t = Environment.TickCount;
445
                        t2 = Environment.TickCount;
447
                        t2 = Environment.TickCount;
446
                        pgs = dState.WAIT_ACK_PRG_EMPTY;
448
                        pgs = dState.WAIT_ACK_PRG_EMPTY;
447
                        break;
449
                        break;
448
                   
450
                   
449
                   
451
                   
450
                    case dState.WAIT_ACK_PRG_EMPTY:
452
                    case dState.WAIT_ACK_PRG_EMPTY:
451
                        // Check for timeout, resend start packet in that case..
453
                        // Check for timeout, resend start packet in that case..
452
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
454
                        if ((Environment.TickCount - t) > 3*TIMEOUT_MS) {
453
                            Console.WriteLine("Timeout while waiting for start prg ack during empty-first-block-cycle.");
455
                            Console.WriteLine("Timeout while waiting for start prg ack during empty-first-block-cycle.");
454
                            errorOccured = true;
456
                            errorOccured = true;
455
                            pgs = dState.SEND_RESET;
457
                            pgs = dState.SEND_RESET;
456
                        }
458
                        }
457
                        //Thread.Sleep(1);
459
                        //Thread.Sleep(1);
458
                        // If received ack.
460
                        // If received ack.
459
                        hasMessage = dc.getData(out cm);
461
                        hasMessage = dc.getData(out cm);
460
                        if (hasMessage) {
462
                        if (hasMessage) {
461
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
463
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
462
                            if (cpnin.isNMTPacket()) {
464
                            if (cpnin.isNMTPacket()) {
463
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
465
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
464
                                // If no error
466
                                // If no error
465
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
467
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
466
                                    // Start sending program data..
468
                                    // Start sending program data..
467
                                    byteSent = 0;
469
                                    byteSent = 0;
468
                                    pgs = dState.SEND_EMPTY_DATA;
470
                                    pgs = dState.SEND_EMPTY_DATA;
469
                                }
471
                                }
470
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
472
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
471
                                    // else ..
473
                                    // else ..
472
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
474
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
473
                                    errorOccured = true;
475
                                    errorOccured = true;
474
                                    pgs = dState.SEND_RESET;
476
                                    pgs = dState.SEND_RESET;
475
                                }
477
                                }
476
                            }
478
                            }
477
                        }
479
                        }
478
                        break;
480
                        break;
479
                       
481
                       
480
                    case dState.SEND_EMPTY_DATA:
482
                    case dState.SEND_EMPTY_DATA:
481
                        // Send program data.
483
                        // Send program data.
482
                        data[0] = 0;
484
                        data[0] = 0;
483
                        data[1] = 0;
485
                        data[1] = 0;
484
                        data[2] = 0xff;
486
                        data[2] = 0xff;
485
                        data[3] = 0xff;
487
                        data[3] = 0xff;
486
 
488
 
487
                        outCm = cpn.getPgmDataPacket(data, 4);
489
                        outCm = cpn.getPgmDataPacket(data, 4);
488
                        dc.sendCanPacket(outCm);
490
                        dc.sendCanPacket(outCm);
489
                       
491
                       
490
                        t = Environment.TickCount;
492
                        t = Environment.TickCount;
491
                        t2 = Environment.TickCount;
493
                        t2 = Environment.TickCount;
492
                        byteSent += 6;
494
                        byteSent += 6;
493
                        pgs = dState.WAIT_ACK_EMPTY_DATA;
495
                        pgs = dState.WAIT_ACK_EMPTY_DATA;
494
                        break;
496
                        break;
495
                       
497
                       
496
                       
498
                       
497
                    case dState.WAIT_ACK_EMPTY_DATA:
499
                    case dState.WAIT_ACK_EMPTY_DATA:
498
                        // Wait for pgm ack.
500
                        // Wait for pgm ack.
499
                        if ((Environment.TickCount - t) > 2*TIMEOUT_MS) {
501
                        if ((Environment.TickCount - t) > 2*TIMEOUT_MS) {
500
                            // Woops, error. 
502
                            // Woops, error. 
501
                            Console.WriteLine("Timeout while waiting for data ack.");
503
                            Console.WriteLine("Timeout while waiting for data ack.");
502
                            errorOccured = true;
504
                            errorOccured = true;
503
                            pgs = dState.SEND_RESET;
505
                            pgs = dState.SEND_RESET;
504
                        }
506
                        }
505
                        //Thread.Sleep(1);
507
                        //Thread.Sleep(1);
506
                        // If received ack.
508
                        // If received ack.
507
                        hasMessage = dc.getData(out cm);
509
                        hasMessage = dc.getData(out cm);
508
                        if (hasMessage) {
510
                        if (hasMessage) {
509
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
511
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
510
                            if (cpnin.isNMTPacket()) {
512
                            if (cpnin.isNMTPacket()) {
511
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
513
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
512
                                // If no error
514
                                // If no error
513
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
515
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
514
                                    pgs = dState.SEND_DONE_EMPTY;
516
                                    pgs = dState.SEND_DONE_EMPTY;
515
                                }
517
                                }
516
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
518
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK ) {
517
                                    // else ..
519
                                    // else ..
518
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
520
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
519
                                    errorOccured = true;
521
                                    errorOccured = true;
520
                                    pgs = dState.SEND_RESET;
522
                                    pgs = dState.SEND_RESET;
521
                                }
523
                                }
522
                            }
524
                            }
523
                        }
525
                        }
524
                        break;
526
                        break;
525
                       
527
                       
526
                    case dState.SEND_DONE_EMPTY:
528
                    case dState.SEND_DONE_EMPTY:
527
                        // Send done
529
                        // Send done
528
                        outCm = cpn.getPgmEndPacket();
530
                        outCm = cpn.getPgmEndPacket();
529
                        dc.sendCanPacket(outCm);
531
                        dc.sendCanPacket(outCm);
530
 
532
 
531
                        t = Environment.TickCount;
533
                        t = Environment.TickCount;
532
                        t2 = Environment.TickCount;
534
                        t2 = Environment.TickCount;
533
                        pgs = dState.WAIT_DONE_EMPTY;
535
                        pgs = dState.WAIT_DONE_EMPTY;
534
                        break;
536
                        break;
535
                       
537
                       
536
                       
538
                       
537
                    case dState.WAIT_DONE_EMPTY:
539
                    case dState.WAIT_DONE_EMPTY:
538
                        // Check for timeout, resend done packet in that case..
540
                        // Check for timeout, resend done packet in that case..
539
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
541
                        if ((Environment.TickCount - t) > TIMEOUT_MS) {
540
                            Console.WriteLine("Timeout while waiting for end prg ack.");
542
                            Console.WriteLine("Timeout while waiting for end prg ack.");
541
                            errorOccured = true;
543
                            errorOccured = true;
542
                            pgs = dState.SEND_RESET;
544
                            pgs = dState.SEND_RESET;
543
                        }
545
                        }
544
                        //Thread.Sleep(1);
546
                        //Thread.Sleep(1);
545
                       
547
                       
546
                        hasMessage = dc.getData(out cm);
548
                        hasMessage = dc.getData(out cm);
547
                        if (hasMessage) {
549
                        if (hasMessage) {
548
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
550
                            CanNMT cpnin = new CanNMT(cm, HWID_INUSE);
549
                            if (cpnin.isNMTPacket()) {
551
                            if (cpnin.isNMTPacket()) {
550
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
552
                            //if (cpnin.isNMTPacket() && (cpnin.getSender() == receiveID)) {
551
                                // If no error
553
                                // If no error
552
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
554
                                if (cpnin.getType() == CAN_NMT_PGM_ACK) {
553
                                    Console.WriteLine("");
555
                                    Console.WriteLine("");
554
                                    //
556
                                    //
555
                                    Console.WriteLine("Done, successfully cleaned application flash.");
557
                                    Console.WriteLine("Done, successfully cleaned application flash.");
556
                                    pgs = dState.SEND_RESET;
558
                                    pgs = dState.SEND_RESET;
557
                                }
559
                                }
558
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
560
                                else if (cpnin.getType() == CAN_NMT_PGM_NACK) {
559
                                    // else resend addr..
561
                                    // else resend addr..
560
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
562
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
561
                                    errorOccured = true;
563
                                    errorOccured = true;
562
                                    pgs = dState.SEND_RESET;
564
                                    pgs = dState.SEND_RESET;
563
                                }
565
                                }
564
                            }
566
                            }
565
                        }
567
                        }
566
                        break;                     
568
                        break;                     
567
                    case dState.SEND_RESET:
569
                    case dState.SEND_RESET:
568
                        // Send reset
570
                        // Send reset
569
                        cpn.doReset(dc, receiveID, hwid);
571
                        cpn.doReset(dc, receiveID, hwid);
570
                        t = Environment.TickCount;
572
                        t = Environment.TickCount;
571
                        t2 = Environment.TickCount;
573
                        t2 = Environment.TickCount;
572
                        pgs = dState.DONE;
574
                        pgs = dState.DONE;
573
                        break;
575
                        break;
574
                       
576
                       
575
                       
577
                       
576
                    case dState.DONE:
578
                    case dState.DONE:
577
                        done = true;
579
                        done = true;
578
                        //down.Abort();
580
                        //down.Abort();
579
                        if (!errorOccured) {
581
                        if (!errorOccured) {
580
                            long tsecs = ((Environment.TickCount - timeStart) / 1000);
582
                            long tsecs = ((Environment.TickCount - timeStart) / 1000);
581
                            double bitRate = hf.getLength() / ((double)(Environment.TickCount - timeStart) / 1000.0);
583
                            double bitRate = hf.getLength() / ((double)(Environment.TickCount - timeStart) / 1000.0);
582
                           
584
                           
583
                            if (tsecs <= 0) tsecs = 1;
585
                            if (tsecs <= 0) tsecs = 1;
584
                            long mins = tsecs / 60;
586
                            long mins = tsecs / 60;
585
                            long secs = tsecs % 60;
587
                            long secs = tsecs % 60;
586
                            string pad = "";
588
                            string pad = "";
587
                            if (secs < 10) {
589
                            if (secs < 10) {
588
                                pad = "0";
590
                                pad = "0";
589
                            }
591
                            }
590
                            Console.WriteLine("Time spent: " + mins + ":" + pad + secs + ", " + Math.Round(bitRate, 2) + " bytes/s");
592
                            Console.WriteLine("Time spent: " + mins + ":" + pad + secs + ", " + Math.Round(bitRate, 2) + " bytes/s");
591
                        }
593
                        }
592
                        //Console.Write("> ");
594
                        //Console.Write("> ");
593
                        break;
595
                        break;
594
                }
596
                }
595
            }
597
            }
596
           
598
           
597
        } catch(Exception) {
599
        } catch(Exception) {
598
            Console.WriteLine("Exception occured");
600
            Console.WriteLine("Exception occured");
599
            //Console.WriteLine(e);
601
            //Console.WriteLine(e);
600
            //skriva ut antal bytes skrivna vore bra här, kanske nån annan form av hjälp för debug, något gick snett liksom
602
            //skriva ut antal bytes skrivna vore bra här, kanske nån annan form av hjälp för debug, något gick snett liksom
601
           
603
           
602
        }
604
        }
603
       
605
       
604
        return !errorOccured;
606
        return !errorOccured;
605
    }
607
    }
606
}
608
}
607
 
609