Ejemplo n.º 1
0
        public bool Init()
        {
            if (!PortHelpers.PortNameExists(Name))
            {
                log.LogError($"Port: '{Name}' not exists");
                //throw new Exception($"{port.PortName} not exists");
                return(false);
            }
            stream = new SerialPort(Name)
            {
                BaudRate     = 115200,
                ReadTimeout  = 1000,
                WriteTimeout = 1000,
                Parity       = Parity.None,
                StopBits     = StopBits.One,
                DataBits     = 8,
                Encoding     = Encoding.ASCII,
                NewLine      = "\r\n"
            };
            stream.DataReceived  += OnStreamDataReceived;
            stream.ErrorReceived += OnStreamErrorReceived;
            stream.PinChanged    += OnStreamPinChanged;

            if (IsRS485)
            {
                stream.Handshake = Handshake.None;
                stream.RtsEnable = true;
                stream.DtrEnable = true;
                log.LogDebug($"Port {Name} in RS485 mode");
            }

            try
            {
                stream.Open();
            }catch (Exception) {}
            if (!stream.IsOpen)
            {
                log.LogCritical($"Error opening serial port {Name}");
                //throw new Exception($"Error opening serial port {port.PortName}");
                return(false);
            }
            log.LogInformation($"Port {Name} open");
            if (stream == null)
            {
                log.LogCritical($"No serial port {Name}");
                // throw new Exception($"No serial port {port.PortName}");
                return(false);
            }
            if (stream.CtsHolding)
            {
                log.LogDebug($"Cts detected {Name}");
            }
            else
            {
                log.LogDebug($"Cts NOT detected {Name}");
            }
            log.LogInformation($"Port listener started: {Name}");
            return(true);
        }
Ejemplo n.º 2
0
 public void ParseTest()
 {
     ParseAndCheck("ALL TRAFFIC");
     ParseAndCheck("ALL PORTS");
     ParseAndCheck("UDP ALL PORTS");
     ParseAndCheck("ICMP Type 8");   // PING
     ParseAndCheck("ICMP Type 4");
     ParseAndCheck("ICMP Type 5 Code 7");
     //Assert.Throws(typeof(NotImplementedException), () => ParseAndCheck("ICMP Type 9"));
     ParseAndCheck("2000-3000");
     ParseAndCheck("2000");
     ParseAndCheck("UDP 2000");
     ParseAndCheck("UDP 2000-3000");
     Assert.AreEqual("100", PortHelpers.Parse("300", 100).ToString());
     Assert.AreEqual("UDP 100", PortHelpers.Parse("UDP 300", 100).ToString());
     Assert.AreEqual("UDP 100-200", PortHelpers.Parse("udp 300", 100, 200).ToString());
     Assert.AreEqual("UDP 100-200", PortHelpers.Parse("UDP 300-400", 100, 200).ToString());
     Assert.AreEqual("UDP 100", PortHelpers.Parse("UDP 300-400", 100, 100).ToString());
     Assert.AreEqual("UDP 100-400", PortHelpers.Parse("UDP 300-400", 100).ToString());
     Assert.AreEqual("UDP 300-500", PortHelpers.Parse("UDP 300-400", 0, 500).ToString());
     Assert.AreEqual("ICMP Type 1", PortHelpers.Parse("ICMP type 3", 1).ToString());
     Assert.AreEqual("ICMP Type 1 Code 8", PortHelpers.Parse("icmp type 3 code 5", 1, 8).ToString());
 }
Ejemplo n.º 3
0
 void ParseAndCheck(string s)
 {
     Assert.AreEqual(s, PortHelpers.Parse(s).ToString());
 }