package tools.ChuanKou;
|
|
|
import gnu.io.*;
|
import tools.ShowMessage;
|
|
import java.util.ArrayList;
|
import java.util.Enumeration;
|
import java.util.List;
|
public class SerialPortManager {
|
static Enumeration<CommPortIdentifier> portList;
|
static CommPortIdentifier portId;
|
|
|
/**
|
* 列出本地连接的所有串口名称集合
|
* @param args
|
*/
|
@SuppressWarnings("unchecked")//告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
|
public static List<String> get_all_port(){
|
List<String> all_port=new ArrayList<String>();
|
portList = CommPortIdentifier.getPortIdentifiers();
|
while(portList.hasMoreElements()){
|
portId = (CommPortIdentifier)portList.nextElement();
|
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
|
all_port.add(portId.getName());
|
}
|
}
|
return all_port;
|
}
|
|
/**打开串口
|
* @param chuankou 串口号
|
* @param botelv 波特率*/
|
public static SerialPort open_chuankou(String chuankou,int botelv) {
|
SerialPort serialPort=null;
|
try {
|
serialPort =SerialPortUtil.openSerialPort(chuankou,botelv);
|
} catch (NoSuchPortException e) {
|
// TODO 自动生成的 catch 块
|
ShowMessage.zidingyi(chuankou+"不存在!");
|
} catch (PortInUseException e) {
|
// TODO 自动生成的 catch 块
|
ShowMessage.zidingyi(chuankou+"已被占用!");
|
} catch (UnsupportedCommOperationException e) {
|
// TODO 自动生成的 catch 块
|
ShowMessage.zidingyi(chuankou+"不支持操作!");
|
}
|
return serialPort;
|
|
}
|
}
|