javarxtx

Java/RXTX issue Windows XP


I'm currently testing a Java/MySQL POS system I've written for a small bar, and am having problems with the cash draw.

The cash drawer has an RJ11 plug connected via a USB->Serial box, and writing any data to the device triggers the draw to open.

I'm having problems with RXTX, and wasn't sure if it was my code, the library or drivers for the device?

Ideally, I'd like the connection to be created when a user logs in to the system, and closed when they log out, but for the moment, the code just opens the connection, writes the data and closes when a sale is rung up (there is a 1-2 second delay between hitting the save button and the drawer opening, which is frustrating).

When the app first starts, the drawer works fine for a few sales (haven't identified a pattern), but then stops working. It shows a range of exceptions occurring, mixing either NoSuchPort, PortInUse or just a plain AccessDenied message. Usually, restarting the app and disconnecting/reconnecting the USB will get it working again for a few more sales.

I can connect to the device using HyperTerminal, and it works consistently, without any issue.

Java code:

public static void openTill() {
    try {
        portId = (CommPortIdentifier) CommPortIdentifier.getPortIdentifier("COM3");
        serialPort = (SerialPort) portId.open("DRAWER_PORT", 2000);

        outputStream = serialPort.getOutputStream();

        serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        serialPort.setRTS(false);
        serialPort.setInputBufferSize(8192);
        serialPort.setOutputBufferSize(8192);
        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT);

        outputStream.write("k".getBytes());
        outputStream.close();
        outputStream = null;

        serialPort.close();
        serialPort = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

I've tried a few different settings, trying to mimic as close as I can the settings HyperTerminal uses (by checking PortMon), but still not working.

Any suggestions would be greatly appreciated!

Thanks, Ryan.


Solution

  • Cannot find anything wrong with the code, but I can suggest some starting points for debugging:

    Edit:

    If you discover that this is actually a rxtx bug, but for some reason don't want to switch to another javax.comm implementation (I've seen this happen :-) here are some additional hints that may be useful (I would try the above suggestions first anyway):