【问题标题】:Zebra printer won't print ZPL formatZebra 打印机无法打印 ZPL 格式
【发布时间】:2023-03-19 04:51:01
【问题描述】:

我正在按照 Zebra Android Link_OS SDK 示例代码通过蓝牙在 ZQ510 上打印测试标签,但它不会以 ZPL 格式打印。

这是我正在运行以打印标签的代码:

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();
 }

这是打印的结果。 (我运行了两次,这就是为什么有两个测试打印)。

然后我读到它可能处于不同的模式,因为由于某种原因 Zebra 无法检测到他们自己的专有语言。所以我尝试获取设置并查看Android应用程序。再次使用给定的 Link-OS SDK 示例代码:

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));
     }
 }

这一次,我得到了一个SettingsException,描述为Operation cannot be performed on raw channel with a printer set to line print mode

如何使用 Mac 打印 ZPL 文本并正确开发 Android?我阅读了有关使用 Zebra Utility 应用程序更改模式的信息,但它仅适用于 Windows,并且他们的 Android 应用程序不起作用。

无论如何,如果有人在不正确的模式下通过打印机使用该应用程序,他们将不得不进行所有这些不必要的设置,这对任何人来说都不是直观的。

感谢您的帮助并感谢任何反馈。

【问题讨论】:

  • 我建议添加您遇到的问题的较小图像,我认为这个对于帖子来说太大了。

标签: android zebra-printers zpl


【解决方案1】:

您可以通过编程方式将打印模式设置为 ZPL,它当前位于 line-mode

这样做:

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");

在您的示例代码中,您正在建立蓝牙连接并尝试发送原始数据,请使用 Zebra 提供的 ZebraPrinterBluetoothConnection 类,而不是来自 com.zebra.sdk.printer 命名空间。

我更正了您的代码,现在应该可以使用了。

 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();

【讨论】:

  • 完美解决方案。感谢您的帮助!
  • 随时!我遇到了和你一样的问题。祝你好运
  • @Dayan 我有一个 .txt 文件,其中包含 ZPL 代码,例如:^XA ^FO50,50^A0,60,60^FDTest^FS ^XZ 但我无法在 Zebra 上打印它的输出ZTC gc420t (EPL) 打印机。我该怎么办?
  • @ArgaPK 您可能应该为此创建一个新问题,但您没有尝试我发布的解决方案。它也应该适用于该文本文件,只需将文本文件读入String,然后将该字符串传递给zPrinterIns.sendCommand,如我的回答中所述。
  • @ArgaPK 您需要安装Zebra Setup Utilities 不确定它们是否在 Mac 上运行,也许用 Windows 启动虚拟机。然后在你的zpl代码之前加载并执行以下命令! U1 setvar "device.languages" "zpl"
【解决方案2】:

如果您不想像 Dayan 答案中那样以编程方式执行此步骤,并且您可以访问 Windows 计算机(或模拟计算机),请安装 Zebra Setup Utilities。然后按照这里https://km.zebra.com/kb/index?page=content&id=SO7296的说明使用命令将打印模式切换到ZPL

! U1 setvar "device.languages" "zpl"

【讨论】:

  • 我认为这是我必须接受的最有可能的解决方案。感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多