sendRawFrame method

  1. @override
Future<Uint8List> sendRawFrame(
  1. Uint8List aduRequest
)
override

Send raw frame to the remote server

Implementation

@override
Future<Uint8List> sendRawFrame(Uint8List aduRequest) async {
  if (_socket == null) {
    await connect();
  }

  _socket!.add(aduRequest);
  await _socket!.flush();

  // Read MBAP header first
  final headerData = await _readBytes(tcpHeaderMbapSize);

  // Extract length from header
  final length =
      ByteData.sublistView(headerData).getUint16(4, Endian.big);

  if (length <= 0) {
    throw Exception(
        'modbus: length in response header \'$length\' must not be zero');
  }
  if (length > (tcpAduMaxSize - (tcpHeaderMbapSize - 1))) {
    throw Exception(
        'modbus: length in response header \'$length\' must not be greater than \'${tcpAduMaxSize - tcpHeaderMbapSize + 1}\'');
  }

  // Read the rest of the data
  final totalLength = tcpHeaderMbapSize + length - 1;
  final restData = await _readBytes(totalLength - tcpHeaderMbapSize);

  final result = Uint8List(totalLength);
  result.setRange(0, tcpHeaderMbapSize, headerData);
  result.setRange(tcpHeaderMbapSize, totalLength, restData);

  return result;
}