NModBus WriteMuitipleRegister Exception

53 views Asked by At

I'm working in a project with read/write a device through Modbus TCP (device can establish a virtual server) using NModBus. I'm using a timer to read, this works fine (ReadHoldingRegisters) but, when I try write to device with WriteMultipleRegister, the program shows System.IO.IOException: Unable to read data from the transport connection, but data can write in, here is the code i use.

public class ModbusController {
  private TcpClient tcpClient;
  private IModbusMaster modbusMaster;

  public ModbusController(string ipAddress, int port) {
    tcpClient = new TcpClient(ipAddress, port);
    var factory = new ModbusFactory();
    modbusMaster = factory.CreateMaster(tcpClient);

  }

  public async Task WriteData(ushort startAddress, ushort[] data) {
    await modbusMaster.WriteMultipleRegistersAsync(1, startAddress, data);
  }
}

ModbusController controller;

private async Task < bool > WriteToRegister() {
  ushort value = ushort.Parse(txtValue.Text);
  ushort address = (ushort) int.Parse(txtRegister.Text);

  ushort[] data = [value];

  Debug.WriteLine(value);

  try {
    controller = new ModbusController("172.16.100.106", 502);
    await controller.WriteData(address, data);
    return true;
  } catch (Exception ex) {
    MessageBox.Show(ex.ToString());
    return false;
  }
}

private async void btn_Send_Click(object sender, EventArgs e) {
  bool writeResult = await WriteToRegister();
}

At first, I'm using WriteSingleRegister for write in, but it's odd that it only change one bit/send, in my opinion, for example, if i send value 7 to register 100, it's should show 0111 (binary) on address 100 right away, but instead it shows 0001, i need to send value 7 again get 0011, and send again get 0111, shouldn't it became the value what I've send at first time?

but in this situation, program works fine with no exception, I'm also use Modbus Poll tool to check, read/write multiple register works fine, but writesingleregister perform the same, change one bit/send. so i change to use WriteMultipleRegister, and then the exception occurred. is that could be some step I'm missing?

0

There are 0 answers