Subversion Repositories HomeAutomation

Rev

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

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