본문 바로가기

서버

node-thermal-printer 물리 영수증 구현 적용기

728x90

키오스크 프로젝트를 진행하면서 물리 영수증을 구현하기로 했다.

물리 영수증은 주방, 계산서, 영수증으로 나뉘고 

세가지를 node thermal printer로 구현해보았다. 

 

순서는 다음과 같다.

1. Thermal Printer 셋팅

2. Node.js로 구현

3. 추가내용

 


1. Thermal Printer 셋팅

 

 

포트는 5개가 있는데

파워, USB포트 두 가지를 사용한다.

출력 준비중인 포스기

 

USB를 연결하면 컴퓨터에서 연결된 포트를 찾을 수 있다.

 

윈도우키 -> 설정 -> 기타 디바이스

 

COM3 포트에 연결됨을 확인할 수 있다.

 


2. Node.js로 구현

const { SerialPort } = require('serialport');
const iconv = require('iconv-lite');

const port = new SerialPort({
  path: 'COM3',  // Change this to match your printer's port
  baudRate: 9600 // Adjust if needed
});

// ESC/POS Command to Initialize Printer
const initPrinter = Buffer.from([0x1B, 0x40]); // ESC @

// Convert Korean text to CP949
const koreanText = "안녕하세요 ThermalPrinter 테스트 중입니다...\n\n";
const encodedText = iconv.encode(koreanText, 'cp949'); 

port.write(initPrinter, (err) => {
  if (err) console.error('Error:', err.message);
  else console.log('Printer initialized');
});

// Send Korean text
port.write(encodedText, (err) => {
  if (err) console.error('Error:', err.message);
  else console.log('Korean text sent to printer');
});

 

 

터미널에 다음과 같이 명령한다.

node test.js

 

 

결과모습

 

 


3. 추가내용

 

- ESC/POS command

- CP949 인코딩

- Baud Rate

 


 

3.1 - ESC/POS command

https://download4.epson.biz/sec_pubs/pos/reference_en/escpos/commands.html

 

Commands in Code Order - TM Printer - ESC/POS Command - Tech.Reference - POS - Epson

Quick Access [Name] [Format] [Range] [Description] [Notes] ESC/POS® includes patented or patent pending commands. The contents must not be disclosed to third parties.

download4.epson.biz

 

Thermal Printer에서 사용하는 표준 명령어이다. 

특정 명령어를 명령하지 않을 경우

계속 버퍼에 쌓이고 출력하지 않는다.

 

const initPrinter = Buffer.from([0x1B, 0x40]); // ESC @

 

위 코드를 보면

"ESC@" 로 프린터를 초기화 시켜준다. 

 

프린터에게 이제 시작임을 알리는 것이다.

 

3.2 - CP949

CP949은 EUC-KR 인코딩 확장이다. 

 

3.3 - baud rate

js conf 22

2bps 이냐 4bps냐에 따라서 작동 여부를 결정할 수 있다. 

 

나는 1초에 2비트를 보내는데

기계는 1초에 4비트를 받고있으면

서로의 값은 다르다

 

나: 00

기계: 0000

 

서로 속도를 맞춰야 한다.

 


마무리

바닐라 js로 구현하고 있어서 

컴포넌트화가 필요하다. 

 

영수증 레이아웃에 맞게 구현 후

해당 포스팅에 덧붙여서 

적용기를 써나갈 예정이다.