|
|
@@ -419,4 +419,56 @@ open class PlasticBagPrinterService( |
|
|
println("Han's Laser TCP 連接已關閉") |
|
|
println("Han's Laser TCP 連接已關閉") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun sendDataFlex6330Zpl(request: PrintRequest) { |
|
|
|
|
|
Socket().use { socket -> |
|
|
|
|
|
try { |
|
|
|
|
|
// Connect with timeout |
|
|
|
|
|
socket.connect(InetSocketAddress(request.printerIp, request.printerPort), 5000) |
|
|
|
|
|
socket.soTimeout = 5000 // read timeout if expecting response (optional) |
|
|
|
|
|
|
|
|
|
|
|
val out = socket.getOutputStream() |
|
|
|
|
|
|
|
|
|
|
|
// Build ZPL dynamically |
|
|
|
|
|
val zpl = buildString { |
|
|
|
|
|
append("^XA\n") |
|
|
|
|
|
append("^PW500\n") // Print width ~42mm @300dpi; adjust 400-630 based on head |
|
|
|
|
|
append("^LL280\n") // Label height ~23mm; tune for your pouch |
|
|
|
|
|
append("^PON\n") // Normal orientation |
|
|
|
|
|
append("^CI28\n") // UTF-8 / extended char set for Chinese |
|
|
|
|
|
|
|
|
|
|
|
// Chinese product name / description (top) |
|
|
|
|
|
append("^FO20,20^A@N,36,36,E:SIMSUN.FNT^FD${request.itemName}^FS\n") // Assumes font loaded |
|
|
|
|
|
|
|
|
|
|
|
// Item code |
|
|
|
|
|
append("^FO20,80^A0N,32,32^FD${request.itemCode}^FS\n") |
|
|
|
|
|
|
|
|
|
|
|
// Expiry date |
|
|
|
|
|
append("^FO20,120^A0N,28,28^FDEXP: ${request.expiryDate}^FS\n") |
|
|
|
|
|
|
|
|
|
|
|
// Lot / Batch No. |
|
|
|
|
|
append("^FO20,160^A0N,28,28^FDLOT: ${request.lotNo}^FS\n") |
|
|
|
|
|
|
|
|
|
|
|
// QR code encoding lotNo (or combine: item|lot|exp) |
|
|
|
|
|
// Position right side, mag 6 (~good size), model 2 |
|
|
|
|
|
val qrData = request.lotNo // or "${request.itemCode}|${request.lotNo}|${request.expiryDate}" |
|
|
|
|
|
append("^FO320,20^BQN,2,6^FDQA,$qrData^FS\n") // QA, prefix for alphanumeric mode |
|
|
|
|
|
|
|
|
|
|
|
append("^XZ\n") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Send as bytes (UTF-8 safe) |
|
|
|
|
|
out.write(zpl.toByteArray(Charsets.UTF_8)) |
|
|
|
|
|
out.flush() |
|
|
|
|
|
|
|
|
|
|
|
println("DataFlex 6330 ZPL: Print job sent to ${request.printerIp}:${request.printerPort}") |
|
|
|
|
|
// Optional: read response if printer echoes anything (rare in raw ZPL) |
|
|
|
|
|
// val response = socket.getInputStream().readBytes().decodeToString() |
|
|
|
|
|
// println("Response: $response") |
|
|
|
|
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
throw RuntimeException("DataFlex 6330 ZPL communication failed: ${e.message}", e) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |