2

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.

0009laH
  • 1,960
  • 13
  • 27
itsreeyan
  • 174
  • 1
  • 9

2 Answers2

1

You should try jSSC: http://code.google.com/p/java-simple-serial-connector/ (it's free unlike SerialIO).

I had a lot of issues (this was one of those) with rxtx serial and usb-serial converters too, so I just migrated from rxtx to this one, it works very well! Integrated DLLs into .jar for Windows, Linux, Mac and Solaris with system auto-detection.

The latest official version doesn't support timeouts, but I contacted the developer and he sent me a jar version with timeouts implemented really quick!

The migration was really simple, now it's working much better (:

Community
  • 1
  • 1
HericDenis
  • 1,364
  • 12
  • 28
  • Thanks for this! I'll look into it. Might be useful still. However, the USB setup on this particular PC was causing the issues. I moved the cash trigger onto a USB thermal printer and it still had the same issues, so then made the printer an ethernet one and it's been working fine for the last 18 months or so, teach me to use cheap made-in-china PCs for this stuff! – itsreeyan Oct 08 '12 at 11:59
1

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
  • 21,501
  • 10
  • 63
  • 107
  • 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. – itsreeyan Sep 23 '10 at 15:26
  • 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 Sep 23 '10 at 15:38
  • By the way, if you found the answer useful, consider accepting (green tick mark in the left hand side) / upvoting :-) – Grodriguez Sep 23 '10 at 15:39
  • implemented javax.comm today, will test over next few days! =) thanks! – itsreeyan Sep 24 '10 at 14:22