gpt4 book ai didi

Flutter Modbus TCP/IP Connection close problem(Flutter Modbus TCP/IP连接关闭问题)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:46 26 4
gpt4 key购买 nike



I am trying to read and write data to modbus via TCP/IP. I can connect to modbus simulator successfully and read data. But after I cannot close the connection. App says connection closed but when I look at to modbus simulator it does not seem so. What can I do to close my connection?

我正在尝试通过TCP/IP将数据读写到modbus。我可以成功连接到modbus模拟器并读取数据。但在我无法关闭连接之后。应用程序显示连接已关闭,但当我查看modbus模拟器时,似乎并非如此。我该怎么办才能关闭连接?


Because, it's needed to close the connection to read data again and again. What is the solution?

因为,需要关闭连接才能一次又一次地读取数据。解决方案是什么?


import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:modbus/modbus.dart' as modbus;
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:korubin_modbus/utils/constants.dart';

class RegisterNotifier extends StateNotifier<List<int>> {
RegisterNotifier() : super([]) {
// Başlangıçta ve ardından belirli bir süre aralığında veri okuma işlemini başlatın
startReadingData();
}


// Belirli bir süre aralığında veri okuma işlemini başlatan fonksiyon
Future<void> startReadingData() async {
const duration = Duration(seconds: 2); // Veri okuma aralığı (2 saniye)
Timer.periodic(duration, (_) async {
await readData(); // Veri okuma işlemini çağırın
});
}

Future<String> getIpAddress() async{
final FlutterSecureStorage secureStorage = FlutterSecureStorage();
final String ipAddress = await secureStorage.read(key: Constants.ipAddressName) ?? "";
return ipAddress;
}


Future<void> readData() async {
// Veri okuma işlemini burada gerçekleştirin

final ipAddress = await getIpAddress();
if (ipAddress.isEmpty) {
print('IpAddress is empty');
return;
}

final client = modbus.createTcpClient(ipAddress, port: 502, mode: modbus.ModbusMode.rtu, timeout: Duration(seconds: 5));
try {
await client.connect();
print('Modbus bağlantı kuruldu');

final registers1 = await client.readHoldingRegisters(0, 100);
final registers2 = await client.readHoldingRegisters(100, 100);
final registers3 = await client.readHoldingRegisters(200, 100);

final List<int> updatedRegisters = [];
updatedRegisters.addAll(registers1);
updatedRegisters.addAll(registers2);
updatedRegisters.addAll(registers3);

state = updatedRegisters; // Veriyi güncelle
await client.close();
print('Okunan tag sayısı: ${updatedRegisters.length}');
} catch (e) {
print('Tag okurken hata: ${e.toString()}');
} finally {
// await client.close();
try {
await client.close();
print('Modbus connection kapatıldı');
} catch (e) {
print('Modbus connection kapatılırken hata oluştu: ${e.toString()}');
}
}
}


void writeData(int writeIpAddress, int value) async{
final ipAddress = await getIpAddress();
final client = modbus.createTcpClient(ipAddress, port: 502, mode: modbus.ModbusMode.rtu, timeout: Duration(seconds: 2));
try{
await client.connect();
print('Yazmak için modbus a bağlandı');

await client.writeSingleRegister(writeIpAddress - 400001, value);
print('Tag Address: $writeIpAddress - Veri yazıldı: $value');
} catch (e) {
print('Tag yazarken hata: ${e.toString()}');
} finally {
await client.close();
print('Modbus connection kapatıldı');
}
}
}

final registerProvider = StateNotifierProvider<RegisterNotifier, List<int>>((ref) => RegisterNotifier());


`

`


Modbus connection close problem

Modbus连接关闭问题


更多回答
优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com