Epson ESCPOS 使用 ESC R n 命令打印丹麦字符

Epson ESCPOS print Danish character using ESC R n command

我使用的是 Epson TM-T20II 热敏收据打印机,我需要打印带有丹麦语字符 (æ,ø,å) 的收据。对用于字符语言选择的 ESCPOS 代码进行了描述 here。我的 Python 代码如下

import win32print
import six
#create some raw data
rawdata = b'\x1b\x40' #Instantiate the printer ESC @
rawdata = rawdata + b'\x1b\x52\x10' #Select the Danish II character set as in documentation ESC R 
where n = 10
rawdata = bytes('Print æ,ø,å', 'utf-8') + b'\n' + b'\x1d\x56' + six.int2byte(66) + b'\x00' #print 
some text and cut

#Creating the printing job in Windows 10 and send the raw text to the printer driver
printer = win32print.OpenPrinter('EPSON TM-T20II Receipt')
hJob = win32print.StartDocPrinter(printer, 1, ("Test print", None, "RAW"))
win32print.WritePrinter(printer, rawdata)
win32print.EndPagePrinter(printer)
win32print.ClosePrinter(printer)

我的问题是打印出一些奇怪的字符。我还通过按住进纸按钮并打开打印机电源将打印机设置为 Danish II。我错过了什么?

目前,您可能想尝试将下面的编码规范从 utf-8 更改为 cp865

rawdata = bytes('Print æ,ø,å', 'utf-8') + b'\n' + b'\x1d\x56' + six.int2byte(66) + b'\x00' #print  

如果这不起作用,您应该停止使用 win32print 并切换到 pyserial。
还需要切换打印机模式,卸载Advanced Printer Driver,安装打印机串口驱动
然后应用程序需要使用原始 ESC/POS 命令创建所有打印数据。

原因如下。


您可以在此处获取TM-T20II 的高级打印机驱动程序以及手册和示例程序。
EPSON Advanced Printer Driver for TM-T20II

根据示例程序"Step 1 Printing Device Font",为了向打印机发送原始ESC/POS 命令,需要select 特定的设备字体。

Prints "Hello APD" with a device font and autocuts a receipt.

C++示例源码核心如下。

CDC dc;
/*
 * Create the device context for the printer
 */
if(! dc.CreateDC(EPS_DRIVER_NAME, EPS_PRINTER_NAME, NULL, NULL) )
{
    AfxMessageBox(_T("Printer is not available."));
    return;
}

dc.StartDoc(&di);

/*
 * Perform the printing of the text
 */
CFont font, *old;
font.CreatePointFont(95, "FontA11", &dc);
old = dc.SelectObject(&font);
dc.TextOut(20, 10, "Hello APD!");
dc.SelectObject(old);
font.DeleteObject();

dc.EndPage();
dc.EndDoc();
dc.DeleteDC();

这是 VB 中的样子。

Dim printFont As New Font("Lucida Console", 8, FontStyle.Regular, GraphicsUnit.Point) ' Substituted to FontA Font

e.Graphics.PageUnit = GraphicsUnit.Point

' Print the string at 6,4 location using FontA font.
e.Graphics.DrawString("Hello APD!", printFont, Brushes.Black, 6, 4)

' Indicate that no more data to print, and the Print Document can now send the print data to the spooler.
e.HasMorePages = False

将这些移植到 Python 的 win32print 不是很难或不可能吗?
win32print API 似乎没有能力在打印过程中自定义字体。
Module win32print

而StartDocPrinter和WritePrinter有如下解释
win32print.StartDocPrinter

Note that the printer driver might ignore the requested data type.

win32print.WritePrinter

Suitable for copying raw Postscript or HPGL files to a printer.

ESC/POS 命令不是原始 PostScript 或 HPGL,EPSON 的高级打印机驱动程序不一定会通过 win32print 调用发送此类数据。