使用 JavaScript 向打印机发送十六进制命令



我正在尝试将 ESC 序列发送到收据打印机以触发现金抽屉打开。我是Javascript的新手,所以我只想检查我做得对。打印机/现金抽屉没有任何反应,尽管我可能发送了错误的代码序列。我知道抽屉确实打开了,因为我可以使用打印机测试软件触发它打开。

这是Javascript。

var windowUrl = 'about:blank';
var uniqueName = new Date();    
var windowName = 'CloseTillPrint' + uniqueName.getTime();
var PrintWindow = window.open(windowUrl, windowName, 'left=300,top=100,width=200,height=900');      
PrintWindow.document.open('text/plain')
PrintWindow.document.write(0x1B); 
PrintWindow.document.write(0x70);
PrintWindow.document.write(0x30); 
PrintWindow.document.write(0x37); 
PrintWindow.document.write(0x79);
PrintWindow.document.close();
PrintWindow.focus();
PrintWindow.print();
PrintWindow.close();    

如果您可以确认我没有对上面的代码犯任何错误,我将假设我发送了错误的十六进制序列。谢谢

您打印的是数字,而不是字符。使用以下命令代替write()命令:

PrintWindow.document.write(String.fromCharCode(0x1B, 0x70, 0x30, 0x37, 0x79));

最新更新