예제 #1
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;
 }
예제 #2
0
 public fmAnalizer(SerialPacketManager manager)
 {
     InitializeComponent();
     this.manager = manager;
 }