📄 porttest.java
字号:
package com.jzl.at.test;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
public class PortTest {
Enumeration portLists = null;
SerialPort sPort = null;
public PortTest(){}
public void getPort(String owner,int time) throws PortInUseException{
portLists = CommPortIdentifier.getPortIdentifiers();
while(portLists.hasMoreElements()){
CommPortIdentifier tempPort =(CommPortIdentifier) portLists.nextElement();
if(tempPort.getPortType() == CommPortIdentifier.PORT_SERIAL){
sPort =(SerialPort)tempPort.open(owner, time);
System.out.println("Serial port name is:"+sPort.getName());
}
}
}
/**
* @param args
* @throws PortInUseException
*/
public static void main(String[] args) throws PortInUseException {
// TODO Auto-generated method stub
PortTest test = new PortTest();
test.getPort(PortTest.class.getName(), 5000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -