我使用了javax.comm库,但它没有用,因为我在x64系统上,所以我尝试了rxtx。我将rxtxSerial
和rxtxParallel
dll文件放在C:\ Program Files \ Java \ jre1.8.0_51 \ bin中,将RXTXcomm jar放在C:\ Program Files \ Java \ jre1.8.0_51中\ lib \ ext以及Eclipse的构建路径。这是我的代码,它完全没有显示消息,因为他可能找不到任何端口。一些帮助将非常感激,因为我在一段时间内对此问题感到困惑。我不知道这是否与此有关,但在设备管理中我没有任何Ports标题或与之相关的任何内容。我使用的是Windows 8.1 64位。感谢。
import java.util.Enumeration;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
public class JavaPOS {
public static void main(String[] args){
Enumeration port_list = CommPortIdentifier.getPortIdentifiers ();
while (port_list.hasMoreElements ()) { // Get the list of ports
CommPortIdentifier port_id =
(CommPortIdentifier) port_list.nextElement ();
// Find each ports type and name
if (port_id.getPortType () == CommPortIdentifier.PORT_SERIAL)
{
System.out.println ("Serial port: " + port_id.getName ());
}
else if (port_id.getPortType () == CommPortIdentifier.PORT_PARALLEL)
{
System.out.println ("Parallel port: " + port_id.getName ());
} else
System.out.println ("Other port: " + port_id.getName ());
// Attempt to open it
try {
CommPort port = port_id.open ("PortListOpen",20);
System.out.println (" Opened successfully");
port.close ();
}
catch (PortInUseException pe)
{
System.out.println (" Open failed");
String owner_name = port_id.getCurrentOwner ();
if (owner_name == null)
System.out.println (" Port Owned by unidentified app");
else
// The owner name not returned correctly unless it is
// a Java program.
System.out.println (" " + owner_name);
}
}
}
}