我在 Windows 7 64 位。我写了一个小应用程序,用于RXTX
通过串口进行通信。我在rxtxSerial.dll
64 位 Windows 上使用它,它在 Eclispe 和 NetBeans 上都运行良好。
在项目的根目录下,我放置RXTXComm.jar
并rxtxSerial.dll.
当我想部署应用程序时出现问题。我在 Eclipse 上使用了导出功能,或者我从 NetBeans 访问了 bin/ 文件夹。我再次放置在文件夹RXTXComm.jar
的rxtxSerial.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";
}
}
}
感谢您的帮助。