0

我在 Windows 7 64 位。我写了一个小应用程序,用于RXTX通过串口进行通信。我在rxtxSerial.dll64 位 Windows 上使用它,它在 Eclispe 和 NetBeans 上都运行良好。

在项目的根目录下,我放置RXTXComm.jarrxtxSerial.dll.

当我想部署应用程序时出现问题。我在 Eclipse 上使用了导出功能,或者我从 NetBeans 访问了 bin/ 文件夹。我再次放置在文件夹RXTXComm.jarrxtxSerial.dll根目录,但是当我执行 Application.jar 时,RXTX似乎没有工作。扫描似乎卡住了,而它不应该持续超过一秒钟。

[抱歉,我“需要至少 10 个声望才能发布图片。”]

我尝试了在互联网上找到的所有建议:

  • 在 JRE 文件夹中安装 dll 和 RXTXComm.jar
  • 将dll放在Windows32文件夹中
  • 尝试了从 Eclipse 导出的所有不同导出选项

我肯定错过了什么。是否有人已经成功部署RXTX了 Windows 32/64 位和 MAC?你能描述一下你做了什么以及这样做有什么必要吗?

请在下面找到扫描端口时执行的代码:

private void scanButtonAction()
{
    if(scanState == ST_FREE)
    {
        scanState = ST_SCANNING;
        redrawComponents();
        scan = new Thread(new ScanPorts());
        scan.start();
    }
}

// Thread run to scan the ports
private class ScanPorts implements Runnable {
    public void run()
    {
        try
        {
            UARTConnection connection = new UARTConnection();

            // listPort() is a long blocking call
            String[][] list = connection.listPorts();

            // Display the ports in the ComboBox
            comboBoxModel.removeAllElements();

            if(list.length == 0)    comboBoxModel.addElement( new Item(-1, "No port scanned", "" ) );
            else
            {
                for(int i = 0; i < list.length; i++)
                {
                    // Id, Description (user's display), PortName (for serial connection)
                    comboBoxModel.addElement( new Item(i, list[i][1], list[i][0]) );
                }

                // To select the first item of the list. Necessary with custom Rendered
                portNumberBox.setSelectedIndex(0);
            }

            scanState = ST_FREE;
            redrawComponents();

            // The connect button is enabled only after a first scan
            connectButton.setEnabled(true);
        }
        catch(Exception ex)
        {
            scanState = ST_FREE;
            redrawComponents();
        }
    }
}

public class UARTConnection {

public UARTConnection()
{

}

public String[][] listPorts() throws Exception
{
    Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
    Enumeration<CommPortIdentifier> tmpPortEnum = portEnum;

    ArrayList<String[]> list = new ArrayList<String[]>();

    int i = 0;
    while ( portEnum.hasMoreElements() ) 
    {
        String port[] = new String[2];
        CommPortIdentifier portIdentifier = portEnum.nextElement();
        System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()));

        port[0] = portIdentifier.getName();
        port[1] = portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType());

        list.add(port);

        i++;
    }

    String listOfPort[][] = new String[list.size()][2];
    for(i = 0; i < list.size(); i++)
    {
        String[] port = list.get(i);
        listOfPort[i][0] = port[0];
        listOfPort[i][1] = port[1];
    }

    return listOfPort;
}

private String getPortTypeName ( int portType )
{
    switch ( portType )
    {
        case CommPortIdentifier.PORT_I2C:
            return "I2C";
        case CommPortIdentifier.PORT_PARALLEL:
            return "Parallel";
        case CommPortIdentifier.PORT_RAW:
            return "Raw";
        case CommPortIdentifier.PORT_RS485:
            return "RS485";
        case CommPortIdentifier.PORT_SERIAL:
            return "Serial";
        default:
            return "unknown type";
    }
}   
}

感谢您的帮助。

4

1 回答 1

1

我找到了解决方案,对于那些可能会有所帮助的人。

从 Eclipse 或 NetBean 运行应用程序时,应用程序以 64 位运行。因此,我使用了 64bit rxtxSerial.dll。它工作正常。

但我意识到,在 IDE 之外运行已编译的 .jar 文件时,正在javaw.exe *32为应用程序显示 Windows 进程列表。由于某种原因我忽略了,编译的 .jar 是 32 位的。因此,所需的驱动程序适用于 Windows 32 位,而不是 64 位。从现在开始它运作良好。

注意:为了能够在我的 MAC 上工作,我必须在 Java 1.6(不是 1.7)中编译应用程序,当然还要提供 MAC 驱动程序。

于 2013-07-03T07:23:04.687 回答