Java/RXTX 问题 Windows XP

发布于 2024-09-24 08:22:32 字数 1429 浏览 4 评论 0原文

我目前正在测试我为一家小酒吧编写的 Java/MySQL POS 系统,并且在现金提取方面遇到问题。

现金抽屉有一个通过 USB-> 串行盒连接的 RJ11 插头,向设备写入任何数据都会触发抽屉打开。

我在 RXTX 方面遇到问题,并且不确定这是否是我的代码、库或设备驱动程序的问题?

理想情况下,我希望在用户登录系统时创建连接,并在注销时关闭连接,但目前,代码仅打开连接,写入数据并在促销开始时关闭(点击保存按钮和打开抽屉之间有 1-2 秒的延迟,这令人沮丧)。

当应用程序首次启动时,抽屉在几次销售中工作正常(尚未识别模式),但随后停止工作。它显示了发生的一系列异常,混合了 NoSuchPort、PortInUse 或只是简单的 AccessDenied 消息。通常,重新启动应用程序并断开/重新连接 USB 将使其再次工作,从而获得更多销量。

我可以使用超级终端连接到设备,并且它可以稳定地工作,没有任何问题。

Java 代码:

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();
    }
}

我尝试了几种不同的设置,试图尽可能模仿超级终端使用的设置(通过检查 PortMon),但仍然无法正常工作。

任何建议将不胜感激!

谢谢, 瑞安.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2024-10-01 08:22:32

找不到代码中的任何问题,但我可以建议一些调试起点:

  • 尝试使用 Sun 的(错误.. Oracle 的)javax.comm 实现使用相同的代码。 Windows 版本不再可以从他们的网站下载,但仍然可以在其他地方找到。即使您不想在最终设置中使用此实现,它也可能会帮助您找到问题。还有其他替代方案,例如 SerialIO

  • 使用com0com安装虚拟com端口。启用日志记录(请参阅 README.txt 文件中的最后一个问题)。将使用代码时的日志与使用超级终端时获得的日志进行比较,并查找任何差异。

  • 尝试不同的系列 -> USB转换器。根据我的经验,其中许多都没有正确实现 RS232,或者有很多错误。

编辑:

如果您发现这实际上是一个 rxtx bug,但由于某种原因不想切换到另一个 javax.comm 实现(我已经看到这种情况发生了:-),这里有一些额外的可能有用的提示(无论如何我都会先尝试上述建议):

  • 是否需要调用 setInputBufferSizesetOutputBufferSize?尝试删除它们。设备实际上是否使用 XON/XOFF 流量控制?如果没有,请尝试将流量控制设置为无。该设备是否需要禁用 RTS?如果没有,也删除此行。另外,请尝试在打开输出流之前设置串行端口参数。当然,这些都不会产生任何影响,但您可能会触发一些 rxtx bug。

  • 问题是否与多次顺序打开和关闭端口有关?您可以尝试保持端口始终打开。每次销售时,只需执行以下操作:

    outputStream.write("k".getBytes());
    输出流.flush();
    

    看看问题是否仍然重现。

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.

清君侧 2024-10-01 08:22:32

您应该尝试 jSSC:http://code.google.com/p/ java-simple-serial-connector/(与 SerialIO 不同,它是免费的)。

我遇到了很多问题(这是其中之一)也带有 rxtx 串行和 USB 串行转换器,所以我刚刚从 rxtx 迁移到这个,它工作得很好!将 DLL 集成到适用于 Windows、Linux、Mac 和 Solaris 的 .jar 中,并具有系统自动检测功能。

最新的官方版本不支持超时,但是我联系了开发者,他给我发了一个jar版本,超时实现得非常快!

迁移非常简单,现在效果好多了(:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文