Pruebas de impresoras

This commit is contained in:
2025-12-29 16:05:01 +01:00
parent 03c5ee1244
commit a665703f46
2 changed files with 115 additions and 4 deletions

View File

@@ -0,0 +1,106 @@
import net from "net"
const PRINTER_IP = "192.168.1.254"
const PRINTER_PORT = 9100
// Definición de comandos especiales en Hexadecimal
const ESC = 0x1B;
const LF = 0x0A;
const FF = 0x0C;
function generateQRCode(data: string) {
const dataBuffer = Buffer.from(data);
const length = dataBuffer.length;
const dL = length & 0xFF; // Byte bajo de la longitud
const dH = (length >> 8) & 0xFF; // Byte alto de la longitud
return Buffer.concat([
Buffer.from([ESC, 0x69, 0x51]), // Comando QR Code (ESC i Q)
Buffer.from([0x02]), // n1: Tamaño de la celda (0x03 a 0x0B)
Buffer.from([0x00]), // n2: Símbolo (0x00 = Standard)
Buffer.from([0x00]), // n3: Structured Append (0x00 = No)
Buffer.from([0x02]), // n4: Error Correction (0x02 = M)
Buffer.from([0x00]), // n5: Data kind (0x00 = Manual)
Buffer.from([dL, dH]), // Longitud de los datos
dataBuffer // El contenido (google.com)
]);
}
const qrdata = "google.com"
function sendPTouchTemplate() {
const client = new net.Socket();
client.connect(PRINTER_PORT, PRINTER_IP, () => {
// Comandos P-Touch Template
const commands = Buffer.concat([
Buffer.from([0x1b, 0x69, 0x61, 0x03]), // 1. Entrar en modo P-touch Template
Buffer.from('^II'), // 2. Inicializar etiqueta
// Configuración de texto
Buffer.from('^ONProduct: ABC123^FS'), // Texto 1
Buffer.from('^ONPrice: $19.99^FS'), // Texto 2
Buffer.from('^ONDate: 2025-12-29^FS'),// Texto 3
// Código QR (Comando ^BQ)
// Parámetros: ^BQ, modelo, tamaño, error, datos
Buffer.from('^BQ,2,4,M,https://google.com^FS'),
Buffer.from('^FF'), // 3. Imprimir y Cortar (Form Feed)
]);
client.write(commands, () => {
console.log('Enviado en modo P-touch Template');
client.destroy();
});
});
}
function sendLabel() {
const client = new net.Socket();
client.connect(PRINTER_PORT, PRINTER_IP, () => {
console.log(`Conectado a la impresora en ${PRINTER_IP}`);
// Creamos un array de buffers para enviar los comandos crudos
const commands = Buffer.concat([
Buffer.from([ESC, 0x40]), // Inicializar (ESC @)
Buffer.from([ESC, 0x69, 0x61, 0x00]), // Modo ESC/P (ESC i a 0)
// Línea 1
Buffer.from([ESC, 0x24, 0x20, 0x00]), // Posición horizontal 32 dots (ESC $ nL nH)
Buffer.from('Product: ABC123\n'),
Buffer.from([LF]), // Salto de línea extra
generateQRCode(qrdata),
// Línea 2
Buffer.from([ESC, 0x24, 0x20, 0x00]),
Buffer.from('Price: $19.99\n'),
Buffer.from([LF]),
// Línea 3
Buffer.from([ESC, 0x24, 0x20, 0x00]),
Buffer.from('Date: 2025-12-29\n'),
Buffer.from([FF]) // Imprimir y expulsar (Form Feed)
]);
client.write(commands, () => {
console.log('Datos enviados correctamente.');
client.destroy(); // Cerramos la conexión después de enviar
});
});
client.on('error', (err) => {
console.error(`Error de conexión: ${err.message}`);
});
client.on('close', () => {
console.log('Conexión cerrada.');
});
}
sendLabel();
//sendPTouchTemplate()

View File

@@ -6,10 +6,15 @@ const PRINTER_PORT = 9100; // Standard raw printing port
// Simple ZPL label example
const zplData = `
^XA
^FO50,50^A0N,50,50^FDZSim Node Server Test^FS
^FO50,120^B3N,N,100,Y,N^FD123456^FS
^XZ
<ESC>@
<ESC>(U<01><00><0A>
<ESC>$<32><00>
Product: ABC123<LF>
<ESC>$<32><00>
Price: $19.99<LF>
<ESC>$<32><00>
Date: 2025-12-29<LF>
<FF>
`;
const client = new net.Socket();