0
I am performing a socket communication using flutter in a Modbus equipment, I need to send several readings on the equipment, but one overlaps the other before the answer of the previous socket. I tried several ways, but I can’t fire automatically. Just in a few clicks on a button. I tried to add a loop. But it only sends several requests and only gets the last answer. But click the click works. Someone help me??
I’m using Dart and Flutter’s IO class.
sleep(const Duration(milliseconds: 300));
Uint8List request = preparaCMDUint8list(counter, 3, 1, '16', null);
await Future.wait([xhandle(request)]).then((_) {
print('acabou o envio $_');
// sleep(const Duration(milliseconds: 200));
// setState(() {});
}).whenComplete(() {});
Uint8List request = preparaCMDUint8list(counter, 3, 1, '17', null);
await Future.wait([xhandle(request)]).then((_) {
print('acabou o envio $_');
// sleep(const Duration(milliseconds: 200));
// setState(() {});
}).whenComplete(() {});
This is the function that sends the socket
Future xhandle(Uint8List _request) async {
conectou = false;
await Socket.connect(connectionProvider.getEnderecoIpConnect(),
int.parse(await connectionProvider.getEnderecoPorta()))
.then((Socket sock) {
conectou = true;
_socket = sock;
_socket.listen(_onData,
onError: errorHandler, onDone: doneHandler, cancelOnError: true);
sleep(const Duration(milliseconds: 100));
}).then((_) {
conectou = true;
print('Enviou, then do socket: $_request');
print(_request); // so that your code runs AFTER the TCP/IP connection
// _socket.write(_request);
//counter++;
print('Passou no timer eviou o request');
print(DateTime.now());
sleep(const Duration(milliseconds: 200));
_socket.add(_request);
sleep(const Duration(milliseconds: 400));
_socket.close();
}).whenComplete(() {
if (!conectou) {
conectou = false;
_socket.close();
print('erro do sockets');
sleep(const Duration(milliseconds: 100));
}
});
}
There are other functions that I believe are not necessary, such as the onDone
, onData
and onError
Sleep are attempts to delay the code so one request does not swallow the other
What does it cost to edit a question and wait for it to be reopened? Try not to pollute the site...
– Matheus Ribeiro