Files
KCGL/inventory-backend/打印测试代码.py

56 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import socket
# ================= 配置区域 =================
PRINTER_IP = '192.168.9.89'
PRINTER_PORT = 9100
def print_barcode_no_text():
try:
# 1. 建立连接
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(5)
client.connect((PRINTER_IP, PRINTER_PORT))
print(f"✅ 已连接 {PRINTER_IP}")
# 2. 准备 TSPL 指令
commands = (
"SIZE 40 mm, 30 mm\r\n"
"GAP 2 mm, 0 mm\r\n"
"CLS\r\n"
"DIRECTION 1\r\n"
# 保持之前的居中设置 (向右偏移 3mm)
"REFERENCE 25, 0\r\n"
# --- 条形码 (不显示数字) ---
# 关键修改第5个参数从 1 改为了 0
# BARCODE x,y,"128",height,human_readable(0=不显),rot,narrow,wide,"content"
"BARCODE 20,10,\"128\",80,0,0,2,2,\"00000002\"\r\n"
# --- 文本内容 (保持字号 3) ---
# 第一行 Y=110
"TEXT 0,110,\"3\",0,1,1,\"N:test01\"\r\n"
"TEXT 150,110,\"3\",0,1,1,\"S:test01\"\r\n"
# 第二行 Y=140
"TEXT 0,140,\"3\",0,1,1,\"T:test01\"\r\n"
"TEXT 150,140,\"3\",0,1,1,\"K:test01\"\r\n"
# 第三行 Y=170
"TEXT 0,170,\"3\",0,1,1,\"L:test01\"\r\n"
"TEXT 150,170,\"3\",0,1,1,\"B:test01\"\r\n"
"PRINT 1,1\r\n"
)
client.sendall(commands.encode('gbk'))
client.close()
print("✅ 条码数字已隐藏,指令发送成功")
except Exception as e:
print(f"❌ 失败: {e}")
if __name__ == "__main__":
print_barcode_no_text()