Subversion Repositories HomeAutomation

Rev

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

Rev 455 Rev 456
1
using System;
1
using System;
2
using System.Threading;
2
using System.Threading;
3
using System.Collections.Generic;
3
using System.Collections.Generic;
4
using System.Text;
4
using System.Text;
5
 
5
 
6
class Program {
6
class Program {
7
    const byte DEBUG_LEVEL = 3; //0, 1, 2, 3
7
    const byte DEBUG_LEVEL = 3; //0, 1, 2, 3
8
    //static SerialConnection sc;
8
    //static SerialConnection sc;
9
    static DaemonConnection dc;
9
    static DaemonConnection dc;
10
    static Arguments CommandLine;
10
    static Arguments CommandLine;
11
    //static TcpServer tcps;
11
    //static TcpServer tcps;
12
    static Downloader dl;
12
    static Downloader dl;
13
   
13
   
14
    //private const byte CAN_NMT = 0x00;
14
    //private const byte CAN_NMT = 0x00;
15
   
15
   
16
    //private const byte CAN_NMT_RESET = 0x04;
16
    //private const byte CAN_NMT_RESET = 0x04;
17
    //private const byte CAN_NMT_START_APP  = 0x24;
17
    //private const byte CAN_NMT_START_APP  = 0x24;
18
    //private const byte CAN_NMT_APP_START  = 0x28;
18
    //private const byte CAN_NMT_APP_START  = 0x28;
19
    private const byte MY_ID = 0x00;
19
    private const byte MY_ID = 0x00;
20
    private const byte MY_NID = 0x0;
20
    private const byte MY_NID = 0x0;
21
 
21
 
22
    private static HexFile hf;
22
    private static HexFile hf;
23
    static bool aBios=false;
23
    static bool aBios=false;
24
    static bool aReset=false;
24
    static bool aReset=false;
25
    static bool aStart=false;
25
    static bool aStart=false;
26
    static bool aTerminal=false;
26
    static bool aTerminal=false;
27
       
27
       
28
    static bool sNodeid=false;
28
    static bool sNodeid=false;
29
    static bool sHexfile=false;
29
    static bool sHexfile=false;
30
       
30
       
31
    static string hexfile=null;
31
    static string hexfile=null;
32
    static byte nodeid=0;
32
    static byte nodeid=0;
33
    static string host=null;
33
    static string host=null;
34
    static int port=1200;
34
    static int port=1200;
35
   
35
   
36
    static void Main(string[] args) {
36
    static void Main(string[] args) {
37
 
37
 
38
        bool error = false;
38
        bool error = false;
39
        if (args.Length < 4) {
39
        if (args.Length < 4) {
40
            Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>");
40
            Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>");
41
            Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200.");
41
            Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200.");
42
            Console.WriteLine("Options:");
42
            Console.WriteLine("Options:");
43
            Console.WriteLine("  -f <hexfile>   Specify a file to be loaded to target.");
43
            Console.WriteLine("  -f <hexfile>   Specify a file to be loaded to target.");
44
            Console.WriteLine("  -n <nodeid>    Id of target node, hex or decimal.");
44
            Console.WriteLine("  -n <nodeid>    Id of target node, hex or decimal.");
45
            Console.WriteLine("  -t             Enter terminal mode.");
45
            Console.WriteLine("  -t             Enter terminal mode.");
46
            Console.WriteLine("  -r             Reset node before loading target.");
46
            Console.WriteLine("  -r             Reset node before loading target.");
47
            Console.WriteLine("  -s             Start node after loading target.");
47
            Console.WriteLine("  -s             Start node after loading target.");
48
            Console.WriteLine("  -b             Program hex as bios, use with caution.");
48
            Console.WriteLine("  -b             Program hex as bios, use with caution.");
49
            error = true;
49
            error = true;
50
            return;
50
            return;
51
        }
51
        }
52
 
52
 
53
        if (!error) {
53
        if (!error) {
54
            //Console.WriteLine("arg0: " + args[0] + "\n");
54
            //Console.WriteLine("arg0: " + args[0] + "\n");
55
            //argPort = args[0];
55
            //argPort = args[0];
56
            //argBaud = Int32.Parse(args[1]);
56
            //argBaud = Int32.Parse(args[1]);
57
            CommandLine=new Arguments(args);
57
            CommandLine=new Arguments(args);
58
           
58
           
59
            if (CommandLine["b"] == "true") { aBios = true; }
59
            if (CommandLine["b"] == "true") { aBios = true; }
60
            if (CommandLine["r"] == "true") { aReset = true; }
60
            if (CommandLine["r"] == "true") { aReset = true; }
61
            if (CommandLine["s"] == "true") { aStart = true; }
61
            if (CommandLine["s"] == "true") { aStart = true; }
62
            if (CommandLine["t"] == "true") { aTerminal = true; }
62
            if (CommandLine["t"] == "true") { aTerminal = true; }
63
 
63
 
64
            hexfile = CommandLine["f"];
64
            hexfile = CommandLine["f"];
65
           
65
           
66
            if (hexfile != null) {
66
            if (hexfile != null) {
67
                sHexfile = true;
67
                sHexfile = true;
68
            }
68
            }
69
            if (!aTerminal && !sHexfile) {
69
            if (!aTerminal && !sHexfile) {
70
                Console.WriteLine("When not in terminal mode a hexfile must be supplied");
70
                Console.WriteLine("When not in terminal mode a hexfile must be supplied");
71
                error = true;
71
                error = true;
72
            }
72
            }
73
           
73
           
74
            string node = CommandLine["n"];
74
            string node = CommandLine["n"];
75
           
75
           
-
 
76
            if (node != null) {
76
            if (!parseNodeId(node)) {
77
                if (!parseNodeId(node)) {
77
                error = true;
78
                    error = true;
-
 
79
                }
78
            }
80
            }
79
           
81
           
80
            if (!aTerminal && !sNodeid) {
82
            if (!aTerminal && !sNodeid) {
81
                Console.WriteLine("When not in terminal mode a nodeid must be supplied");
83
                Console.WriteLine("When not in terminal mode a nodeid must be supplied");
82
                error = true;
84
                error = true;
83
            }
85
            }
84
           
86
           
85
            host = CommandLine["h"];
87
            host = CommandLine["h"];
86
            string sPort = CommandLine["p"];
88
            string sPort = CommandLine["p"];
87
            if ((host == null) || (sPort.Length == 0)) {
89
            if ((host == null) || (sPort.Length == 0)) {
88
                error = true;
90
                error = true;
89
            } else {
91
            } else {
90
                try {
92
                try {
91
                    port = int.Parse(sPort);
93
                    port = int.Parse(sPort);
92
                } catch {
94
                } catch {
93
                    error = true;
95
                    error = true;
94
                }
96
                }
95
            }
97
            }
96
        }
98
        }
97
       
99
       
98
        if  (!error) {
100
        if  (!error) {
99
            //commandline feedback output (use debuglevel for hiding these)
101
            //commandline feedback output (use debuglevel for hiding these)
100
            Console.WriteLine("canDude settings:");
102
            Console.WriteLine("canDude settings:");
101
            Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid));
103
            if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
102
            Console.WriteLine("Host: {0}:{1}", host, port);
104
            Console.WriteLine("Host: {0}:{1}", host, port);
103
            if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
105
            if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
104
            Console.Write("Parameters: ");
106
            Console.Write("Parameters: ");
105
            if (aBios) { Console.Write("Bios "); }
107
            if (aBios) { Console.Write("Bios "); }
106
            if (aReset) { Console.Write("Reset "); }
108
            if (aReset) { Console.Write("Reset "); }
107
            if (aStart) { Console.Write("Start "); }
109
            if (aStart) { Console.Write("Start "); }
108
            if (aTerminal) { Console.Write("Terminal"); }
110
            if (aTerminal) { Console.Write("Terminal"); }
109
            Console.WriteLine("");
111
            Console.WriteLine("");
110
        }
112
        }
111
       
113
       
112
        if (!error && aTerminal) {
114
        if (!error && aTerminal) {
113
            bool success = true;
115
            bool success = true;
114
           
116
           
115
            dc = new DaemonConnection();
117
            dc = new DaemonConnection();
116
            if (dc.init(host, port)) {
118
            if (dc.init(host, port)) {
117
                Console.WriteLine("Connected to candaemon");
119
                Console.WriteLine("Connected to candaemon");
118
            } else {
120
            } else {
119
                Console.WriteLine("Connection to candaemon could not be established, I quit");
121
                Console.WriteLine("Connection to candaemon could not be established, I quit");
120
                success = false;
122
                success = false;
121
            }
123
            }
122
           
124
           
123
            hf = new HexFile();
125
            hf = new HexFile();
124
            if (sHexfile && success) {
126
            if (sHexfile && success) {
125
                if (hf.loadHex(hexfile)) {
127
                if (hf.loadHex(hexfile)) {
126
                    Console.WriteLine("Hexfile loaded");
128
                    Console.WriteLine("Hexfile loaded");
127
                } else { success = false; }
129
                } else { success = false; }
128
            }
130
            }
129
           
131
           
130
            if (success) {
132
            if (success) {
131
                if (DEBUG_LEVEL>0) {
133
                if (DEBUG_LEVEL>0) {
132
                    Console.WriteLine("");
134
                    Console.WriteLine("");
133
                    Console.WriteLine("canDude commands:");
135
                    Console.WriteLine("canDude commands:");
134
                    //Console.WriteLine("reset - reset communication.");
136
                    //Console.WriteLine("reset - reset communication.");
135
                    Console.WriteLine("exit             - exit program");
137
                    Console.WriteLine("exit             - exit program");
136
                    Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile");
138
                    Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile");
137
                    Console.WriteLine("node <nodeid>    - specify id of target node, hex or decimal");
139
                    Console.WriteLine("node <nodeid>    - specify id of target node, hex or decimal");
138
                    Console.WriteLine("reset            - reset node");
140
                    Console.WriteLine("reset            - reset node");
139
                    Console.WriteLine("start            - start application in node (no feedback yet)");
141
                    Console.WriteLine("start            - start application in node (no feedback yet)");
140
                    Console.WriteLine("go               - start download application to target");
142
                    Console.WriteLine("go               - start download application to target");
141
                    Console.WriteLine("go bios          - start download bios to target, use with caution");
143
                    Console.WriteLine("go bios          - start download bios to target, use with caution");
142
                    Console.WriteLine("");
144
                    Console.WriteLine("");
143
                    //Thread.Sleep(1000);
145
                    //Thread.Sleep(1000);
144
                }
146
                }
145
               
147
               
146
                string instr;
148
                string instr;
147
                do {
149
                do {
148
                    Console.Write("> ");
150
                    Console.Write("> ");
149
                    instr = Console.ReadLine().ToLower();
151
                    instr = Console.ReadLine();
150
                } while (parseInput(instr));
152
                } while (parseInput(instr));
151
               
153
               
152
                //exit command
154
                //exit command
153
                dc.stop();
155
                dc.stop();
154
            }
156
            }
155
        }
157
        }
156
 
158
 
157
        if (!error && !aTerminal) {
159
        if (!error && !aTerminal) {
158
            //not terminal mode
160
            //not terminal mode
159
            bool success = true;
161
            bool success = true;
160
           
162
           
161
            //load hexfile (a hexfile must be supplied as commandline argument)
163
            //load hexfile (a hexfile must be supplied as commandline argument)
162
            hf = new HexFile();
164
            hf = new HexFile();
163
            if (hf.loadHex(hexfile)) {
165
            if (hf.loadHex(hexfile)) {
164
                Console.WriteLine("Hexfile loaded");
166
                Console.WriteLine("Hexfile loaded");
165
            } else {
167
            } else {
166
                success = false;
168
                success = false;
167
                Console.WriteLine("Hexfile could not be loaded, I quit");
169
                Console.WriteLine("Hexfile could not be loaded, I quit");
168
            }
170
            }
169
           
171
           
170
            //connect to candaemon
172
            //connect to candaemon
171
            dc = new DaemonConnection();
173
            dc = new DaemonConnection();
172
            if (success) {
174
            if (success) {
173
                if (dc.init(host, port)) {
175
                if (dc.init(host, port)) {
174
                    Console.WriteLine("Connected to candaemon");
176
                    Console.WriteLine("Connected to candaemon");
175
                } else {
177
                } else {
176
                    Console.WriteLine("Connection to candaemon could not be established, I quit");
178
                    Console.WriteLine("Connection to candaemon could not be established, I quit");
177
                    success = false;
179
                    success = false;
178
                }
180
                }
179
            }
181
            }
180
           
182
           
181
            //if a hexfile is loaded and we are connected to candaemon then start autodownloading sequence
183
            //if a hexfile is loaded and we are connected to candaemon then start autodownloading sequence
182
            if (success) {
184
            if (success) {
183
                bool autosuccess = true;
185
                bool autosuccess = true;
184
               
186
               
185
                CanNMT cpn = new CanNMT();
187
                CanNMT cpn = new CanNMT();
186
                if (aReset) {
188
                if (aReset) {
187
                    //send a reset command (and wait for feedback)
189
                    //send a reset command (and wait for feedback)
188
                    if (!cpn.doReset(dc, nodeid)) {
190
                    if (!cpn.doReset(dc, nodeid)) {
189
                        Console.WriteLine("Target node did not respond to reset, I quit");
191
                        Console.WriteLine("Target node did not respond to reset, I quit");
190
                        autosuccess = false;
192
                        autosuccess = false;
191
                    }
193
                    }
192
                }
194
                }
193
               
195
               
194
                if (autosuccess) {
196
                if (autosuccess) {
195
                    //send application
197
                    //send application
196
                    Console.WriteLine("Programming not implemented yet, I quit");
198
                    Console.WriteLine("Programming not implemented yet, I quit");
197
                    autosuccess = false;
199
                    autosuccess = false;
198
                }
200
                }
199
               
201
               
200
                if (autosuccess && aStart) {
202
                if (autosuccess && aStart) {
201
                    //start applikation
203
                    //start applikation
202
                    cpn.doStart(dc, nodeid);
204
                    cpn.doStart(dc, nodeid);
203
                }
205
                }
204
               
206
               
205
            }
207
            }
206
        }
208
        }
207
    }
209
    }
208
   
210
   
209
    static private bool parseNodeId(string node) {
211
    static private bool parseNodeId(string node) {
210
        sNodeid = false;
212
        sNodeid = false;
211
        if (node.Length > 0) {
213
        if (node.Length > 0) {
212
            try {
214
            try {
213
                nodeid = byte.Parse(node);
215
                nodeid = byte.Parse(node);
214
                sNodeid = true;
216
                sNodeid = true;
215
            } catch {}
217
            } catch {}
216
        }
218
        }
217
        if (node.Length > 2) {
219
        if (node.Length > 2) {
218
            if (node.Substring(0, 2).Equals("0x")) {
220
            if (node.Substring(0, 2).Equals("0x")) {
219
                if (node.Length == 3) {
221
                if (node.Length == 3) {
220
                    try {
222
                    try {
221
                        nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
223
                        nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
222
                        sNodeid = true;
224
                        sNodeid = true;
223
                    } catch {
225
                    } catch {
224
                        Console.WriteLine("nodeadress error!");
226
                        Console.WriteLine("nodeadress error!");
225
                        return false;
227
                        return false;
226
                    }
228
                    }
227
                } else if (node.Length == 4) {
229
                } else if (node.Length == 4) {
228
                    try {
230
                    try {
229
                        nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
231
                        nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
230
                        sNodeid = true;
232
                        sNodeid = true;
231
                    } catch {
233
                    } catch {
232
                        Console.WriteLine("nodeadress error!");
234
                        Console.WriteLine("nodeadress error!");
233
                        return false;
235
                        return false;
234
                    }
236
                    }
235
                } else {
237
                } else {
236
                    Console.WriteLine("nodeadress error!");
238
                    Console.WriteLine("nodeadress error!");
237
                    return false;
239
                    return false;
238
                }
240
                }
239
            }
241
            }
240
        }
242
        }
241
       
243
       
242
        return true;
244
        return true;
243
    }
245
    }
244
   
246
   
245
    static private bool parseInput(string instr) {
247
    static private bool parseInput(string instr) {
246
        if (instr.Equals("reset")) {
248
        if (instr.Equals("reset")) {
247
            if (sNodeid) {
249
            if (sNodeid) {
248
                CanNMT cpn = new CanNMT();
250
                CanNMT cpn = new CanNMT();
249
                cpn.doReset(dc, nodeid);
251
                cpn.doReset(dc, nodeid);
250
            } else {
252
            } else {
251
                Console.WriteLine("No nodeid has been specified");
253
                Console.WriteLine("No nodeid has been specified");
252
            }
254
            }
253
        }
255
        }
254
        else if (instr.Equals("start")) {
256
        else if (instr.Equals("start")) {
255
            if (sNodeid) {
257
            if (sNodeid) {
256
                CanNMT cpn = new CanNMT();
258
                CanNMT cpn = new CanNMT();
257
                cpn.doStart(dc, nodeid);
259
                cpn.doStart(dc, nodeid);
258
            } else {
260
            } else {
259
                Console.WriteLine("No nodeid has been specified");
261
                Console.WriteLine("No nodeid has been specified");
260
            }
262
            }
261
        }
263
        }
262
        else if (instr.StartsWith("go")) {
264
        else if (instr.StartsWith("go")) {
263
            bool dlBios = false;
265
            bool dlBios = false;
264
            bool error = true;
266
            bool error = true;
265
            if (instr.Equals("go bios")) {
267
            if (instr.Equals("go bios")) {
266
                dlBios = true;
268
                dlBios = true;
267
                error = false;
269
                error = false;
268
            } else if (instr.Equals("go")) {
270
            } else if (instr.Equals("go")) {
269
                error = false;
271
                error = false;
270
            }
272
            }
271
           
273
           
272
            if (!sNodeid) {
274
            if (!sNodeid) {
273
                Console.WriteLine("No nodeid has been specified");
275
                Console.WriteLine("No nodeid has been specified");
274
                error = true;
276
                error = true;
275
            }
277
            }
276
            if (!sHexfile) {
278
            if (!sHexfile) {
277
                Console.WriteLine("No hexfile has been specified");
279
                Console.WriteLine("No hexfile has been specified");
278
                error = true;
280
                error = true;
279
            }
281
            }
280
           
282
           
281
            CanNMT cpn = new CanNMT();
283
            CanNMT cpn = new CanNMT();
282
            if (!error && aReset) {
284
            if (!error && aReset) {
283
                //send a reset command (and wait for feedback)
285
                //send a reset command (and wait for feedback)
284
                if (!cpn.doReset(dc, nodeid)) {
286
                if (!cpn.doReset(dc, nodeid)) {
285
                    Console.WriteLine("Target node did not respond to reset");
287
                    Console.WriteLine("Target node did not respond to reset");
286
                    error = true;
288
                    error = true;
287
                }
289
                }
288
            }
290
            }
289
               
291
               
290
            if (!error) {
292
            if (!error) {
291
                //send application
293
                //send application
292
                dl = new Downloader(hf, dc, nodeid, dlBios);
294
                dl = new Downloader(hf, dc, nodeid, dlBios);
293
                if (!dl.go()) {
295
                if (!dl.go()) {
294
                    Console.WriteLine("Error occured during download");
296
                    Console.WriteLine("Error occured during download");
295
                    error = true;
297
                    error = true;
296
                }
298
                }
297
            }
299
            }
298
               
300
               
299
            if (!error && aStart) {
301
            if (!error && aStart) {
300
                //start applikation
302
                //start applikation
301
                cpn.doStart(dc, nodeid);
303
                cpn.doStart(dc, nodeid);
302
            }
304
            }
303
           
305
           
304
        }
306
        }
305
        else if (instr.StartsWith("load")) {
307
        else if (instr.StartsWith("load")) {
306
            if (instr.Length > 5) {
308
            if (instr.Length > 5) {
307
                //second argument is a hexfile, this file should be loaded
309
                //second argument is a hexfile, this file should be loaded
308
                hexfile = instr.Substring(5);
310
                hexfile = instr.Substring(5);
309
                if (hf.loadHex(hexfile)) {
311
                if (hf.loadHex(hexfile)) {
310
                    sHexfile = true;
312
                    sHexfile = true;
311
                    Console.WriteLine("Hexfile loaded");
313
                    Console.WriteLine("Hexfile loaded");
312
                } else {
314
                } else {
313
                    Console.WriteLine("Hexfile could not be loaded");
315
                    Console.WriteLine("Hexfile could not be loaded");
314
                }
316
                }
315
            } else {
317
            } else {
316
                //reload current hexfile. if a hexfile already has been specified
318
                //reload current hexfile. if a hexfile already has been specified
317
                if (sHexfile) {
319
                if (sHexfile) {
318
                    if (hf.loadHex(hexfile)) {
320
                    if (hf.loadHex(hexfile)) {
319
                        Console.WriteLine("Hexfile reloaded");
321
                        Console.WriteLine("Hexfile reloaded");
320
                    } else {
322
                    } else {
321
                        Console.WriteLine("Hexfile could not be reloaded");
323
                        Console.WriteLine("Hexfile could not be reloaded");
322
                    }
324
                    }
323
                } else {
325
                } else {
324
                    Console.WriteLine("No hexfile has been specified");
326
                    Console.WriteLine("No hexfile has been specified");
325
                }
327
                }
326
               
328
               
327
            }
329
            }
328
        }
330
        }
329
        else if (instr.StartsWith("node")) {
331
        else if (instr.StartsWith("node")) {
330
            //change nodeid
332
            //change nodeid
331
            if (instr.Length > 5) {
333
            if (instr.Length > 5) {
332
                //second argument is nodeid
334
                //second argument is nodeid
333
                string node = instr.Substring(5);
335
                string node = instr.Substring(5);
334
                parseNodeId(node);
336
                parseNodeId(node);
335
            }
337
            }
336
            Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid));
338
            Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid));
337
        }
339
        }
338
        else if (instr.Equals("exit")) {
340
        else if (instr.Equals("exit")) {
339
            return false;
341
            return false;
340
        }
342
        }
341
        else {
343
        else {
342
            Console.WriteLine("Unknown command.");
344
            Console.WriteLine("Unknown command.");
343
        }
345
        }
344
       
346
       
345
        return true;
347
        return true;
346
    }
348
    }
347
   
349
   
348
}
350
}
349
 
351