Design web plate form procedure with modbus tcp by use flutter

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),
          ],
        ),
      ),
    );
  }
}

New contributor

Camiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Finally i solved with webscoket.

  1. Build a websocketserver by C#(i used) or node.js
  2. Let websocketserver handle modbus tcp <=> Flutter web_socket_chanel request.

It works for me.

New contributor

Camiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Design web plate form procedure with modbus tcp by use flutter

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),
          ],
        ),
      ),
    );
  }
}

New contributor

Camiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Finally i solved with webscoket.

  1. Build a websocketserver by C#(i used) or node.js
  2. Let websocketserver handle modbus tcp <=> Flutter web_socket_chanel request.

It works for me.

New contributor

Camiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật