我想通过 USB 电缆使用 Zebra zd420 打印机进行打印,但数据未发送到打印机。我试过 Zebra GK420,效果很好。
fun print(connection: UsbDeviceConnection?, usbInterface: UsbInterface?, zplData: String, totalPrint:Int=1): Boolean {
var testBytes = zplData.toByteArray()
if (usbInterface == null) {
Utility.showToast(resources.getString(R.string.local_printer_interface_error), Constants.ERROR)
}
else if (connection == null) {
Utility.showToast(resources.getString(R.string.local_printer_connection_error), Constants.ERROR)
}
else if (forceCLaim == null) {
Utility.showToast(resources.getString(R.string.local_printer_force_claim_error), Constants.ERROR)
}
// else if (!printerReady) {
// Utility.showToast(resources.getString(R.string.local_printer_not_connected),Constants.ERROR)
// }
else {
connection.claimInterface(usbInterface, forceCLaim)
Utility.showToast("I'm reached")
val thread = Thread(Runnable {
for(i in 0 until totalPrint) {
val cut_paper = byteArrayOf(0x1D, 0x56, 0x41, 0x10)
// val cut_paper = byteArrayOf(58, 0xAC.toByte(), 0x82.toByte(), 0x20)
connection.bulkTransfer(mEndPoint, testBytes, testBytes!!.size, 0)
connection.bulkTransfer(mEndPoint, cut_paper, cut_paper.size, 0)
}
})
thread.run()
return true
}
return false
}
这是我的 zpl 数据
var data = "^XA ^CFA,20^A0N,30,35^FOR ^FO${80 + x},${30 + y}^FDDispatch Date:${Utility.getTime("dd-MM-yyyy")} ^FS ^FX Third section with barcode. ^BY2,1,120 ^FO${100 + x},${80 + y}^BC^FD0123456789^FS ^A0N,30,35^FO${80 + x},${270 + y}^FDPicked By:${ Utility.printInitials(Utility.getPreference("name")) } ^FS ^A0N,30,35^FO${80 + x},${300 + y}^FDPicked At:${Utility.getTime("dd-MM-yyyy HH:mm:ss")}^FS^XZ"
对于 Zebra 打印机,恐怕批量传输不是正确的选择。您需要使用 ZPL 发送命令才能向打印机发送有效命令。