Click here to Skip to main content
12,547,922 members (48,137 online)
Click here to Skip to main content
Add your own
alternative version



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
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

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, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006:

You may also be interested in...


Comments and Discussions

QuestionTwo-tier ERP in global projects: SAP + Dynamics NAV Pin
Zdzichu Fjutek8-Sep-14 4:37
memberZdzichu Fjutek8-Sep-14 4:37 
AnswerSerial port Pin
Zdzichu Fjutek11-Aug-14 2:13
memberZdzichu Fjutek11-Aug-14 2:13 
AnswerSerial port Pin
Zdzichu Fjutek11-Aug-14 2:11
memberZdzichu Fjutek11-Aug-14 2:11 
GeneralMy vote of 5 Pin
Volynsky Alex7-Aug-14 10:01
professionalVolynsky Alex7-Aug-14 10:01 
Questionostatni Pin
Zdzichu Fjutek1-Aug-14 0:32
memberZdzichu Fjutek1-Aug-14 0:32 
Questionok przedostatni Pin
Zdzichu Fjutek1-Aug-14 0:32
memberZdzichu Fjutek1-Aug-14 0:32 
Questioni jeszcze raz Pin
Zdzichu Fjutek1-Aug-14 0:31
memberZdzichu Fjutek1-Aug-14 0:31 
Questionsprawdzam 2 Pin
Zdzichu Fjutek1-Aug-14 0:31
memberZdzichu Fjutek1-Aug-14 0:31 
Questionsprawdzam mozliwosci Pin
Zdzichu Fjutek1-Aug-14 0:30
memberZdzichu Fjutek1-Aug-14 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.161021.1 | Last Updated 1 Aug 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid