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