Subversion Repositories HomeAutomation

Rev

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