Exemple #1
0
 static void Main()
 {
     var settings = ConfigurationManager.AppSettings;
     var port = settings["COM_PORT"];
     var speed = Convert.ToInt32(settings["COM_SPEED"]);
     var tcpPort = Convert.ToInt32(settings["TCP_PORT"]);
     var httpPort = Convert.ToInt32(settings["HTTP_PORT"]);
     var cncAddr = Convert.ToByte(settings["CNC_ADDR"]);
     SerialPort = new SerialAddressedManager(port, speed);
     SerialPort.DeviceAddr = cncAddr;
     //SerialPort.Connect();
     HttpServer = new SmallHttpServer(httpPort);
     HttpServer.Start();
     TcpServer = new HardwareTcpServer(tcpPort);
     TcpServer.Start();
     CncController.Init(SerialPort, Application.StartupPath, cncAddr);
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new fmMain());
     HttpServer.Close();
     TcpServer.Close();
     SerialPort.Close();
     CncController.Close();
 }
 public static void Init(SerialAddressedManager serialPort, string cfgPath, byte cncAddress)
 {
     CncController.cfgPath = cfgPath;
     CncAddress = cncAddress;
     Uart = serialPort;
     Uart.OnReceive += ProcessMessage;
     if (!File.Exists(cfgPath + "\\config.json"))
     {
         StreamWriter writer = new StreamWriter(cfgPath + "\\config.json", false, Encoding.UTF8);
         writer.Close();
         GetStateAsync();
         return;
     }
     StreamReader reader = new StreamReader(cfgPath + "\\config.json");
     var cfgStr = reader.ReadToEnd();
     reader.Close();
     if (cfgStr.Length > 0)
     {
         LastState = JsonConvert.DeserializeObject<MotorState>(cfgStr);
     }
     else
     {
         LastState = new MotorState();
     }
     initialized = true;
     
 }
Exemple #3
0
 protected static SerialConfig AddPort(SerialConfig cfgBase, bool autoConnect)
 {
     if (Serials.ContainsKey(cfgBase.PortName))
     {
         //Serials[cfg.PortName].SetParams((uint)cfg.Speed, cfg.DataBits, cfg.StopBits, cfg.Parity);
         if (autoConnect && !Serials[cfgBase.PortName].IsOpened)
         {
             Serials[cfgBase.PortName].Connect();
         }
         return cfgBase;
     }
     var cfg = ComPortConfigs.FirstOrDefault(s => s.PortName == cfgBase.PortName);
     SerialConfig cfgNew = null;
     SerialManager sm;
     switch (cfgBase.RxPacketType)
     {
         case PacketType.SimpleCoded:
         case PacketType.SimpleCRC: throw new NotImplementedException("PacketType.SimpleCRC have no manager today!");
         case PacketType.Sized:
             sm = new SerialPacketManager(cfgBase);
             break;
         case PacketType.SizedOld:
             sm = new SerialPacketManager(cfgBase);
             break;
         case PacketType.Addressed:
             sm = new SerialAddressedManager(cfgBase);
             (sm as SerialAddressedManager).DeviceAddr = 0xAA;
             break;
         case PacketType.AddressedOld:
             sm = new SerialAddressedManager(cfgBase);
             break;
         case PacketType.XRouting:
             /*if (cfg == null)
             {
                 cfgNew = new XRSerialConfig(cfgBase);
                 sm = new XRoutingManager(cfgNew as XRSerialConfig);
             }
             else
             {
                 sm = new XRoutingManager(cfg as XRSerialConfig);
             }   */
             sm = new XRoutingManager(cfgBase);
             (sm as XRoutingManager).DeviceAddr = 0xAB;
             
             break;
         default:
             sm = new SerialManager(cfgBase);
             break;
     }
     if (cfg == null)
     {
         if (cfgNew != null)
         {
             ComPortConfigs.Add(cfgNew);
             cfg = cfgNew;
         }
         else
         {
             ComPortConfigs.Add(cfgBase);
             cfg = cfgBase;
         }
     }
     Log(cfg);
     sm.OnReceive += serial_OnReceive;
     sm.OnSend += sm_OnSend;
     sm.OnError += serial_OnError;
     sm.OnStateChange += serial_OnStateChange;
     if (autoConnect)
     {
         if (sm.Connect())
         {
             Serials.Add(cfg.PortName, sm);
             return cfg;
         }
         else
         {
             Warn("Can't connect " + cfg.PortName);
             sm.OnReceive -= serial_OnReceive;
             sm.OnSend -= sm_OnSend;
             sm.OnError -= serial_OnError;
             sm.OnStateChange -= serial_OnStateChange;
             return null;
         }
     }
     return cfg;
 }