Rev 473 | Rev 655 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 473 | Rev 586 | ||
|---|---|---|---|
| 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 | ///TODO: |
6 | ///TODO: |
| 7 | // argument -v # (debuglevel) |
7 | // argument -v # (debuglevel) |
| 8 | // argument till konstruktor på alla objekt: debuglevel |
8 | // argument till konstruktor på alla objekt: debuglevel |
| 9 | 9 | ||
| 10 | class Program { |
10 | class Program { |
| 11 | const byte DEBUG_LEVEL = 3; //0, 1, 2, 3 |
11 | const byte DEBUG_LEVEL = 3; //0, 1, 2, 3 |
| 12 | 12 | ||
| 13 | //connects to candaemon on tcp to send and receive packets |
13 | //connects to candaemon on tcp to send and receive packets |
| 14 | static DaemonConnection dc; |
14 | static DaemonConnection dc; |
| 15 | //a commandline arguments parser |
15 | //a commandline arguments parser |
| 16 | static Arguments CommandLine; |
16 | static Arguments CommandLine; |
| 17 | //functions for downloading hex to target |
17 | //functions for downloading hex to target |
| 18 | static Downloader dl; |
18 | static Downloader dl; |
| 19 | //maintains a hexfile |
19 | //maintains a hexfile |
| 20 | private static HexFile hf; |
20 | private static HexFile hf; |
| 21 | 21 | ||
| 22 | static bool aBios=false; //if commandline says it is a bios update |
22 | static bool aBios=false; //if commandline says it is a bios update |
| 23 | static bool aReset=false; //if commandline says a reset should be sent before downloading to target |
23 | static bool aReset=false; //if commandline says a reset should be sent before downloading to target |
| 24 | static bool aStart=false; //if commandline says a start should be sent after downloading to target |
24 | static bool aStart=false; //if commandline says a start should be sent after downloading to target |
| 25 | static bool aTerminal=false; //if commandline says we should start in interactive mode |
25 | static bool aTerminal=false; //if commandline says we should start in interactive mode |
| 26 | 26 | ||
| 27 | static bool sNodeid=false; //true when a nodeid has been parsed |
27 | static bool sNodeid=false; //true when a nodeid has been parsed |
| 28 | static bool sHexfile=false; //true when a hexfile has been specified |
28 | static bool sHexfile=false; //true when a hexfile has been specified |
| 29 | 29 | ||
| 30 | static string hexfile=null; //path to a hexfile |
30 | static string hexfile=null; //path to a hexfile |
| 31 | static byte nodeid=0; //id of current node |
31 | static byte nodeid=0; //id of current node |
| 32 | static string host=null; //address of candaemon-host |
32 | static string host=null; //address of candaemon-host |
| 33 | static int port=1200; //port of candaemon-host |
33 | static int port=1200; //port of candaemon-host |
| 34 | 34 | ||
| 35 | static void Main(string[] args) { |
35 | static void Main(string[] args) { |
| 36 | bool error = false; |
36 | bool error = false; |
| 37 | if (args.Length < 4) { |
37 | if (args.Length < 4) { |
| 38 | //if to few arguments then print info and quit |
38 | //if to few arguments then print info and quit |
| 39 | printSyntax(); |
39 | printSyntax(); |
| 40 | error = true; |
40 | error = true; |
| 41 | } |
41 | } |
| 42 | 42 | ||
| 43 | if (!error) { |
43 | if (!error) { |
| 44 | //parse commandline arguments |
44 | //parse commandline arguments |
| 45 | CommandLine=new Arguments(args); |
45 | CommandLine=new Arguments(args); |
| 46 | 46 | ||
| 47 | //check single arguments |
47 | //check single arguments |
| 48 | if (CommandLine["b"] == "true") { aBios = true; } |
48 | if (CommandLine["b"] == "true") { aBios = true; } |
| 49 | if (CommandLine["r"] == "true") { aReset = true; } |
49 | if (CommandLine["r"] == "true") { aReset = true; } |
| 50 | if (CommandLine["s"] == "true") { aStart = true; } |
50 | if (CommandLine["s"] == "true") { aStart = true; } |
| 51 | if (CommandLine["t"] == "true") { aTerminal = true; } |
51 | if (CommandLine["t"] == "true") { aTerminal = true; } |
| 52 | 52 | ||
| 53 | //check hexfile argument |
53 | //check hexfile argument |
| 54 | hexfile = CommandLine["f"]; |
54 | hexfile = CommandLine["f"]; |
| 55 | if (hexfile != null) { |
55 | if (hexfile != null) { |
| 56 | sHexfile = true; |
56 | sHexfile = true; |
| 57 | } |
57 | } |
| 58 | if (!aTerminal && !sHexfile && !aReset && !aStart) { |
58 | if (!aTerminal && !sHexfile && !aReset && !aStart) { |
| 59 | if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a hexfile, a reset or a start parameter must be supplied, I quit"); } |
59 | if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a hexfile, a reset or a start parameter must be supplied, I quit"); } |
| 60 | error = true; |
60 | error = true; |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | //check nodeid argument |
63 | //check nodeid argument |
| 64 | string node = CommandLine["n"]; |
64 | string node = CommandLine["n"]; |
| 65 | if (node != null) { |
65 | if (node != null) { |
| 66 | if (!parseNodeId(node)) { |
66 | if (!parseNodeId(node)) { |
| 67 | error = true; |
67 | error = true; |
| 68 | } |
68 | } |
| 69 | } |
69 | } |
| 70 | if (!aTerminal && !sNodeid) { |
70 | if (!aTerminal && !sNodeid) { |
| 71 | if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid must be supplied, I quit"); } |
71 | if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid must be supplied, I quit"); } |
| 72 | error = true; |
72 | error = true; |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | //check host and port arguments |
75 | //check host and port arguments |
| 76 | host = CommandLine["h"]; |
76 | host = CommandLine["h"]; |
| 77 | string sPort = CommandLine["p"]; |
77 | string sPort = CommandLine["p"]; |
| 78 | if ((host == null) || (sPort.Length == 0)) { |
78 | if ((host == null) || (sPort.Length == 0)) { |
| 79 | if (DEBUG_LEVEL>0) { printSyntax(); } |
79 | if (DEBUG_LEVEL>0) { printSyntax(); } |
| 80 | error = true; |
80 | error = true; |
| 81 | } else { |
81 | } else { |
| 82 | try { |
82 | try { |
| 83 | port = int.Parse(sPort); |
83 | port = int.Parse(sPort); |
| 84 | } catch { |
84 | } catch { |
| 85 | if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); } |
85 | if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); } |
| 86 | error = true; |
86 | error = true; |
| 87 | } |
87 | } |
| 88 | } |
88 | } |
| 89 | } |
89 | } |
| 90 | 90 | ||
| 91 | if (!error) { |
91 | if (!error) { |
| 92 | //commandline feedback output |
92 | //commandline feedback output |
| 93 | if (DEBUG_LEVEL>2) { |
93 | if (DEBUG_LEVEL>2) { |
| 94 | Console.WriteLine("canDude settings:"); |
94 | Console.WriteLine("canDude settings:"); |
| 95 | if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); } |
95 | if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); } |
| 96 | Console.WriteLine("Host: {0}:{1}", host, port); |
96 | Console.WriteLine("Host: {0}:{1}", host, port); |
| 97 | if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); } |
97 | if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); } |
| 98 | Console.Write("Parameters: "); |
98 | Console.Write("Parameters: "); |
| 99 | if (aBios) { Console.Write("Bios "); } |
99 | if (aBios) { Console.Write("Bios "); } |
| 100 | if (aReset) { Console.Write("Reset "); } |
100 | if (aReset) { Console.Write("Reset "); } |
| 101 | if (aStart) { Console.Write("Start "); } |
101 | if (aStart) { Console.Write("Start "); } |
| 102 | if (aTerminal) { Console.Write("Terminal"); } |
102 | if (aTerminal) { Console.Write("Terminal"); } |
| 103 | Console.WriteLine(""); |
103 | Console.WriteLine(""); |
| 104 | } |
104 | } |
| 105 | } |
105 | } |
| 106 | 106 | ||
| 107 | if (!error) { |
107 | if (!error) { |
| 108 | //connect to candaemon |
108 | //connect to candaemon |
| 109 | dc = new DaemonConnection(); |
109 | dc = new DaemonConnection(); |
| 110 | if (dc.init(host, port)) { |
110 | if (dc.init(host, port)) { |
| 111 | if (DEBUG_LEVEL>1) { Console.WriteLine("Connected to candaemon"); } |
111 | if (DEBUG_LEVEL>1) { Console.WriteLine("Connected to candaemon"); } |
| 112 | } else { |
112 | } else { |
| 113 | if (DEBUG_LEVEL>0) { Console.WriteLine("Connection to candaemon could not be established, I quit"); } |
113 | if (DEBUG_LEVEL>0) { Console.WriteLine("Connection to candaemon could not be established, I quit"); } |
| 114 | error = true; |
114 | error = true; |
| 115 | } |
115 | } |
| - | 116 | Thread.Sleep(100); |
|
| 116 | } |
117 | } |
| 117 | 118 | ||
| 118 | if (!error && aTerminal) { |
119 | if (!error && aTerminal) { |
| 119 | //terminal mode (==interactive mode) |
120 | //terminal mode (==interactive mode) |
| 120 | bool success = true; |
121 | bool success = true; |
| 121 | //load hexfile if one has been specified as commandline argument |
122 | //load hexfile if one has been specified as commandline argument |
| 122 | hf = new HexFile(); |
123 | hf = new HexFile(); |
| 123 | if (sHexfile && success) { |
124 | if (sHexfile && success) { |
| 124 | if (hf.loadHex(hexfile)) { |
125 | if (hf.loadHex(hexfile)) { |
| 125 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
126 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 126 | } else { success = false; } |
127 | } else { success = false; } |
| 127 | } |
128 | } |
| 128 | 129 | ||
| 129 | if (success) { |
130 | if (success) { |
| 130 | if (DEBUG_LEVEL>1) { |
131 | if (DEBUG_LEVEL>1) { |
| 131 | Console.WriteLine(""); |
132 | Console.WriteLine(""); |
| 132 | printHelp(); //print available commands |
133 | printHelp(); //print available commands |
| 133 | Console.WriteLine(""); |
134 | Console.WriteLine(""); |
| 134 | } |
135 | } |
| 135 | 136 | ||
| 136 | string instr; |
137 | string instr; |
| 137 | do { |
138 | do { |
| 138 | Console.Write("> "); //a command has been executed, print a new console char |
139 | Console.Write("> "); //a command has been executed, print a new console char |
| 139 | instr = Console.ReadLine(); //read std in |
140 | instr = Console.ReadLine(); //read std in |
| 140 | } while (parseInput(instr)); //parse a line from std in |
141 | } while (parseInput(instr)); //parse a line from std in |
| 141 | 142 | ||
| 142 | //exit command |
143 | //exit command |
| 143 | } |
144 | } |
| 144 | } |
145 | } |
| 145 | 146 | ||
| 146 | if (!error && !aTerminal) { |
147 | if (!error && !aTerminal) { |
| 147 | //not terminal mode |
148 | //not terminal mode |
| 148 | bool success = true; |
149 | bool success = true; |
| 149 | 150 | ||
| 150 | if (sHexfile) { |
151 | if (sHexfile) { |
| 151 | //load hexfile (a hexfile must be supplied as commandline argument) |
152 | //load hexfile (a hexfile must be supplied as commandline argument) |
| 152 | hf = new HexFile(); |
153 | hf = new HexFile(); |
| 153 | if (hf.loadHex(hexfile)) { |
154 | if (hf.loadHex(hexfile)) { |
| 154 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
155 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 155 | } else { |
156 | } else { |
| 156 | success = false; |
157 | success = false; |
| 157 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); } |
158 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); } |
| 158 | } |
159 | } |
| 159 | } |
160 | } |
| 160 | 161 | ||
| 161 | //if a hexfile is loaded then start autodownloading sequence |
162 | //if a hexfile is loaded then start autodownloading sequence |
| 162 | if (success) { |
163 | if (success) { |
| 163 | bool autosuccess = true; |
164 | bool autosuccess = true; |
| 164 | 165 | ||
| 165 | CanNMT cpn = new CanNMT(); |
166 | CanNMT cpn = new CanNMT(); |
| 166 | if (aReset) { |
167 | if (aReset) { |
| 167 | //send a reset command (and wait for feedback) |
168 | //send a reset command (and wait for feedback) |
| 168 | if (!cpn.doReset(dc, nodeid)) { |
169 | if (!cpn.doReset(dc, nodeid)) { |
| 169 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); } |
170 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); } |
| 170 | autosuccess = false; |
171 | autosuccess = false; |
| 171 | } |
172 | } |
| 172 | } |
173 | } |
| 173 | 174 | ||
| 174 | if (sHexfile && autosuccess) { |
175 | if (sHexfile && autosuccess) { |
| 175 | //send application |
176 | //send application |
| 176 | dl = new Downloader(hf, dc, nodeid, aBios); |
177 | dl = new Downloader(hf, dc, nodeid, aBios); |
| 177 | if (!dl.go()) { |
178 | if (!dl.go()) { |
| 178 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
179 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 179 | autosuccess = false; |
180 | autosuccess = false; |
| 180 | } |
181 | } |
| 181 | } |
182 | } |
| 182 | 183 | ||
| 183 | if (autosuccess && aStart) { |
184 | if (autosuccess && aStart) { |
| 184 | //start application |
185 | //start application |
| 185 | cpn.doStart(dc, nodeid); |
186 | cpn.doStart(dc, nodeid); |
| 186 | } |
187 | } |
| 187 | 188 | ||
| 188 | } |
189 | } |
| 189 | } |
190 | } |
| 190 | if (dc != null) { |
191 | if (dc != null) { |
| 191 | //stop tcp client thread |
192 | //stop tcp client thread |
| 192 | dc.stop(); |
193 | dc.stop(); |
| 193 | } |
194 | } |
| 194 | } |
195 | } |
| 195 | 196 | ||
| 196 | static private void printSyntax() { |
197 | static private void printSyntax() { |
| 197 | Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>"); |
198 | Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>"); |
| 198 | Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200."); |
199 | Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200."); |
| 199 | Console.WriteLine("Options:"); |
200 | Console.WriteLine("Options:"); |
| 200 | Console.WriteLine(" -f <hexfile> Specify a file to be loaded to target."); |
201 | Console.WriteLine(" -f <hexfile> Specify a file to be loaded to target."); |
| 201 | Console.WriteLine(" -n <nodeid> Id of target node, hex or decimal."); |
202 | Console.WriteLine(" -n <nodeid> Id of target node, hex or decimal."); |
| 202 | Console.WriteLine(" -t Enter terminal mode."); |
203 | Console.WriteLine(" -t Enter terminal mode."); |
| 203 | Console.WriteLine(" -r Reset node before loading target."); |
204 | Console.WriteLine(" -r Reset node before loading target."); |
| 204 | Console.WriteLine(" -s Start node after loading target."); |
205 | Console.WriteLine(" -s Start node after loading target."); |
| 205 | Console.WriteLine(" -b Program hex as bios, use with caution."); |
206 | Console.WriteLine(" -b Program hex as bios, use with caution."); |
| 206 | } |
207 | } |
| 207 | 208 | ||
| 208 | static private void printHelp() { |
209 | static private void printHelp() { |
| 209 | Console.WriteLine("canDude commands:"); |
210 | Console.WriteLine("canDude commands:"); |
| 210 | Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile"); |
211 | Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile"); |
| 211 | Console.WriteLine("node <nodeid> - specify id of target node, hex or decimal"); |
212 | Console.WriteLine("node <nodeid> - specify id of target node, hex or decimal"); |
| 212 | Console.WriteLine("reset - reset node"); |
213 | Console.WriteLine("reset - reset node"); |
| 213 | Console.WriteLine("start - start application in node (no feedback yet)"); |
214 | Console.WriteLine("start - start application in node (no feedback yet)"); |
| 214 | Console.WriteLine("go - start download application to target"); |
215 | Console.WriteLine("go - start download application to target"); |
| 215 | Console.WriteLine("go bios - start download bios to target, use with caution"); |
216 | Console.WriteLine("go bios - start download bios to target, use with caution"); |
| 216 | Console.WriteLine("help - prints this help"); |
217 | Console.WriteLine("help - prints this help"); |
| 217 | Console.WriteLine("exit or quit - exit program"); |
218 | Console.WriteLine("exit or quit - exit program"); |
| 218 | } |
219 | } |
| 219 | 220 | ||
| 220 | static private bool parseNodeId(string node) { |
221 | static private bool parseNodeId(string node) { |
| 221 | // the goal here is to find a byte from 0-255 in a string, format in decimal (0-255) or in hex (0x0-0xff) |
222 | // the goal here is to find a byte from 0-255 in a string, format in decimal (0-255) or in hex (0x0-0xff) |
| 222 | // this is probably not done very neat |
223 | // this is probably not done very neat |
| 223 | sNodeid = false; |
224 | sNodeid = false; |
| 224 | if (node.Length > 0) { |
225 | if (node.Length > 0) { |
| 225 | try { |
226 | try { |
| 226 | nodeid = byte.Parse(node); |
227 | nodeid = byte.Parse(node); |
| 227 | sNodeid = true; |
228 | sNodeid = true; |
| 228 | } catch {} |
229 | } catch {} |
| 229 | } |
230 | } |
| 230 | if (node.Length > 2) { |
231 | if (node.Length > 2) { |
| 231 | if (node.Substring(0, 2).Equals("0x")) { |
232 | if (node.Substring(0, 2).Equals("0x")) { |
| 232 | if (node.Length == 3) { |
233 | if (node.Length == 3) { |
| 233 | try { |
234 | try { |
| 234 | nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber); |
235 | nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber); |
| 235 | sNodeid = true; |
236 | sNodeid = true; |
| 236 | } catch { |
237 | } catch { |
| 237 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); } |
238 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); } |
| 238 | return false; |
239 | return false; |
| 239 | } |
240 | } |
| 240 | } else if (node.Length == 4) { |
241 | } else if (node.Length == 4) { |
| 241 | try { |
242 | try { |
| 242 | nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); |
243 | nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); |
| 243 | sNodeid = true; |
244 | sNodeid = true; |
| 244 | } catch { |
245 | } catch { |
| 245 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 2"); } |
246 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 2"); } |
| 246 | return false; |
247 | return false; |
| 247 | } |
248 | } |
| 248 | } else { |
249 | } else { |
| 249 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 3"); } |
250 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 3"); } |
| 250 | return false; |
251 | return false; |
| 251 | } |
252 | } |
| 252 | } |
253 | } |
| 253 | } |
254 | } |
| 254 | 255 | ||
| 255 | return true; |
256 | return true; |
| 256 | } |
257 | } |
| 257 | 258 | ||
| 258 | static private bool parseInput(string instr) { |
259 | static private bool parseInput(string instr) { |
| 259 | //parse commands entered on std in |
260 | //parse commands entered on std in |
| 260 | if (instr.Equals("reset")) { //reset command, send a reset to current node |
261 | if (instr.Equals("reset")) { //reset command, send a reset to current node |
| 261 | if (sNodeid) { |
262 | if (sNodeid) { |
| 262 | dc.flushData(); |
263 | dc.flushData(); |
| 263 | CanNMT cpn = new CanNMT(); |
264 | CanNMT cpn = new CanNMT(); |
| 264 | cpn.doReset(dc, nodeid); |
265 | cpn.doReset(dc, nodeid); |
| 265 | } else { |
266 | } else { |
| 266 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
267 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 267 | } |
268 | } |
| 268 | } |
269 | } |
| 269 | else if (instr.Equals("start")) { //start command, send a start to current node |
270 | else if (instr.Equals("start")) { //start command, send a start to current node |
| 270 | if (sNodeid) { |
271 | if (sNodeid) { |
| 271 | CanNMT cpn = new CanNMT(); |
272 | CanNMT cpn = new CanNMT(); |
| 272 | cpn.doStart(dc, nodeid); |
273 | cpn.doStart(dc, nodeid); |
| 273 | } else { |
274 | } else { |
| 274 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
275 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 275 | } |
276 | } |
| 276 | } |
277 | } |
| 277 | else if (instr.StartsWith("go")) { //downloading command |
278 | else if (instr.StartsWith("go")) { //downloading command |
| 278 | bool dlBios = false; |
279 | bool dlBios = false; |
| 279 | bool error = true; |
280 | bool error = true; |
| 280 | if (instr.Equals("go bios")) { //if bios should be downloaded |
281 | if (instr.Equals("go bios")) { //if bios should be downloaded |
| 281 | dlBios = true; |
282 | dlBios = true; |
| 282 | error = false; |
283 | error = false; |
| 283 | } else if (instr.Equals("go")) { //is an application should be downloaded |
284 | } else if (instr.Equals("go")) { //is an application should be downloaded |
| 284 | error = false; |
285 | error = false; |
| 285 | } |
286 | } |
| 286 | 287 | ||
| 287 | if (!sNodeid) { |
288 | if (!sNodeid) { |
| 288 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
289 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 289 | error = true; |
290 | error = true; |
| 290 | } |
291 | } |
| 291 | if (!sHexfile) { |
292 | if (!sHexfile) { |
| 292 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
293 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 293 | error = true; |
294 | error = true; |
| 294 | } |
295 | } |
| 295 | 296 | ||
| 296 | CanNMT cpn = new CanNMT(); |
297 | CanNMT cpn = new CanNMT(); |
| 297 | if (!error && aReset) { //commandline arguments specified that a reset should be done before download |
298 | if (!error && aReset) { //commandline arguments specified that a reset should be done before download |
| 298 | //send a reset command (and wait for feedback) |
299 | //send a reset command (and wait for feedback) |
| 299 | if (!cpn.doReset(dc, nodeid)) { |
300 | if (!cpn.doReset(dc, nodeid)) { |
| 300 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); } |
301 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); } |
| 301 | error = true; |
302 | error = true; |
| 302 | } |
303 | } |
| 303 | } |
304 | } |
| 304 | 305 | ||
| 305 | if (!error) { |
306 | if (!error) { |
| 306 | //send application |
307 | //send application |
| 307 | dc.flushData(); |
308 | dc.flushData(); |
| 308 | dl = new Downloader(hf, dc, nodeid, dlBios); |
309 | dl = new Downloader(hf, dc, nodeid, dlBios); |
| 309 | if (!dl.go()) { |
310 | if (!dl.go()) { |
| 310 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
311 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 311 | error = true; |
312 | error = true; |
| 312 | } |
313 | } |
| 313 | } |
314 | } |
| 314 | 315 | ||
| 315 | if (!error && aStart) { //commandline arguments specified that a start should be done after download |
316 | if (!error && aStart) { //commandline arguments specified that a start should be done after download |
| 316 | //start applikation |
317 | //start applikation |
| 317 | cpn.doStart(dc, nodeid); |
318 | cpn.doStart(dc, nodeid); |
| 318 | } |
319 | } |
| 319 | 320 | ||
| 320 | } |
321 | } |
| 321 | else if (instr.StartsWith("load")) { //load hexfile or reload current hexfile |
322 | else if (instr.StartsWith("load")) { //load hexfile or reload current hexfile |
| 322 | if (instr.Length > 5) { |
323 | if (instr.Length > 5) { |
| 323 | //second argument is a hexfile, this file should be loaded |
324 | //second argument is a hexfile, this file should be loaded |
| 324 | hexfile = instr.Substring(5); |
325 | hexfile = instr.Substring(5); |
| 325 | if (hf.loadHex(hexfile)) { |
326 | if (hf.loadHex(hexfile)) { |
| 326 | sHexfile = true; |
327 | sHexfile = true; |
| 327 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
328 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 328 | } else { |
329 | } else { |
| 329 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); } |
330 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); } |
| 330 | } |
331 | } |
| 331 | } else { |
332 | } else { |
| 332 | //reload current hexfile. if a hexfile already has been specified |
333 | //reload current hexfile. if a hexfile already has been specified |
| 333 | if (sHexfile) { |
334 | if (sHexfile) { |
| 334 | if (hf.loadHex(hexfile)) { |
335 | if (hf.loadHex(hexfile)) { |
| 335 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); } |
336 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); } |
| 336 | } else { |
337 | } else { |
| 337 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); } |
338 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); } |
| 338 | } |
339 | } |
| 339 | } else { |
340 | } else { |
| 340 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
341 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 341 | } |
342 | } |
| 342 | 343 | ||
| 343 | } |
344 | } |
| 344 | } |
345 | } |
| 345 | else if (instr.StartsWith("node")) { |
346 | else if (instr.StartsWith("node")) { |
| 346 | //change nodeid |
347 | //change nodeid |
| 347 | if (instr.Length > 5) { |
348 | if (instr.Length > 5) { |
| 348 | //second argument is nodeid |
349 | //second argument is nodeid |
| 349 | string node = instr.Substring(5); |
350 | string node = instr.Substring(5); |
| 350 | parseNodeId(node); |
351 | parseNodeId(node); |
| 351 | } |
352 | } |
| 352 | if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); } |
353 | if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); } |
| 353 | } |
354 | } |
| 354 | else if (instr.Equals("help")) { |
355 | else if (instr.Equals("help")) { |
| 355 | printHelp(); |
356 | printHelp(); |
| 356 | } |
357 | } |
| 357 | else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) { |
358 | else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) { |
| 358 | return false; |
359 | return false; |
| 359 | } |
360 | } |
| 360 | else { |
361 | else { |
| 361 | Console.WriteLine("Unknown command."); |
362 | Console.WriteLine("Unknown command."); |
| 362 | } |
363 | } |
| 363 | 364 | ||
| 364 | return true; |
365 | return true; |
| 365 | } |
366 | } |
| 366 | 367 | ||
| 367 | } |
368 | } |
| 368 | 369 | ||