Exemplo n.º 1
0
        static int RunType(TypeOptions opts)
        {
            using (var client = new AdvantechClient())
            {
                client.PortName = opts.PortName;
                client.BaudRate = opts.BaudRate;
                client.DataBits = opts.DataBits;
                client.Parity   = opts.Parity;

                if (!OpenPort(client))
                {
                    return(1);
                }

                string type;
                try
                {
                    type = client.ReadModuleType(opts.UnitAddress);
                }
                catch (Exception exp)
                {
                    return(HandleException(exp));
                }
                string desc = TypeDescriptionLookup(type);

                Console.Out.Write(type);
                if (!string.IsNullOrEmpty(desc))
                {
                    Console.Out.WriteLine($" ({desc})");
                }

                return(0);
            }
        }
Exemplo n.º 2
0
        static int RunDetectPorts(DetectPortsOptions opts)
        {
            var validPorts = new List <PortDescription>();

            using (var client = new AdvantechClient())
            {
                client.BaudRate = opts.BaudRate;
                client.DataBits = opts.DataBits;
                client.Parity   = opts.Parity;

                foreach (var portDesc in SerialPortStream.GetPortDescriptions())
                {
                    client.PortName = portDesc.Port;
                    if (opts.Verbose)
                    {
                        Console.Error.Write($"Trying {portDesc.Port}");
                        if (!string.IsNullOrEmpty(portDesc.Description))
                        {
                            Console.Out.Write($" ({portDesc.Description})");
                        }
                        Console.Out.Write("...");
                    }
                    try
                    {
                        client.Open();
                    }
                    catch (Exception exp) when(exp is UnauthorizedAccessException || exp is InvalidOperationException)
                    {
                        if (opts.Verbose)
                        {
                            Console.Error.WriteLine("already in use.");
                        }
                        continue;
                    }
                    catch (IOException)
                    {
                        if (opts.Verbose)
                        {
                            Console.Error.WriteLine("cannot open.");
                        }
                        continue;
                    }

                    try
                    {
                        client.ReadModuleType(opts.UnitAddress);
                    }
                    catch (TimeoutException)
                    {
                        if (opts.Verbose)
                        {
                            Console.Error.WriteLine("timeout.");
                        }
                        client.Close();
                        continue;
                    }
                    catch (FormatException)
                    {
                        if (opts.Verbose)
                        {
                            Console.Error.WriteLine("frame error.");
                        }
                        client.Close();
                        continue;
                    }
                    if (opts.Verbose)
                    {
                        Console.Error.WriteLine("valid.");
                    }
                    validPorts.Add(portDesc);
                    client.Close();
                }
            }
            if (opts.Verbose)
            {
                Console.Error.WriteLine();
            }
            foreach (var portDesc in validPorts)
            {
                Console.Out.Write($"{portDesc.Port}");
                if (!string.IsNullOrEmpty(portDesc.Description))
                {
                    Console.Out.Write($" ({portDesc.Description})");
                }
                Console.Out.WriteLine();
            }

            return(0);
        }