如何使用JavaPOS使两台计算机相继连接到同一台打印机?
目前,我有两台计算机试图连接并打印到一台网络打印机。问题在于方法如何使用JavaPOS使两台计算机相继连接到同一台打印机?,java,printing,javapos,Java,Printing,Javapos,目前,我有两台计算机试图连接并打印到一台网络打印机。问题在于方法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(BaseJposCont
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的规范,它可以从任意数量的计算机上打开。
只有声明成功的计算机才能在声明后正常调用,直到释放
因此,打印机的硬件和JavaPOS控制可能不符合JavaPOS规范
通过网络连接的设备难以共享/独占控制,因此可能无法实现
向您的打印机供应商询问这个问题以及如何使用它。我不知道该库是如何工作的,但是如果将打印连接放在一个循环中,并尝试捕获JposException
并使用continue
,如果捕获到它,则使用break
。每次捕获jposeException
时,最终放置一个线程.sleep(ms)和一个couter以防止无限循环。您好,感谢您的输入!我完全按照你说的做了。但是打印有点不稳定,特别是当3台机器试图同时连接到同一台网络打印机时。有某种碰撞。你们都遇到过这样的问题吗?对不起,我从来没有为打印机或类似的东西开发过应用程序。我只是想帮你找到一个普遍的解决办法。谢谢你,劳尔,我发现了这个问题。这与我的连接方法有关。我现在正在努力解决它。