porttest.java
来自「自己实现的基于串口通讯发送短消息的低层协议JAVA代码」· Java 代码 · 共 41 行
JAVA
41 行
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 + =
减小字号Ctrl + -
显示快捷键?