static int RunInteractive(InteractiveOptions 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); } var parser = new InteractiveCommandParser(client); bool close = false; do { Console.Error.Write("] "); var line = Console.In.ReadLine(); try { close = parser.ProcessCommand(line); } catch (Exception exp) { HandleException(exp); } Console.Out.WriteLine(); }while (!close); } return(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); } }
static int RunWriteAO(WriteAOOptions 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); } try { client.WriteAO(opts.UnitAddress, opts.Channel, opts.Value); } catch (Exception exp) { return(HandleException(exp)); } return(0); } }
static int RunReadAO(ReadAOOptions 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); } double value; try { value = client.ReadAO(opts.UnitAddress, opts.Channel); } catch (Exception exp) { return(HandleException(exp)); } Console.Out.WriteLine(value); return(0); } }
static bool OpenPort(AdvantechClient client) { try { client.Open(); } catch (Exception exp) when( exp is UnauthorizedAccessException || exp is InvalidOperationException || exp is IOException) { Console.Error.WriteLine("Unable to open port."); return(false); } return(true); }
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); }
public InteractiveCommandParser(AdvantechClient client) { _client = client; }