package com.zh.client.serial.utils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/获取com端口,windows专用/
public class ComPortScanUtil {
public static List getComPorts() {
List ports = new ArrayList<>();
try {
String command = "reg query HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM";
Process process = Runtime.getRuntime().exec(command);
InputStream in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = br.readLine()) != null) {
if (line == null || "".equals(line)) {
continue;
}
if (index != 0) {
String[] strs = line.replaceAll(" +", ",").split(",");
String comPort = strs[strs.length - 1];
ports.add(comPort);
}
index++;
}
} catch (Exception e) {
e.printStackTrace();
}
return ports;
}
public static void main(String[] args) {
System.out.println(getComPorts());
}
}