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);
PrintUtils.SendCommandToPrinter(keepPrinterOn);
if (radbtnBar.Checked)
{
SendCommandToPrinter(advanceToBlackBar);
}
else if (radbtnGap.Checked)
{
SendCommandToPrinter(advanceToGap);
}
public static bool SendCommandToPrinter(string cmd)
{
bool success;
using (SerialPort serialPort = new SerialPort())
{
serialPort.BaudRate = 19200;
serialPort.Handshake = Handshake.XOnXOff;
serialPort.Open();
serialPort.Write(cmd);
serialPort.Close();
}
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.