Click here to Skip to main content
14,357,858 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using this method, I am having difficulty.

int [] ReadWriteMultipleRegisters (int startingAddressRead, int quantityRead, int startingAddressWrite, int [] values)

Read / Write Multiple Registers (Function code 23).

startingAddressRead: First input register to read.

quantityRead: Number of input registers to read.

startingAddressWrite: First input register to write.

values: Values to write.

returns: Int Array which contains the holding registers [0..quantityRead-1].


I'm having an error assigning the variable writeData, they could help me.

Thank you.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyModbus;





namespace comunicacion_tcp
{
    class Program
    {
        private string IpAddress = "192.168.178.111";
        private int port = 502;
        private ushort startAddres = 4352;
        private ushort quantity = 1;
        private ushort startAddres1 = 4353;
        private int writeData;
        private ModbusClient modbustcp;
        public Program()
        {
            modbustcp = new ModbusClient(IpAddress, port);
            modbustcp.Connect();
            int[] response = modbustcp.ReadWriteMultipleRegisters( startAddres,quantity, startAddres1 , writeData);
            modbustcp.Disconnect();
            Console.WriteLine("value of input register 1" + response[0].ToString());


        }

        static void Main(string[] args)
        {
            Program _program = new Program();
            Console.ReadKey();
        }
    }
}
Posted
Updated 20-Aug-18 21:02pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try:
int[] writeData = new int[quantity];

That will work if you are reading values, but for writing you will need to define the size to at least the data you want to write, and set the values in there.
   
Comments
Member 13937256 21-Aug-18 14:44pm
   
The assignment error of the variable writeData is removed (Error: can not convert from int to int [])
I get an error 'Starting address invalid or starting address + quantity invalid'

When I run the program, I'm a little raw on this, sorry.
OriginalGriff 22-Aug-18 1:29am
   
That's coming from the "ReadWriteMultipleRegisters" method, and it's saying that the numeric parameters you have passed are out of range. That's going to be dependant on registers you are reading and from what - so you are going to have to start reading the manuals where you got those "magic numbers" from: 4352, 1, 4353, and so on.
We can't help you with that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100