Subversion Repositories HomeAutomation

Rev

Rev 452 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
434 arune 1
using System;
2
using System.Threading;
3
using System.Collections.Generic;
4
using System.Text;
5
 
6
class Program {
7
	//static HexFile hf;
8
	const byte DEBUG_LEVEL = 3;	//0, 1, 2, 3
9
	static SerialConnection sc;
10
	static Daemon d;
11
	static TcpServer tcps;
12
	//static Downloader dl;
13
 
14
	static private string argPort;
15
	static private int argBaud;
16
 
17
	static void Main(string[] args) {
18
		bool error = false;
19
		if (args.Length != 2) {
20
			Console.WriteLine("Syntax: program.exe <port> <baudrate>");
21
			Console.WriteLine("port        - can be in form of /dev/ttyS0 or com2 or /udp/ipaddress/port");
22
			Console.WriteLine("baudrate    - any baudrate like 19200 (for udp baudrate could be any number)");
23
			Console.WriteLine("");
24
			Console.WriteLine("example: program.exe /dev/ttyUSB0 19200");
25
			Console.WriteLine("example: program.exe /udp/192.168.0.10/1100 0");
26
			error = true;
27
            return;
28
		} 
29
 
30
		if (!error) {
31
			//Console.WriteLine("arg0: " + args[0] + "\n");
32
			argPort = args[0];
33
			argBaud = Int32.Parse(args[1]);
34
		}
35
 
36
		if (!error) {
37
			if (DEBUG_LEVEL>0) {
38
				Console.WriteLine("canDaemon");
39
				Console.WriteLine("Commands:");
40
				//Console.WriteLine("reset - reset communication.");
41
				Console.WriteLine("exit  - exit program");
42
				Console.WriteLine("");
43
				Thread.Sleep(1000);
44
			}
45
 
46
			sc = new SerialConnection();
47
			try {
48
				sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
49
			}
50
			catch (Exception e) { Console.WriteLine("Error: " + e.Message); }
51
			if (!sc.open()) { 
52
				Console.WriteLine("Error opening port to target"); 
53
				//... här ska man testa om igen...
54
 
55
			} else {
56
 
57
				d = new Daemon(sc);
58
				tcps = new TcpServer(1200);
59
				d.addServer(tcps);
60
 
61
				Thread t = new Thread(d.thread);
62
				t.Start();
63
 
64
				string instr;
65
				do {
66
					//Console.Write("> ");
67
					instr = Console.ReadLine().ToLower();
68
				} while (parseInput(instr));
69
				d.stop();
70
			}
71
		}
72
	}
73
 
74
	static private bool parseInput(string instr) {
75
		if (instr.Equals("reset")) {
76
			//Console.WriteLine("Resetting..");
77
			//sc = new SerialConnection();
78
			//try {
79
			//	sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
80
			//}
81
			//catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
82
			//if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
83
 
84
			//byte[] data = new byte[8];
85
			//CanPacket cp = new CanPacket(CAN_NMT, CAN_NMT_RESET, MY_ID, argReceiverID, 0, data);
86
			//sc.writePacket(cp);
87
            //sc.close();
88
		}
89
		else if (instr.Equals("exit")) {
90
			return false;
91
		}
92
		else {
93
			Console.WriteLine("Unknown command.");
94
		}
95
 
96
		return true;
97
	}
98
 
99
}