Zebra printer won't print ZPL format
Asked Answered
A

2

9

I am following the Zebra Android Link_OS SDK sample code for printing a test label on a ZQ510 over Bluetooth, but it won't print in ZPL format.

Here is the code I'm running to print the label:

private void sendZplOverBluetooth(final String theBtMacAddress) {

     new Thread(new Runnable() {
         public void run() {
             try {
                 // Instantiate connection for given Bluetooth® MAC Address.
                 Connection thePrinterConn = new BluetoothConnection(theBtMacAddress);

                 // Initialize
                 Looper.prepare();

                 // Open the connection - physical connection is established here.
                 thePrinterConn.open();

                 // This example prints "This is a ZPL test." near the top of the label.
                 String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";

                 // Send the data to printer as a byte array.
                 thePrinterConn.write(zplData.getBytes());

                 // Make sure the data got to the printer before closing the connection
                 Thread.sleep(500);

                 // Close the connection to release resources.
                 thePrinterConn.close();

                 Looper.myLooper().quit();
             } catch (Exception e) {
                 // Handle communications error here.
                 e.printStackTrace();
             }
         }
     }).start();
 }

And here is the result of the print. (I ran it twice, that's why there are two test prints).

Example print Then I read about how it might be in a different mode because for some reason Zebra can't detect their own proprietary language. So I tried to get the settings and see through the Android app. Again using the given Link-OS SDK example code:

private static void displaySettings(Connection c) throws ConnectionException, ZebraPrinterLanguageUnknownException, SettingsException, ZebraIllegalArgumentException {
     ZebraPrinter genericPrinter = ZebraPrinterFactory.getInstance(c);
     ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter(genericPrinter);

     if (linkOsPrinter != null) {

         System.out.println("Available Settings for myDevice");
         Set<String> availableSettings = linkOsPrinter.getAvailableSettings();
         for (String setting : availableSettings) {
             System.out.println(setting + ": Range = (" + linkOsPrinter.getSettingRange(setting) + ")");
         }

         System.out.println("\nCurrent Setting Values for myDevice");
         Map<String, String> allSettingValues = linkOsPrinter.getAllSettingValues();
         for (String settingName : allSettingValues.keySet()) {
             System.out.println(settingName + ":" + allSettingValues.get(settingName));
         }

         String darknessSettingId = "print.tone";
         String newDarknessValue = "10.0";
         if (availableSettings.contains(darknessSettingId) &&
                 linkOsPrinter.isSettingValid(darknessSettingId, newDarknessValue) &&
                 linkOsPrinter.isSettingReadOnly(darknessSettingId) == false) {
             linkOsPrinter.setSetting(darknessSettingId, newDarknessValue);
         }

         System.out.println("\nNew " + darknessSettingId + " Value = " + linkOsPrinter.getSettingValue(darknessSettingId));
     }
 }

This time, I get a SettingsException with the description of Operation cannot be performed on raw channel with a printer set to line print mode

How am I able to print ZPL text using a Mac and developing Android correctly? I read about using some Zebra Utility app for changing the mode, but it's only available for Windows, and their Android app doesn't work.

Regardless, if someone was to use the app with a printer in the incorrect mode, they would have to go through all this unnecessary setup that wouldn't be intuitive for just anybody.

Thanks for the help and appreciate any feedback.

Agon answered 15/6, 2017 at 19:33 Comment(1)
I recommend adding a smaller image of the issue you had, I think this one is way too big for the post.Equi
E
10

You can programmatically set the print mode to ZPL, it's currently in line-mode.

To do so:

BluetoothConnection printerIns= new BluetoothConnection(theBtMacAddress);
ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(printerIns);
//Set printer to ZPL mode
zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n");
//Feed and calibrate to the media
zPrinterIns.sendCommand("~jc^xa^jus^xz");

In your example code, You are establishing a Bluetooth connection and attempting to send raw data, make use of the ZebraPrinterand BluetoothConnection classes provided by Zebra instead from the com.zebra.sdk.printer namespace.

I corrected your code, it should work now.

 new Thread(new Runnable() {
         public void run() {
             try {
                 // Instantiate connection for given Bluetooth&reg; MAC Address.
                 BluetoothConnection thePrinterConn = new BluetoothConnection(theBtMacAddress);

                 // Initialize
                 Looper.prepare();

                 // Open the connection - physical connection is established here.
                 ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(thePrinterConn);
                 zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n");
                 zPrinterIns.sendCommand("~jc^xa^jus^xz");
                 Thread.sleep(500);

                 // Send the data to printer as a byte array.
                 zPrinterIns.sendCommand("^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ");

                 // Make sure the data got to the printer before closing the connection
                 Thread.sleep(500);

                 // Close the connection to release resources.
                 thePrinterConn.close();

                 Looper.myLooper().quit();
             } catch (Exception e) {
                 // Handle communications error here.
                 e.printStackTrace();
             }
         }
}).start();
Equi answered 15/6, 2017 at 19:56 Comment(5)
Perfect solution. Thanks for the help!Agon
Anytime! I went through the same problem as you. Have a good oneEqui
@Equi i have a .txt file which contains ZPL code like: ^XA ^FO50,50^A0,60,60^FDTest^FS ^XZ But i am unable to print it's output on Zebra ZTC gc420t (EPL) printer. What should i do?Astromancy
@Astromancy You should probably create a new question for that, but didn't you try the solution I posted. it should work for that text file as well, Just read-in the text file into a String then pass that string to the zPrinterIns.sendCommand as stated in my answer.Equi
@Astromancy You need to install the Zebra Setup Utilities not sure if they run on Mac, maybe bootup a virtual machine with windows. Then load up and execute the following command prior to your zpl code ! U1 setvar "device.languages" "zpl"Equi
D
2

If you don't want to perform this step programmatically like in the Dayan answer and you have acces to a Windows machine (or emulating one), install the Zebra Setup Utilities. Then following the instructions here https://km.zebra.com/kb/index?page=content&id=SO7296 to switch the print mode to ZPL with the command

! U1 setvar "device.languages" "zpl"
Durst answered 15/6, 2017 at 19:51 Comment(1)
I figured this is the most likely solution I'll have to accept. Thanks for the answer.Agon

© 2022 - 2024 — McMap. All rights reserved.