diff --git a/src/impresoras/printer-ejemplo-brother.ts b/src/impresoras/printer-ejemplo-brother.ts new file mode 100644 index 0000000..7ba3cec --- /dev/null +++ b/src/impresoras/printer-ejemplo-brother.ts @@ -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() diff --git a/src/printer-ejemplo.ts b/src/impresoras/printer-ejemplo.ts similarity index 79% rename from src/printer-ejemplo.ts rename to src/impresoras/printer-ejemplo.ts index b7b2064..a2941be 100644 --- a/src/printer-ejemplo.ts +++ b/src/impresoras/printer-ejemplo.ts @@ -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 +@ +(U<01><00><0A> +$<32><00> +Product: ABC123 +$<32><00> +Price: $19.99 +$<32><00> +Date: 2025-12-29 + `; const client = new net.Socket();