I am developing a windows form to function as a modbus tcp Master Simulator.I am using NModbus library. I want to connect to multiple slaves simultaneously and do the read and write operation,does the NModbus library supports this kind of implementation? and if so how?. Currently i am able to connect to single slave device and do the read/write operations,but i am stuck on how to the same with multiple slaves though. Should i use the threading Concept to achieve the same. Here is my code to connect to single slave device and do the read/write operation.
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
TcpClient masterTcpClient = new TcpClient(txtIP.Text, 502);
master = ModbusIpMaster.CreateIp(masterTcpClient);
MessageBox.Show("Connected");
}
catch (SystemException error)
{
MessageBox.Show(error.Message);
}
}
private void btnReadCoil_Click(object sender, EventArgs e)
{
try
{
byte slaveID = 255;
ushort startAddress = Convert.ToUInt16(txtStartaddress.Text);
ushort numInputs = Convert.ToUInt16(txtSize.Text);
bool[] inputs = master.ReadCoils(slaveID, startAddress, numInputs);
AnswerFromServer.Items.Clear();
for (int i = 0; i < inputs.Length; i++)
{
AnswerFromServer.Items.Add(Convert.ToInt16(inputs[i]));
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Exception Reading values from Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnWriteSingleRegister_Click(object sender, EventArgs e)
{
try
{
byte slaveID = 255;
ushort RegisterAddress = Convert.ToUInt16(txtStartaddress.Text);
ushort value = Convert.ToUInt16(txtSingleValue.Text);
master.WriteSingleRegister(slaveID, RegisterAddress, value);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Exception writing values to Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Can any one please help me with this one?