python - 使用 Brother P950NW 的全页宽度

标签 python python-3.x printing escp

我想在我的 Brother P950NW 打印机中使用 18mm strip 的整个宽度来打印图像。目前,我使用的是 ESC/P(不是 ESC/POS,这台打印机似乎不支持),但如果不可能的话,我可以使用这台打印机支持的任何其他协议(protocol)。 (更新:用Brother的Windows软件,全角打印是可以的,但它使用LPR协议(protocol),似乎没有任何Python库。)

我正在使用密度为 72 的 ESC/P 命令 ESC*(根据 the printer's documentation 可用的最高值),它只允许以 48 点为步长填充宽度。

如何在 ESC/P-speak 高度为 200 的图像中打印 200 像素宽的 strip ?那应该很容易装到 strip 上。然而,由于 ESC*72 只接受 48 个 block ,超过 192 的所有内容都在另一个 strip 上输出。

这是我的演示代码:

import socket
import struct

def escp(density_code=72):
    stack_size_in_bytes = {72: 6}[density_code]
    height = 200
    width = 130

    yield b'\x1bia\x00'  # ESC/P command mode: ESC/P standard
    yield b'\x1b@'  # Initialize
    yield b'\x1bim\x00\x00'  # margin: 0
    yield b'\x1biXE2\x00\x00\x00'  # barcode margin: 0
    yield b'\x1b3' + struct.pack('!B', 24)  # line feed length: 24 dots (i.e. no space between lines)

    for y_offset in range(0, height, 8 * stack_size_in_bytes):
        yield b'\x1b*' + struct.pack('!B', density_code) + struct.pack('<H', width)
        yield b'\xff' * width * stack_size_in_bytes
        yield b'\x0a'  # linefeed (move position 24 dots down)
    yield b'\x0c' # Print start

c = socket.create_connection(('10.222.2.206', 9100))
c.sendall(b''.join(escp()))
c.close()

我对原始二进制文件的解决方案没问题;这是 the binary fileshortened hexdump由上述程序生成。

最佳答案

基于DOC的第8页,我们可以在打印一行之前指定打印位置,即使是图片也是一行一行打印。ESC $可以指定绝对水平位置和ESC J完成当前行的输入,然后将垂直打印位置向前移动n/180英寸。结合这两个,也许你可以使用所有234个可打印区域.

import socket
import struct

def escp(density_code=72):
    stack_size_in_bytes = {72: 6}[density_code]
    height = 200
    width = 130

    yield b'\x1bia\x00'  # ESC/P command mode: ESC/P standard
    yield b'\x1b@'  # Initialize
    yield b'\x1bim\x00\x00'  # margin: 0
    yield b'\x1biXE2\x00\x00\x00'  # barcode margin: 0
    yield b'\x1b3' + struct.pack('!B', 24)  # line feed length: 24 dots (i.e. no space between lines)

    for y_offset in range(0, height, 8 * stack_size_in_bytes):
        yield b'\x1b*' + struct.pack('!B', density_code) + struct.pack('<H', width)
        yield b'\xff' * width * stack_size_in_bytes
        # the added command ECS J
        yield b'\x1b4a' + struct.pack('!B', 42)
        # ESC $ is b'1b24'+struct.pack('!B', 0)+struct.pack('!B', 0)
        yield b'\x0a'  # linefeed (move position 24 dots down)
    yield b'\x0c' # Print start

c = socket.create_connection(('10.222.2.206', 9100))
c.sendall(b''.join(escp()))
c.close()

b'\x1b4a' + struct.pack('!B', 42)isESC J 向前进纸

ASCII: ESC J n

十进制:27 74 n

十六进制:1B 4A n

参数

0≤n≤255

描述

 完成当前行的输入,然后将垂直打印位置向前移动n/180英寸。

 如果n小于24,进给量为24/180英寸(约0.34厘米)。

b'1b24'+struct.pack('!B', 0)+struct.pack('!B', 0)是ESC $指定绝对水平位置

ASCII: ESC $ n1 n2

十进制:27 36 n1 n2

十六进制:1B 24 n1 n2

参数

0≤n1≤255, 0≤n2≤255

描述

 指定下一个数据的绝对打印位置(以1/60英寸为单位)。

 绝对打印位置指定从左边距开始的水平打印位置。

 下一个字符打印在距左边距 (n1 + 256 * n2)/60 英寸的位置。

 n1和n2可以指定的最大点数为1023/60英寸。

关于python - 使用 Brother P950NW 的全页宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929551/

相关文章:

python - 通过文本宽度获取单词的打印长度

python - Google App Engine 任务队列如何工作?

python -\Z 与字符串结尾不匹配

python - 批量大小如何影响神经网络的时间执行?

python - 如何在 Django ORM 中连接两个表而没有第一个表中的任何列引用第二个

python-3.x - python3请求使用quote而不是quote_plus

python - 奇点行为 : shell vs exec

python - 如何在 Pydantic 的严格类型(如 StrictStr)中使用验证器?

css - 用Bootstrap打印富文本,BR元素被IE完全忽略

printing - Argox OS214 中的自动换行文本 - PPLA