Exemplo n.º 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;
 }
Exemplo n.º 2
0
        private void _msg_routing_GiveFactoryNum(XPacket packet, XRoutingManager manager)
        {
            if (packet.SourceAddr == 0 && packet.DestinationType == 1)
            {
                var fr = packet.ToFactoryRecord();
                XDevice device = KnownDevices.FirstOrDefault<XDevice>(xd => xd.FactoryRec == fr);
                if (device == null)
                {
                    device = new XDevice() { FactoryRec = fr };
                    byte addr = 01;
                    while (KnownDevices.FirstOrDefault<XDevice>(xd => xd.Addr == addr) != null)
                    {
                        addr++;
                        if (addr >= 255) throw new Exception("All addressed closed!");
                    }
                    device.Addr = addr;
                    KnownDevices.Add(device);
                }
                LastDeviceAddr = device.Addr;
                
                /*
                #dstAddr  00
                #dstType  02
                #srcAddr  254
                #srcType  A6
                FactoryNum
                */

                Send(new XPacket() { 
                    Data = packet.Data, 
                    SourceAddr = DeviceAddr, 
                    SourceType = device.Addr, 
                    DestinationType = 2 }
                );
                State = EDeviceState.Working;
            }
            if (packet.DestinationAddr == DeviceAddr && packet.DestinationType == 4)
            {
                XDevice device = KnownDevices.FirstOrDefault<XDevice>(xd => xd.Addr == packet.SourceAddr);
                if (device == null)
                {
                    device = new XDevice() { Addr = packet.SourceAddr, FactoryRec = packet.ToFactoryRecord() };
                    KnownDevices.Add(device);
                }
                if (packet.SourceAddr == 0)
                {
                    if (device.Addr == 0)
                    {
                        byte addr = 01;
                        while (KnownDevices.FirstOrDefault<XDevice>(xd => xd.Addr == addr) != null)
                        {
                            addr++;
                            if (addr >= 255) throw new Exception("All addressed closed!");
                        }
                        device.Addr = addr;
                    }

                    /*
                      #dstAddr  3F
                      #dstType  04
                      #srcAddr  23
                      #srcType  00
                      FactoryNum
                    */

                    Send(new XPacket()
                    {
                        Data = packet.Data,
                        SourceAddr = DeviceAddr,
                        SourceType = device.Addr,
                        DestinationType = 2
                    });
                }
                else
                {
                    if (packet.SourceAddr == DeviceAddr) DeviceAddr--;//ЕСЛИ В СЕТИ УЖЕ ЕСТЬ УСТРОЙСТВО С ТАКИМ АДРЕСОМ
                }
                LastDeviceAddr = device.Addr;
                State = EDeviceState.Working;
            }
            if (packet.DestinationType == 5)
            {
                Send(new XPacket()
                {
                    Data = FactoryRec,
                    SourceAddr = DeviceAddr,
                    SourceType = 05,
                    DestinationAddr = packet.DestinationAddr,
                    DestinationType = 4
                });
            }
        }