添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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]}");
                "having an error" - please include the error message in your question (the line works OK 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, 2021 at 9:34

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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.