tags:

views:

81

answers:

1

Hi Guys,

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.

A: 

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

  • Try the same code with Sun's (errr.. Oracle's) javax.comm implementation. The Windows version is no longer available for download from their site, but it can still be found in other places. Even if you don't want to use this implementation in your final setup, it might help you find the problem. There are also other alternatives such as SerialIO.

  • Use com0com to install a virtual com port. Enable logging (see last question in the README.txt file). Compare the logs when you use your code with the logs you get when using HyperTerminal, and look for any differences.

  • Try a different serial -> USB converter. In my experience, many of these don't implement RS232 properly, or have plenty of bugs.

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):

  • Are the calls to setInputBufferSize, setOutputBufferSize required? Try removing them. Does the device actually use XON/XOFF flow control? If not, try setting flow control to none. Does the device require RTS disabled? If not, remove this line as well. Also, try to set the serial port params before opening the output stream. Of course, none of this should make any difference, but you might be triggering some rxtx bug.

  • Is the problem related to opening and closing the port in sequence several times? You could try to keep the port always open. On each sale, just do:

    outputStream.write("k".getBytes());
    outputStream.flush();
    

    And see if the problem still reproduces.

Grodriguez
Thanks Grodriguez!
Ryan
Will try those things... As for the multiple opens/closes, the code originally opened the port at the beginning and closed when exiting, but I thought the errors may have been due to a timeout kind of issue, so moved them to single open/closes per transaction.Settings used are me trying to get close as I can to the settings HyperTerminal uses.
Ryan
The logging in com0com can help you accurately compare what HyperTerminal and your application is doing, however as I said there seems to be nothing wrong with your code. I'd say rxtx bug and/or buggy USB converter.
Grodriguez
By the way, if you found the answer useful, consider accepting (green tick mark in the left hand side) / upvoting :-)
Grodriguez
implemented javax.comm today, will test over next few days! =) thanks!
Ryan