NModbus Master writes to NModbus Slave

2,768 views
Skip to first unread message

daniel...@gmx.de

unread,
Feb 14, 2008, 12:42:36 PM2/14/08
to NModbus
Hello all !
I have a little Problem with writing MultipleRegisters from a Master
to a Slave via TCP.
I wrote two C# programms one is a Modbus Slave (I used the example
code) with a TestDataStore.
The other programm is a Modbus Master which reads or writes Multiple
Registers (also from example code).
Both programs are Windows Forms. The Master has buttons for read and
write and a message Box to show the received data.
If I read MultipleRegister it works, I can se the data in the
Messagebox, there is no error.
If I write MultipleRegisters there is also no error or exception, but
if I read again there was no change made in the Slave DataStore.

The Slave:
public static void CreateSlave()
{
byte slaveID = 1;
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0,
1 });

// create and start the TCP slave
TcpListener slaveTcpListener = new TcpListener(address,
port);
slaveTcpListener.Start();
ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveID,
slaveTcpListener);

slave.DataStore = DataStoreFactory.CreateTestDataStore();

slave.Listen();
}

The Master :
private void btnWrite_Click(object sender, EventArgs e)
{
UInt16 StartAddress= 0;
UInt16[] Data = new UInt16[5];

Data[0] = 111;
Data[1] = 222;
Data[2] = 333;
Data[3] = 444;
Data[4] = 555;

using (TcpClient client = new TcpClient("127.0.0.1", 502))
{
ModbusIpMaster master =
ModbusIpMaster.CreateTcp(client);
master.WriteMultipleRegisters(StartAddress, Data);
}

}


private void btnRead_Click(object sender, EventArgs e)
{
TcpClient masterTcpClient = new TcpClient("127.0.0.1",
502);
ModbusIpMaster master =
ModbusIpMaster.CreateTcp(masterTcpClient);

ushort numInputs = 127;
ushort startAddress = 0;

// read five register values
ushort[] inputs = master.ReadInputRegisters(startAddress,
numInputs);

string show = string.Empty;
foreach (ushort rec in inputs)
{
show = show + rec.ToString();
}
MessageBox.Show(show);

}

Has someone an idea?
Thank you!

Scott Alexander

unread,
Feb 17, 2008, 8:24:54 PM2/17/08
to NModbus...@googlegroups.com
You're writing to the Holding registers but you're reading from the
Input registers. Use the method ReadIHoldingRegisters.

Scott

daniel...@gmx.de

unread,
Feb 20, 2008, 4:22:15 AM2/20/08
to NModbus

Thank you Scott, now it works!
Reply all
Reply to author
Forward
0 new messages