static int RunRead3(Read3Options opts) { using (var client = new ModbusRtuClient()) { client.PortName = opts.PortName; client.BaudRate = opts.BaudRate; client.DataBits = opts.DataBits; client.Parity = opts.Parity; if (!OpenPort(client)) { return(1); } ushort[] values; try { values = client.ReadHoldingRegisters(opts.UnitAddress, opts.RegisterAddress, opts.RegisterCount); } catch (Exception exp) { return(HandleException(exp)); } for (int i = 0; i < values.Length; i++) { Console.Out.Write("0x"); Console.Out.Write(values[i].ToString("X4")); Console.Out.Write(" "); Console.Out.WriteLine(values[i].ToString("####0")); } return(0); } }
static void DoClientWork(ModbusRtuClient client, ILogger logger) { Span <byte> data; var sleepTime = TimeSpan.FromMilliseconds(100); var unitIdentifier = (byte)0x01; var startingAddress = (ushort)0; var registerAddress = (ushort)0; // ReadHoldingRegisters = 0x03, // FC03 data = client.ReadHoldingRegisters(unitIdentifier, startingAddress, 10); logger.LogInformation("FC03 - ReadHoldingRegisters: Done"); Thread.Sleep(sleepTime); // WriteMultipleRegisters = 0x10, // FC16 client.WriteMultipleRegisters(unitIdentifier, startingAddress, new byte[] { 10, 00, 20, 00, 30, 00, 255, 00, 255, 01 }); logger.LogInformation("FC16 - WriteMultipleRegisters: Done"); Thread.Sleep(sleepTime); // ReadCoils = 0x01, // FC01 data = client.ReadCoils(unitIdentifier, startingAddress, 10); logger.LogInformation("FC01 - ReadCoils: Done"); Thread.Sleep(sleepTime); // ReadDiscreteInputs = 0x02, // FC02 data = client.ReadDiscreteInputs(unitIdentifier, startingAddress, 10); logger.LogInformation("FC02 - ReadDiscreteInputs: Done"); Thread.Sleep(sleepTime); // ReadInputRegisters = 0x04, // FC04 data = client.ReadInputRegisters(unitIdentifier, startingAddress, 10); logger.LogInformation("FC04 - ReadInputRegisters: Done"); Thread.Sleep(sleepTime); // WriteSingleCoil = 0x05, // FC05 client.WriteSingleCoil(unitIdentifier, registerAddress, true); logger.LogInformation("FC05 - WriteSingleCoil: Done"); Thread.Sleep(sleepTime); // WriteSingleRegister = 0x06, // FC06 client.WriteSingleRegister(unitIdentifier, registerAddress, new byte[] { 65, 67 }); logger.LogInformation("FC06 - WriteSingleRegister: Done"); }
static int RunDetectPorts(DetectPortsOptions opts) { var validPorts = new List <PortDescription>(); using (var client = new ModbusRtuClient()) { 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.ReadHoldingRegisters(opts.UnitAddress, 0, 1); } 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; } catch (ModbusException) { } 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); }