open method

  1. @override
Future<void> open()
override

Open the transport (idempotent).

Implementation

@override
Future<void> open() async {
  if (_open) return;
  if (_closed) throw StateError('PcanCanTransport already closed');

  if (!Platform.isWindows) {
    throw UnsupportedError(
        'PcanCanTransport: production target is Windows; on Linux use '
        'SocketCanTransport with PEAK kernel driver instead');
  }

  final bindings = PcanBindings.load(overridePath: libraryPath);
  _bindings = bindings;

  final int initStatus;
  if (enableFd) {
    // CAN_InitializeFD signature: TPCANStatus(TPCANHandle, LPSTR)
    // — load via additional lookup since classic Initialize binding
    // has a different signature.
    throw UnsupportedError(
        'PcanCanTransport: enableFd=true not yet wired (CAN_InitializeFD '
        'binding requires LPSTR allocation — track separately)');
  } else {
    initStatus = bindings.canInitialize(channel, baudrate, 0, 0, 0);
  }
  if (initStatus != PcanStatus.ok) {
    throw StateError(
        'CAN_Initialize failed: status=0x${initStatus.toRadixString(16)} '
        'channel=0x${channel.toRadixString(16)} '
        'baud=0x${baudrate.toRadixString(16)}');
  }

  _rxMsg = malloc<TpcanMsg>();
  _rxMsgFd = enableFd ? malloc<TpcanMsgFd>() : null;
  _rxTs = malloc<TpcanTimestamp>();
  _rxCtrl = StreamController<CanFrame>.broadcast();
  _open = true;
  _startPolling(bindings);
}