Subversion Repositories HomeAutomation

Rev

Rev 452 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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