java - JavaPOS如何让两台电脑接连连接到同一台打印机?
问题描述
目前我有 2 台计算机尝试连接并打印到单个网络打印机。问题在于ptr.open("POSPrinter_LAN_1");
无法首先连接的打印机总是会抛出此异常的方法:
jpos.JposException: Can not open the communication port.
at com.sewoo.jpos.POSPrinterService.open(POSPrinterService.java:729)
at jpos.BaseJposControl.open(BaseJposControl.java:371)
at ReceiptTestSingleNetworkPrinter.main(ReceiptTestSingleNetworkPrinter.java:31)
因此有没有办法让打印机等待连接再次被释放/释放?
我目前的代码:
import jpos.*;
import jpos.JposConst;
import jpos.JposException;
import jpos.POSPrinter;
import jpos.POSPrinterConst;
import jpos.util.JposPropertiesConst;
public class ReceiptTestSingleNetworkPrinter
{
public static void main(String[] args)
{
// constants defined for convience sake (could be inlined)
String ESC = ((char) 0x1b) + "";
String LF = ((char) 0x0a) + "";
String SPACES = " ";
POSPrinter ptr = new POSPrinter();
try
{
// To test Printer
ptr.open("POSPrinter_LAN_1");
ptr.claim(100000);
ptr.setDeviceEnabled(true);
// begining a transaction
// This transaction mode causes all output to be buffered
ptr.transactionPrint(POSPrinterConst.PTR_S_RECEIPT, POSPrinterConst.PTR_TP_TRANSACTION);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|cA" + ESC + "|4C" + ESC + "|bC" + "Receipt" + LF + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + ESC + "|bC" + "TEL (123)-456-7890" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|cA" + ESC + "|bC" + "Thank you for coming to our shop!" + LF + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Chicken $10.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Hamburger $20.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Pizza $30.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Lemons $40.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Drink $50.00" + LF + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Excluded tax $150.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|uC" + "Tax(5%) $7.50" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|bC" + ESC + "|2C" + "Total $157.50" + LF + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|bC" + "Payment $200.00" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|bC" + "Change $42.50" + LF);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|100fP");
// terminate the transaction causing all of the above buffered data to be sent to the printer
ptr.transactionPrint(POSPrinterConst.PTR_S_RECEIPT, POSPrinterConst.PTR_TP_NORMAL);
// release current printer
ptr.setDeviceEnabled(false);
ptr.release();
ptr.close();
}
catch(JposException e)
{
e.printStackTrace();
}
finally
{
System.exit(0);
}
}
}
解决方案
作为一种可能性,即使由于某种原因关闭执行后,第一台计算机和打印机之间的连接也没有断开。
但是,根据 JavaPOS 的规范,它可以从任意数量的计算机上打开。只有Claim成功的计算机才能在Claim之后正常调用,直到Release。
因此,您的打印机的硬件和 JavaPOS 控制可能不符合 JavaPOS 规范。
通过网络连接的设备的共享/独占控制很困难,因此可能无法实现。
向您的打印机供应商询问该问题以及如何使用它。
推荐阅读
- dart - Flutter 在获取数据时抛出错误。我该如何解决?
- javascript - 为什么在我打开检查工具之前我的图像不会加载到 carousal 中?
- spring - http 404 错误源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
- recursion - makefile递归所有子系统总是最新的
- python - 将大型 python 文件拆分为较小的文件
- c# - EF Core 修改实体状态的行为如何?
- ios - Alamofire POST 请求,正文中有一个简单的字符串作为参数,标头仍然显示缺少参数值
- cesium - Cesium:我可以用图像覆盖地形,然后在其上添加slopeRamp 材质吗?
- angular - 材料表格的材料表(使用材料滑块)不起作用
- angular - 角度材料步进器在每次点击时动态添加新的步进项目