printEscPos method
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,
})
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 = [];
// Si se solicita inicialización automática, añadir comando de inicialización al principio
if (autoInitialize) {
// ESC @ - Inicializar impresora
finalCommands.addAll([0x1B, 0x40]);
}
// Añadir los comandos principales enviados por parámetro
finalCommands.addAll(commandBytes);
// Si se solicita corte automático, añadir comando de corte al final
if (autoCut) {
// GS V - Cortar papel (modo total)
finalCommands.addAll([0x1D, 0x56, 0x00]);
}
// Convertir List<int> a Uint8List
final data = Uint8List.fromList(finalCommands);
// Intentar enviar con varios endpoints si el predeterminado falla
Map<String, dynamic> result = await sendDataToPrinter(
vendorId,
productId,
data,
interfaceNumber: interfaceNumber,
endpointAddress: endpointAddress,
readEndpointAddress: readEndpointAddress,
timeout: timeout,
expectResponse: expectResponse,
);
// Si la operación falló, intentar con un endpoint alternativo
if (result['success'] == false &&
result['error']?.contains('transferencia') == true) {
log("Intentando con endpoint alternativo 0x02...");
result = await sendDataToPrinter(
vendorId,
productId,
data,
interfaceNumber: interfaceNumber,
endpointAddress: 0x02,
readEndpointAddress: readEndpointAddress,
timeout: timeout,
expectResponse: expectResponse,
);
}
return result;
}