All
I would like to use flutter to design a modbus tcp procedure on web plate form. But i don't know how to start. I saw the package( https://pub.dev/packages/modbus)(https://pub.dev/packages/modbus_client),and want to try it, but it didn't work on web plate form. Could any one show me a simple example or have experience to share, thank you.
I tried use package modbus . But didn't work on web plate form. The below code can used on windows plateform,but if run debug on chrome,connect fail:
import 'package:flutter/material.dart';
import 'package:modbus_client/modbus_client.dart';
import 'package:modbus_client_tcp/modbus_client_tcp.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _ipController = TextEditingController();
final TextEditingController _portController = TextEditingController();
final TextEditingController _registerController = TextEditingController();
String _result = '';
Future<void> _readHoldingRegister() async {
final ip = _ipController.text;
final port = int.tryParse(_portController.text) ?? 502;
final registerAddress = int.tryParse(_registerController.text) ?? 0;
var battery = ModbusInt16Register(
name: "Battery",
type: ModbusElementType.inputRegister,
address: 4098,
uom: "V",
multiplier: 0.1,
onUpdate: (self) => print(self));
try {
// Initialize Modbus client
var client = ModbusClientTcp (
ip,
serverPort: port,
unitId: 15, // unitId
responseTimeout :const Duration(seconds: 3),
connectionMode: ModbusConnectionMode.autoConnectAndDisconnect,
);
// Connect to the Modbus server
final connected = await client.connect();
if (!connected) {
setState(() {
_result = 'Failed to connect to the server';
});
return;
}
// Create a Modbus request to read holding registers
final request = battery.getReadRequest();
// Send the request
final responseCode = await client.send(request);
// Check the response code
if (responseCode == ModbusResponseCode.requestSucceed) {
// Read the register value
final result = await client.send(request);
setState(() {
_result = 'Register value: ${battery.value}';
});
} else {
setState(() {
_result = 'Error: ${responseCode.code}';
});
}
// Disconnect after request
await client.disconnect();
} catch (e) {
setState(() {
_result = 'Exception: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Modbus TCP Reader'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _ipController,
decoration: InputDecoration(labelText: 'IP Address'),
),
TextField(
controller: _portController,
decoration: InputDecoration(labelText: 'Port'),
keyboardType: TextInputType.number,
),
TextField(
controller: _registerController,
decoration: InputDecoration(labelText: 'Register Address'),
keyboardType: TextInputType.number,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _readHoldingRegister,
child: Text('Read Register'),
),
SizedBox(height: 20),
Text(_result),
],
),
),
);
}
}
Finally i solved with webscoket.
It works for me.