daniel...@gmx.de
unread,Feb 14, 2008, 12:42:36 PM2/14/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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!