Vik*_*ica 14 python thermal-printer epson point-of-sale
我正在尝试创建一个原型来将文本文件的位图数据打印到支持LAN的epson pos打印机TM-T88V.
虽然我没有问题发送文本和文本格式说明,我不明白,我必须做什么,让我的打印机打印Arecibo消息的数据.
前几行:
00000010101010000000000
00101000001010000000100
10001000100010010110010
10101010101010100100100
00000000000000000000000
00000000000011000000000
00000000001101000000000
00000000001101000000000
00000000010101000000000
00000000011111000000000
00000000000000000000000
11000011100011000011000
10000000000000110010000
11010001100011000011010
11111011111011111011111
00000000000000000000000
00010000000000000000010
00000000000000000000000
00001000000000000000001
Run Code Online (Sandbox Code Playgroud)
该消息有73行和23列,产生1679个图像元素.每个元素由1表示黑色或0表示为白色,应打印为8x8(或16x16)点的正方形.结果会导致
Arecibo消息http://www.satsig.net/seti/message-to-gliese-581.gif
从打印机的规格:
虽然 - 正如我所说 - 连接和发送到打印机是没有问题的,我只是不明白,这个指令想告诉我什么.在Arecibo消息的情况下会是什么
我必须向打印机发送什么号码?我需要发送每个点吗?什么nL, nH specify the number of dots of the image data in the horizontal direction as (nL + nH × 256).意思?
这是我用于原型设计的简单Python程序:
# -*- coding: utf-8 -*-
import struct
import socket
def sendInstructions(mySocket,l):
for x in l:
mySocket.send(struct.pack('h', *[x]),1)
def emphasizeOn(mySocket):
sendInstructions(mySocket,[27,33,48])
def emphasizeOff(mySocket):
sendInstructions(mySocket,[27,33,0])
def lineFeed(mySocket,number):
for i in range(number):
sendInstructions(mySocket,[0x0a,])
def paperCut(mySocket):
sendInstructions(mySocket,[29,86,0])
def sendText(mySocket,string):
mySocket.send(string.encode('UTF-8'))
def main():
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.connect(('192.168.1.15',9100))
lines = ["Hello,","World!"]
emphasizeOff(mySocket)
lineFeed(mySocket,2)
for l in lines:
if lines.index(l) == 0:
emphasizeOn(mySocket)
else:
emphasizeOff(mySocket)
sendText(mySocket,l)
lineFeed(mySocket,2)
lineFeed(mySocket,4)
paperCut(mySocket)
mySocket.close()
if __name__=="__main__":
main()
Run Code Online (Sandbox Code Playgroud)
此命令一次生成一个图像的水平条带.条带高8或24点,具体取决于m的值.
nL和nH是整数的低字节和高字节,指定水平条带的点的宽度.该宽度计算为nL + nH*256,因此如果您希望图像宽度为550点,则nH = 2且nL = 38.
参数d是位图数据; 如果图像条是8点高,那么每个字节代表条带中的一列.如果条带是24点高,那么三个字节代表一列.
所以,假设你有一个WxH numpy数组的int,1或0,你会:
data = np.zeros((W, H), dtype=np.ubyte)
## (fill in data here)
## Use m=33 since this is apparently the only mode with
## square pixels and also the highest resolution
## (unless it prints too slowly for your liking)
m = 33
nH = W // 256 ## note this is integer division, but SO's
## syntax hilighting thinks it looks like a comment.
nL = W % 256
## Divide the array into sections with shape Wx24:
for n in range(data.shape[1] // 24):
## Note that if the image height is not a multiple of 24,
## you'll have to pad it with zeros somehow.
strip = data[:, n*24:(n+1)*24]
## Convert each strip into a string of bytes:
strip = strip.reshape(W, 3, 8)
bytes = (strip * (2**np.arange(8)[np.newaxis, np.newaxis, :])).sum(axis=2) # magic
byteString = bytes.astype(np.ubyte).tostring()
## Send the command to POS
Run Code Online (Sandbox Code Playgroud)