Click here to Skip to main content
Click here to Skip to main content

How to send a command to a Zebra QLn220 Printer from a Compact Framework app

, 1 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Sending commands in a Compact Framework app via C# to a Zebra QLn220 printer

Some Say Zebras Are Just Mules with Stripes

When interfacing with a Zebra printer, you might need to send it commands to change some of its settings programmatically. For example, for some reason, there is a setting that will shut the printer off when a command is sent to it (as bizarre and nonsensical as it seems to me for this to be the default setting, so it is, and maybe there's a good reason for it, though for the life of me I can't grok it).

This behavior (the printer shutting off after sending a command) did not meet our tough standards, so I added the necessary call to change that setting from "dtr_power_off = on" to "dtr_power_off = off"

Also, the printer can advance after printing a label to the precisely right position for the next label to print, but it needs to know which type of label you're using: a "bar" label (which has a black strip on the back, and which the printer presumably "reads" to know where to stop) or a "gap" label (where the gap between labels is "looked for" by the printer).

If you have a mismatch between this setting ("media.sense_mode") and the labels in the printer, the printer gets confused and spews out reams of labels, looking for the place to stop. It's as if it's wildly looking first left, then right, then up, then down, yelling "Which way did they go? Which way did they go?"

So, this is an important setting, too. To allow your user to select which type of labels they're going to use (and they may switch from one to the other at times), you can allow them to change that setting by, say, selecting a radio button. They think they're clever when they do this, but really it's your code behind that radio-button-checking action that is really the clever one here. That is shown below, too.

The code below assumes it is being called from a form on which there is a radiobutton named "radbtnBar" and one named "radbtnGap"

Note: The SerialPort class used in the code is an OpenNETCF.IO component.

So, now, to the code snippet:

const string quote = "\"";
string keepPrinterOn = string.Format("! U1 setvar {0}power.dtr_power_off{0} {0}off{0}\r\n", quote);
string advanceToBlackBar = string.Format("! U1 setvar {0}media.sense_mode{0} {0}bar{0}\r\n", quote);
string advanceToGap =      string.Format("! U1 setvar {0}media.sense_mode{0} {0}gap{0}\r\n", quote);


if (radbtnBar.Checked)
else if (radbtnGap.Checked)

public static bool SendCommandToPrinter(string cmd)
	bool success; // is the default "false" at this point
	using (SerialPort serialPort = new SerialPort())
		serialPort.BaudRate = 19200;
		serialPort.Handshake = Handshake.XOnXOff;
	success = true;
	return success;

Variety is the Spice of Coding (YMMV)

An alternative way of setting the values is like so (mark == bar, web == gap):

string advanceToBlackBarAlternative = string.Format("! U1 setvar {0}ezpl.media_type{0} {0}mark{0}\r\n", quote);
string advanceToGapAlternative = string.Format("! U1 setvar {0}ezpl.media_type{0} {0}web{0}\r\n", quote);

Yet another alternative (If you have newer firmware -- see this post for details) -- is to join the goatee-and-tattoo set and send JSON to the printer:

string advanceToBlackBarJSON = string.Format("{{}} {{{0}media.sense_mode{0} {0}bar{0}}}\r\n", quote);
string advanceToGapJSON =  string.Format("{{}} {{{0}media.sense_mode{0} {0}gap{0}}}\r\n", quote);

Show some Appreciation, Already!

If you like this tip and it made your nanosecond, send me the pillow that you dream on (de-mite it, first, though, pretty please). Otherwise, if that's inconvenient, send me the Pillars of Hercules, wrapped in pink ribbons and "shipped" via Amazon Drone.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of the Windows Store App "Photrax", which can be downloaded as a trial (7 days) from
Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.
I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction:
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionTwo-tier ERP in global projects: SAP + Dynamics NAV PinmemberZdzichu Fjutek8-Sep-14 5:37 
AnswerSerial port PinmemberZdzichu Fjutek11-Aug-14 3:13 
AnswerSerial port PinmemberZdzichu Fjutek11-Aug-14 3:11 
GeneralMy vote of 5 PinprofessionalVolynsky Alex7-Aug-14 11:01 
Questionostatni PinmemberZdzichu Fjutek1-Aug-14 1:32 
Questionok przedostatni PinmemberZdzichu Fjutek1-Aug-14 1:32 
Questioni jeszcze raz PinmemberZdzichu Fjutek1-Aug-14 1:31 
Questionsprawdzam 2 PinmemberZdzichu Fjutek1-Aug-14 1:31 
Questionsprawdzam mozliwosci PinmemberZdzichu Fjutek1-Aug-14 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 1 Aug 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid