start method

void start(
  1. SendPort setupSendPort
)

Implementation

void start(SendPort setupSendPort) async {
  _log('opening port');
  copyStringToBuf(portPath);
  ttyFd = dylib.openPortMth(buf255);
  if (ttyFd! <= 0)
    throw 'open port failed: $ttyFd, ${dylib.lastErrorMth()}, $portPath';

  cmdPort.listen(processCmds);
  setupSendPort.send(cmdPort.sendPort);

  // Read and forward any messages from the port
  while (ttyFd != null) {
    _log('reading...');
    var count = dylib.readBytesMth(ttyFd!, 255, buf255);
    if (count > 1) {
      var rxData = Uint8List(count);
      for (var index = 0; index < count; index++) //
        rxData[index] = buf255.elementAt(index).value;
      _log('  read: $rxData');
      notificationSendPort.send(rxData);
    } else if (count == 1) {
      var byteValue = buf255.elementAt(0).value;
      _log('  read: $byteValue');
      notificationSendPort.send(byteValue);
    } else if (count == 0) {
      _log('  sleep before trying to read again');
      await Future.delayed(const Duration(milliseconds: 100));
    } else {
      var byteValue = buf255.elementAt(0).value;
      var lastError = dylib.lastErrorMth();
      _log('  read error: $byteValue, $lastError');
      notificationSendPort.send(byteValue);
      notificationSendPort.send(lastError);
    }
  }
  _log('exit read loop');
}