Looking for an error in my Modbus Console.WriteLine

164 Views Asked by At

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]}");
                }
            }
        }
    }
}
2

There are 2 best solutions below

0
On

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);
                }
1
On
namespace NModbus.TestDriver

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


    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                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]}");
                    }
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine (ex.Message);
            }

        }
    }
}

Can u try like this and say to your ex message ?