Skip to content

Instantly share code, notes, and snippets.

@kcchien
Forked from gourneau/zpl-tcp.py
Last active October 11, 2023 21:38
Show Gist options
  • Save kcchien/7bc35392bd4235285ee4078f02b687be to your computer and use it in GitHub Desktop.
Save kcchien/7bc35392bd4235285ee4078f02b687be to your computer and use it in GitHub Desktop.
Printing ZPL over TCP/IP Example Python , Node.js 如何透過TCP/IP 直接送出ZPL檔到條碼機列印
const net = require('net'); // 引入網路 (Net) 模組
const HOST = '10.10.10.168';
const PORT = 9100;
let zpl = `
^XA
^FO150,40^BY3
^BCN,110,Y,N,N
^FD123456^FS
^XZ
`
// 使用 net.connect() 方法,建立一個 TCP 用戶端 instance
let client = net.connect(PORT, HOST, ()=>{
console.log('Printing labels...');
// 向伺服器端發送資料,該方法其實就是 socket.write() 方法,因為 client 參數就是一個通訊端的物件
client.write(zpl);
client.end();
});
// data 事件
client.on('data', (data)=>{
console.log(data.toString());
// 輸出由 client 端發來的資料位元組長度
console.log('socket.bytesRead is ' + client.bytesRead);
// 在列印輸出資料後,執行關閉用戶端的操作,其實就是 socket.end() 方法
client.end();
});
// end 事件
client.on('end', ()=>{
console.log('client disconnected');
});
#!/usr/bin/env python
# ZPL docs can be found at https://support.zebra.com/cpws/docs/zpl/zpl_manual.pdf
# This works with Python 3, change the bytes to str if you are using Python 2
import socket
# One easy way to find the IP address is with this nmap command
# nmap 192.168.0.* -p T:9100 --open
IP = '10.10.10.168'
TCP_PORT = 9100
BUFFER_SIZE = 1024
# This will print a code 128 barcode
zpl = """
^XA
^FO150,40^BY3
^BCN,110,Y,N,N
^FD123456^FS
^XZ
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, TCP_PORT))
s.send(bytes(zpl, "utf-8"))
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment