Last active
January 15, 2018 07:28
-
-
Save sepastian/5735775 to your computer and use it in GitHub Desktop.
Print a label on a Zebra TLP 2844 in Ruby. The printer is EPL/2 capable and connected via USB.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print a label on a Zebra TLP 2844 in Ruby. | |
# The printer is EPL/2 capable and connected via USB. | |
# EPL/2 reference: http://www.zebra.com/apps/dlmanager?dlp=-227178c9720c025483893483886ea54a70963bb77ca94fcc1d65ce9394326ed960e43d023beba35831d5d9bfc1740296347157b5024977a&c=gb&l=en | |
# https://github.com/larskanis/libusb | |
require 'libusb' | |
# Select the printer by vendor ID and product ID. | |
# To get a list of connected devices, use 'lsusb'. | |
# 0a5f:0x000a identifies the Zebra TLP 2844. | |
usb = LIBUSB::Context.new | |
device = usb.devices(:idVendor => 0x0a5f, :idProduct => 0x000a).first | |
# Both an interface and endpoint must be selected on the printer. | |
# Each interface of an USB device can have one or more endpoints. | |
# Here is how to print information about interfaces and endpoints. | |
# (Use interface 0 and endpoint 6 for printing on the TLP 2844.) | |
# p device.interfaces | |
# p device.endpoints | |
# Open interface 0. | |
# The kernel might block the interface, try to release it, if so. | |
handle = device.open | |
begin | |
handle.claim_interface(0) | |
rescue LIBUSB::ERROR_BUSY | |
# Tell the kernel to release interface 0. | |
handle.detach_kernel_driver(0) | |
handle.claim_interface(0) | |
end | |
# Construct an EPL2 command. | |
# NOTE: append "\n" to the last command, "P1\n", | |
# otherwise, the printer won't start printing! | |
epl2_command = '%s\n' % [ %w( | |
N | |
A50,200,0,5,1,1,N,"EXAMPLE1" | |
P1 | |
).join("\n") ] | |
# Use bulk transfer to send the EPL2 command to the printer. | |
handle.bulk_transfer(endpoint: 6, dataOut: epl2_command) | |
# Cleanup. | |
handle.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment