0

currently I have 2 computers trying to connect and print to a single network printer. The Problems lies with the method ptr.open("POSPrinter_LAN_1"); in that the printer that couldn't connect first will always throw this exception:

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)

Therefore is there a way to somehow make the printer wait for the connection to be released/free again?

My current Code:

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);
    }
    }
}
Vivek Pratap Singh
  • 9,326
  • 5
  • 21
  • 34
Chiggiddi
  • 542
  • 1
  • 8
  • 26
  • 1
    I don't know how that lib work, but what if put connection to print in a loop and try to catch `JposException` and use `continue` if it was catched and `break` otherwise. Eventualy put a Thread.sleep(ms) every time you catch `JposException` and a couter to prevent infinite loop. – KunLun Aug 07 '18 at 08:16
  • Hi, thank you for your input! I did exactly as you said. But the printing is kinda instable especially when 3 machines are trying to connect to the same network printer at the same time. There is some sort of collision. Have you every come across this sort of issue? – Chiggiddi Aug 07 '18 at 19:06
  • Sorry, but I never developed apps for printers or things like this. I just tried to help with a general solution. – KunLun Aug 08 '18 at 07:48
  • thank you raul, I have found the issue. It has something to do with my connect method. I am trying to resolve it now. – Chiggiddi Aug 09 '18 at 09:12

1 Answers1

0

As a possibility, the connection between the first computer and the printer was not disconnected even after close execution for some reason.

However, according to the specification of JavaPOS, it can be opened from any number of computers. Only the computer that Claim succeeded can call normally after Claim until Release.

So, the hardware and JavaPOS control of your printer may not be compliant with JavaPOS specification.

Sharing/exclusive control of devices connected via the network is difficult, so it may not be implemented.

Ask your printer vendor about the problem and how to use it.

kunif
  • 4,060
  • 2
  • 10
  • 30