public static Map<String, String> getPortProperties(String portName) {
if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX) {
return getLinuxPortProperties(portName);
} else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
return getNativePortProperties(portName);
} else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){
// TODO
return new HashMap<String, String>();
} else {
return new HashMap<String, String>();
}
}
public static Map<String, String> getLinuxPortProperties(String portName) {
Map<String, String> props = new HashMap<String, String>();
try {
// portName has the format /dev/ttyUSB0
String dev = portName.split("/")[2];
File sysfsNode = new File("/sys/bus/usb-serial/devices/"+dev);
// resolve the symbolic link and store the resulting components in an array
String[] sysfsPath = sysfsNode.getCanonicalPath().split("/");
// walk the tree to the root
for (int i=sysfsPath.length-2; 0 < i; i--) {
String curPath = "/";
for (int j=1; j <= i; j++) {
curPath += sysfsPath[j]+"/";
}
// look for specific attributes
String[] attribs = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
for (int j=0; j < attribs.length; j++) {
try {
Scanner in = new Scanner(new FileReader(curPath+attribs[j]));
// we treat the values just as strings
props.put(attribs[j], in.next());
} catch (Exception e) {
// ignore the attribute
}
}
// stop once we have at least one attribute
if (0 < props.size()) {
break;
}
}
} catch (Exception e) {
// nothing to do, return what we have so far
}
return props;
}
public static Map<String, String> getNativePortProperties(String portName) {
Map<String, String> props = new HashMap<String, String>();
try {
// use JNI functions to read those properties
String[] names = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
String[] values = SerialNativeInterface.getPortProperties(portName);
for (int i=0; i < names.length; i++) {
if (values[i] != null) {
props.put(names[i], values[i]);
}
}
} catch (Exception e) {
// nothing to do, return what we have so far
}
return props;
}