public SerialPacketManager(SerialConfig config) : base(config)
 {
     receiverStart = PACKET_SIZED_START_BYTE;
     if (config.RxPacketType == PacketType.SimpleCRC || config.RxPacketType == PacketType.SizedCRC || config.RxPacketType == PacketType.SizedCRC_old)
     {
         receiverCRCValue = config.ReceiverCRC;
     }
     if (config.TxPacketType == PacketType.SimpleCRC || config.TxPacketType == PacketType.SizedCRC || config.TxPacketType == PacketType.SizedCRC_old)
     {
         transmitterCRCValue = config.TransmitterCRC;
     }
     if (config.RxPacketType == PacketType.SizedOld || config.RxPacketType == PacketType.SizedCRC_old)
     {
         receiverEnd = PACKET_END_TRANSMIT;
     }
     if (config.TxPacketType == PacketType.SizedOld || config.TxPacketType == PacketType.SizedCRC_old)
     {
         transmitterEnd = PACKET_END_TRANSMIT;
     }
     if (config.RxPacketType == PacketType.Simple || config.RxPacketType == PacketType.SimpleCoded || config.RxPacketType == PacketType.SimpleCRC)
     {
         receiverStart = PACKET_UNSIZED_START_BYTE;
         receiverEnd = PACKET_END_TEXT;
     }
     if (config.TxPacketType == PacketType.Simple || config.TxPacketType == PacketType.SimpleCoded || config.TxPacketType == PacketType.SimpleCRC)
     {
         transmitterStart = PACKET_UNSIZED_START_BYTE;
         transmitterEnd = PACKET_END_TEXT;
     }
 }
예제 #2
0
 public static SerialConfig PortOpen(string portName, uint speed)
 {
     var cfg = ComPortConfigs.FirstOrDefault(s => s.PortName == portName);
     if (Serials.ContainsKey(portName))
     {
         if (cfg == null) throw new Exception("Port " + portName + " opened, but config doesn't exists");
         if (Serials[portName].IsOpened)
         {
             return cfg;
         }
         else
         {
             Serials[portName].Close();
         }                
     }
     if (cfg == null)
     {
         cfg = new SerialConfig();
         cfg.PortName = portName;
         if (speed > 0)
         {
             cfg.Speed = speed;
         }
     }
     if (AddPort(cfg, true) == null)
     {
         throw new Exception("Can't open port " + portName);
     }
     return cfg;
 }
예제 #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;
 }
예제 #4
0
 private void menuUARTconnect_Click(object sender, EventArgs e)
 {
     if (cbCOMs.SelectedIndex >= 0 && cbUARTSpeeds.SelectedIndex >= 0)
     {
         var portName = cbCOMs.SelectedItem + "";
         SerialConfig cfg = ComPortConfigs.FirstOrDefault(c => c.PortName == portName);
         SerialManager sm;
         if (cfg == null)
         {
             cfg = new SerialConfig(portName)
             {
                 Speed = Convert.ToUInt32(cbUARTSpeeds.SelectedItem + "")
             };
         }
         OpenDevice(cfg);
     }
 }
예제 #5
0
        protected void OpenDevice(SerialConfig cfg)
        {
            if (cfg == null) return;
            if (device != null)
            {
                device.Close();
            }
            try
            {
                switch (cfg.RxPacketType)
                {
                    case PacketType.Raw:
                        device = new SerialManager(cfg);
                        break;
/*case PacketType.Simple:
                    case PacketType.SimpleCoded:
                    case PacketType.SimpleCRC:
                    case PacketType.Sized:                    
                    case PacketType.SizedOld:
                    case PacketType.SizedCRC:
                    case PacketType.SizedCRC_old:
                    case PacketType.PacketInvariant:
                        device = new SerialPacketManager(cfg);
                        break;*/
                    case PacketType.Addressed:
                    case PacketType.AddressedOld:
                        device = new SerialAddressedManager(cfg);
                        break;
                    case PacketType.XRouting:
                        device = new XRoutingManager(cfg);
                        break;
                    default: 
                        device = new SerialPacketManager(cfg);
                        break;
                }
                if (device.Connect())
                {
                    device.OnStateChange += SelectedPort_Disposed;
                    ShowMessage(cfg.PortName + "  " + "\nConnected!");
                    ShowMessage(JsonConvert.SerializeObject(cfg));
                    EnableItems();
                }
                else
                {
                    DisableItems();
                    device.Close();
                    device = null;
                    ShowError(cfg.PortName + "Connecting error: " + device.LastError);
                }
            }
            catch (Exception err)
            {
                ShowError(cfg.PortName + "Connecting error: ", err);
            }
        }
 public SerialAddressedManager(SerialConfig config) : base(config) { }
예제 #7
0
 public XRoutingManager(SerialPort port, SerialConfig config) : base(port, config) {
     receiverCRCValue = config.ReceiverCRC;
     transmitterCRCValue = config.TransmitterCRC;
     DeviceAddr = config.DefaultDeviceAddr;
 }
예제 #8
0
        public SerialManager(SerialConfig config) : this(new SerialPort(config.PortName, (int)config.Speed, config.Parity, config.DataBits, config.StopBits))
        {

        }