An ESC/POS Network & Serial Printer Emulator by XETRON (xelth.com/xetron)
This Node.js application emulates an ESC/POS (Epson Standard Code for Printers) printer. Instead of printing to physical paper, it captures the raw ESC/POS data sent by a Point of Sale (POS) system or other application, parses it, and saves the output as structured text files containing both the printable text and tagged ESC/POS commands. It can listen for print jobs via TCP/IP network connections and/or serial (COM) ports, providing distinct audible notifications for each.
- Network Listener: Acts as a TCP server listening on a configurable printer port (default: 9100).
- Serial Port Listener: Can listen on a specified serial (COM) port (e.g., COM2, COM5) at a configured baud rate.
- Distinct Audible Notifications: Produces different sound alerts for data received via TCP versus Serial, using PowerShell's
[Console]::Beep()
for reliable sound on Windows without triggering common intrusive system sounds. Includes a sound queueing mechanism for events arriving in rapid succession. - ESC/POS Parsing: Implements a custom parser to interpret incoming ESC/POS byte streams.
- Text Extraction: Decodes and extracts printable text from the ESC/POS data, attempting to respect character encodings specified by
ESC t n
commands (with support for CP437, CP850, CP866, Windows-1251, Windows-1252, etc.). - Command Tagging: Identifies common ESC/POS commands and represents them as human-readable tags (e.g.,
<Initialize Printer>
,<Line Feed>
,<Partial Cut>
) interspersed with the extracted text. - File Output: For each print job received (from TCP or Serial):
- Saves the original raw binary ESC/POS data (
*_pos-input-original.bin
). - Saves the parsed output (text + command tags) as a UTF-8 encoded text file (
*_receipt-rich-text_UTF-8.txt
). - Saves a plain text version (tags removed, basic newlines preserved) re-encoded into Windows-1251 (
*_receipt-plain-text_WINDOWS-1251.txt
).
- Saves the original raw binary ESC/POS data (
- Console Logging: Provides real-time logging of client connections (TCP) or serial port activity, data reception, sound notifications, and a formatted version of the parsed rich text output for immediate review.
- Organized File Naming: Output files for each print job are prefixed with a common ISO timestamp and source identifier (IP address or COM port) for easy correlation.
- Configurable Listeners: TCP and Serial listeners can be enabled/disabled individually via flags in the
emulator.js
script. - Robust Error Handling: Attempts to continue running other listeners if one fails (e.g., COM port busy but TCP listener can still operate).
This emulator is designed for:
- Debugging POS Systems: Understanding the exact ESC/POS commands a POS system sends, with clear audible distinction for network or serial data.
- Developing Printer-Interfacing Applications: Testing applications that generate ESC/POS without needing a physical printer.
- Data Analysis for LLMs: Providing structured textual representations of receipts for analysis by Large Language Models.
- Troubleshooting Printer Issues: Comparing expected ESC/POS output with what a physical printer produces.
- Testing Serial Port Communication: Emulating a serial-connected printer for applications that use COM ports.
- The Node.js script starts listeners for TCP/IP and/or Serial (COM) ports based on configuration.
- A POS system or application sends print jobs to the emulator's TCP endpoint or configured COM port.
- Upon receiving data, the emulator:
- Plays a distinct audible notification (customizable frequency/duration via PowerShell
[Console]::Beep()
) indicating the source (TCP or Serial). A sound queue handles nearly simultaneous events to ensure each notification is played sequentially. - Processes the data at the end of the TCP connection or after a timeout for serial data.
- Plays a distinct audible notification (customizable frequency/duration via PowerShell
- The custom parser (
parseEscPosToRichText
) processes the received byte buffer, converting commands to tags and decoding text. - The structured "rich text" is logged to the console and saved to files, along with the original raw binary data and a plain text Windows-1251 version.
For each print job, files are generated in the receipts_output
directory (e.g., 2025-06-13T10-21-38-012Z_TCP-192.168.13.60_51711_...
):
..._pos-input-original.bin
: Raw binary ESC/POS data...._receipt-rich-text_UTF-8.txt
: Parsed text with command tags (UTF-8)...._receipt-plain-text_WINDOWS-1251.txt
: Plain text version (Windows-1251).
- Node.js (e.g., v18.x or as targeted by
pkg
) - npm (for installing dependencies)
- Windows Operating System (for PowerShell
[Console]::Beep()
functionality. For other OS, sound notification might fallback or be disabled). iconv-lite
(for character encoding, included in dependencies)serialport
(for serial communication, included in dependencies)
-
Clone the repository or download the files.
-
Navigate to the project directory in your terminal.
-
Install dependencies:
npm install
-
Configure Listeners & Sound (Optional): Open
emulator.js
and adjust constants at the top:ENABLE_TCP_LISTENER
,TCP_PORT
ENABLE_SERIAL_LISTENER
,SERIAL_PORT_PATH
,SERIAL_BAUD_RATE
SOUND_BASE_COOLDOWN_MS
: Cooldown between sound notifications.- Sound parameters (frequency, duration) within the
SoundNotifier
class methods (playTcpSound
,playSerialSound
) can be tweaked for preference. OUTPUT_DIR_NAME
.
-
Setting up Virtual COM Ports for Local Testing (Optional): If sending data from another application on the same machine to the emulator's serial listener, use a virtual COM port pair tool like
com0com
for Windows (ensure signed drivers for Win10/11) orsocat
for Linux. (See previousreadme.md
versions for detailedcom0com
/socat
setup if needed, or thefirewall.md
forcom0com
example commandsetupc.exe install PortName=COM1 PortName=COM2
).
If using the TCP listener and connecting from other machines, allow the application through Windows Defender Firewall. Serial (COM) port access typically doesn't require firewall rules.
Administrator Privileges Required for modifying firewall rules.
How to open Command Prompt/PowerShell as Administrator:
- Windows Key, type
cmd
orpowershell
. - Right-click -> "Run as administrator".
- UAC: Click "Yes".
Creating Firewall Rule (Command Prompt, as Administrator):
(Replace 9100
with TCP_PORT
if changed)
netsh advfirewall firewall add rule name="ESC/POS Emulator (TCP 9100)" dir=in action=allow protocol=TCP localport=9100
To check: netsh advfirewall firewall show rule name="ESC/POS Emulator (TCP 9100)"
To delete: netsh advfirewall firewall delete rule name="ESC/POS Emulator (TCP 9100)"
- Open terminal/command prompt in the project directory.
- Run:
node emulator.js
- Console shows listener status, IP addresses, output directory.
- Configure your sending application to the emulator's IP:Port (TCP) or COM port (Serial).
- Data reception triggers console logs, file saving, and distinct audible alerts.
- Press
Ctrl+C
to stop.
Use pkg
to package into a standalone executable.
- Install
pkg
globally:npm install -g pkg
- From project directory:
- Windows x64:
npm run build
(createsdist/eckPos-emulator.exe
) - All platforms (Win, Linux, macOS x64):
npm run build-all
- Windows x64:
The executable is self-contained. receipts_output
is created relative to the executable. PowerShell must be available on the target Windows system for sound notifications to work as intended.
- "Port X is already in use" (TCP): Change
TCP_PORT
or stop the other application. - "Could not open port COMx" (Serial): Port in use, non-existent, or permission issue. Check virtual COM setup if used.
- No sound (Windows):
- Ensure PowerShell is installed and accessible.
- Check system volume and audio output device.
- The
execAsync
command for PowerShell might be failing; check console for[SOUND] PowerShell beep failed
errors. This could be due to execution policies or PowerShell being unavailable in very restricted environments.
- No files created: Check console for directory/file write permission errors. Ensure data is being sent.
- Incorrect text decoding:
initialEncoding
in parser orESC t n
commands might need review. - Firewall blocking (TCP): Ensure rule is correctly added for external connections.