sendTransaction method

  1. @override
void sendTransaction(
  1. String data,
  2. int timeout
)
override

timeout Milliseconds to wait.

Implementation

@override
void sendTransaction(String data, int timeout) async {
  if (_isTimerActive()) {
    logger?.log("Job is already running.");
    return;
  }

  final isOTGSupported = await _isUsbOTGSupported() ?? false;
  if (!isOTGSupported) {
    final error = PosConnectorException(
        PosConnectorException.usbOtgNotSupported,
        "Your system doesn't support USB OTG. The operation cannot be done.");
    logger?.logError(error);
    _callback?.onOperationError(error);
    return;
  }

  final usbDevice = await _getSupportedUsbDevice();
  if (usbDevice == null) {
    final error = PosConnectorException(PosConnectorException.noDeviceFound,
        "No USB device was found. You may need to enable USB OTG or re-insert the device.");
    logger?.logError(error);
    _callback?.onOperationError(error);
    return;
  }

  final isOpened = await _plugin?.connect(usbDevice, _baudRate) ?? false;
  if (!isOpened) {
    final error = PosConnectorException(
        PosConnectorException.openSerialPortError, "Unable to open port.");
    logger?.logError(error);
    _callback?.onOperationError(error);
    return;
  }
  logger?.log("Serial port opened.");
  _sendAndWaitForResponse(data, timeout);
}