I'm encountering an issue with a Node.js application that communicates with a serial port printer. When attempting to print non-English characters (such as accented letters or characters from languages like Korean or Chinese, the output appears broken or garbled. Print example
Technology Stack: Node.js for application development, utilizing a serial port library for communication with the printer. Problem: When sending non-English characters to the printer, the output is not rendered correctly and appears broken. Expected Behavior: The printer should correctly render non-English characters as it does with English characters. Attempts: I've attempted to encode the characters differently before sending them to the printer using various encoding methods, but none seem to produce the correct output.
Printer model = HMK-830M Libraries used = serialport (^11.0.0), iconv-lite(^0.6.3)
const { SerialPort } = require('serialport');
const iconv = require('iconv-lite');
const port = new SerialPort({
path: 'COM16',
baudRate: 19200,
stopBits: 1,
parity: 'none',
autoOpen: false,
flowControl: false
});
const printWithSerialPort = () => {
const printContent = async () => {
var command = '';
command += '\x1b@' // Reset Printer
command += '\x1ba\x00' // 80mm paper size
command += '\x1DL\x1D\x00' // 16mm left margin
command += '\x1B\x02' // Initial Line Spacing
command += '\x1b!FS' //COLLECTIVE SETTING OF KOREAN CHARACTER PRINTING_MODE
command += '\x1b&FS' // SET KOREAN CHARACTER MODE EXTENDED GRAPHIC MODE
command += iconv.encode('다른 사람들이 사용하기에 도움이 될 것입니다', 'euc-kr');
command += "\n\n\n\n";
command += '\x1bi' // PAPER CUT FULL
port.write(command, (err) => {
if (err) {
console.error('Error on write: ', err.message);
closePort()
} else {
closePort()
}
})
}
const openPort = () => {
if (!port.isOpen) {
port.open((err) => {
if (err) {
return console.error('Error opening port:', err.message);
}
console.log('Serial Port Opened');
printContent();
});
} else {
console.log('Port is already open')
printContent()
}
}
const closePort = () => {
if (port.isOpen) {
port.close((err) => {
if (err) {
return console.error('Error closing port:', err.message);
}
console.log('Serial Port Closed');
port.removeAllListeners('open');
port.removeAllListeners('data');
});
} else {
console.log('Port is already closed');
}
}
openPort()
}
printWithSerialPort()
I'm seeking guidance or insights into how I can properly encode or format non-English characters within Node.js for printing through a serial port, ensuring they are rendered correctly by the printer.
Any help, suggestions, or pointers in the right direction would be greatly appreciated! Thank you in advance.