printEscPos method

Future<Map<String, dynamic>> printEscPos(
  1. int vendorId,
  2. int productId,
  3. List<int> commandBytes, {
  4. int interfaceNumber = 0,
  5. int endpointAddress = 0x01,
  6. int readEndpointAddress = 0x81,
  7. int timeout = 5000,
  8. bool autoInitialize = false,
  9. bool autoCut = false,
  10. bool expectResponse = false,
})

Implementation

Future<Map<String, dynamic>> printEscPos(
  int vendorId,
  int productId,
  List<int> commandBytes, {
  int interfaceNumber = 0,
  int endpointAddress = 0x01,
  int readEndpointAddress = 0x81,
  int timeout = 5000,
  bool autoInitialize = false,
  bool autoCut = false,
  bool expectResponse = false,
}) async {
  List<int> finalCommands = [];

  // If automatic initialization is requested, add initialization command at the beginning
  if (autoInitialize) {
    // ESC @ - Initialize printer
    finalCommands.addAll([0x1B, 0x40]);
  }

  // Add main commands sent as parameters
  finalCommands.addAll(commandBytes);

  // If automatic cut is requested, add cut command at the end
  if (autoCut) {
    // GS V - Cut paper (full mode)
    finalCommands.addAll([0x1D, 0x56, 0x00]);
  }

  // Convert List<int> to Uint8List
  final data = Uint8List.fromList(finalCommands);

  // Attempt to send with various endpoints if the default fails
  Map<String, dynamic> result = await sendDataToPrinter(
    vendorId,
    productId,
    data,
    interfaceNumber: interfaceNumber,
    timeout: timeout,
    expectResponse: expectResponse,
  );

  // If the operation failed, try with an alternative endpoint
  if (result['success'] == false &&
      result['error']?.contains('transfer') == true) {
    log("Trying with alternative endpoint 0x02...");
    result = await sendDataToPrinter(
      vendorId,
      productId,
      data,
      interfaceNumber: interfaceNumber,
      timeout: timeout,
      expectResponse: expectResponse,
    );
  }

  return result;
}