cabbi / modbus_client_tcp

BSD 3-Clause "New" or "Revised" License
7 stars 3 forks source link

data coil read #5

Closed hasanuc20 closed 8 months ago

hasanuc20 commented 8 months ago

Hello, how can I read the data of the coil or data? I want to read bool variable data with address 1, but I couldn't.

cabbi commented 8 months ago

This is an example for reading bit/bool values

import 'package:logging/logging.dart';
import 'package:modbus_client/modbus_client.dart';
import 'package:modbus_client_tcp/modbus_client_tcp.dart';

void main() async {
  // Simple modbus logging
  ModbusAppLogger(Level.FINE);

  // Create a modbus int16 register element
  var bitValue = ModbusCoil(name: "My Bit", address: 1);

  // Create the modbus client.
  var modbusClient = ModbusClientTcp("127.0.0.1", unitId: 1);

  // Send a read request from the element
  await modbusClient.send(bitValue.getReadRequest());
  print(bitValue.value);

  // Ending here
  modbusClient.disconnect();
}

Looking to the implementation I see I forgot to add the "onUpdate" parameter to Coil and Descrete Input register. I will do a new package release to handle it. With that new release the code can look like this:

  // Create a modbus int16 register element
  var bitValue =
      ModbusCoil(name: "My Bit", address: 1, onUpdate: (self) => print(self));

Please close the issue if the example fits your requirements, thanks

cabbi commented 8 months ago

New modbus_client 1.0.4 is available

hasanuc20 commented 8 months ago

Thank you.