0

I have this C# Code, and it is producing an error on the Console.WriteLine($"Register {startAddress + i}={registers[i]}"); line. I have tried double )), and placed it everywhere in the statement. I simply cannot see where the error is. I am probably missing something simple and I'm just not seeing it.

    namespace NModbus.TestDriver

{
    using System;
    using System.Net.Sockets;
    using Modbus.Device;
    using NModbus;


    class Program
    {
        static void Main(string[] args)
        {
                         using (TcpClient client = new TcpClient("192.168.111.169", 502))
                {
                    client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                    var master = ModbusIpMaster.CreateIp(client);
                    // read five input values
                  byte slaveId = 1;
                ushort startAddress = 1;
                ushort numRegisters = 5;

                // read five registers      
                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i < numRegisters; i++)
                {
                    Console.WriteLine($"Register {startAddress + i}={registers[i]}");
                }
            }
        }
    }
}
blackgreen
  • 34,072
  • 23
  • 111
  • 129
lnxbox
  • 1
  • "having an error" - please include the error message in your question (the line [works OK](https://dotnetfiddle.net/dX4Vbc) for me). Probably better to tag this with `c#` and remove the modbus tags (you are using modbus but the issue you face does not appear to be modbus related). – Brits May 12 '21 at 09:34

1 Answers1

0

Try defining the string before doing Console.WriteLine -- so the for loop is like this

             for (int i = 0; i < numRegisters; i++)
                {
                    string whateverName = $"Register {startAddress + i}={registers[i]}"
                    Console.WriteLine(whateverName);
                }
C. Peck
  • 3,641
  • 3
  • 19
  • 36