Edit:
To assign a fixed port number to a USB serial device connected to Raspberry Pi, go to post #10.
----------
I'm looking at the possibility to improve the jSerial libray by adding the getLinuxPortProperties method from jSSC (SerialPortList.java).
The purpose is to be able to select a specific USB/RS232 adapter connected to a Raspberry Pi, based on the idVendor or idProduct or serial number of the adapter.
jSerial – Serial.java: https://github.com/AnywhereSoftware...r/src/anywheresoftware/b4j/serial/Serial.java
jSSC – SerialPortList.java: https://github.com/gohai/java-simpl.../processing/src/java/jssc/SerialPortList.java
Method getLinuxPortProperties from line 368:
I must say that programming in Java is not my strong side , but I have tried to find inspiration by looking at other examples where maps are used.
If I understand correctly, I should add following lines at the beginning of the jSerial library in order to use the map object:
and then the new method could be something like this:
Alternative 1:
Alternative 2:
Could someone tell me if I'm on the right way with one of these two alternatives, or if I'm completely lost?
Thank you in advance for your help
To assign a fixed port number to a USB serial device connected to Raspberry Pi, go to post #10.
----------
I'm looking at the possibility to improve the jSerial libray by adding the getLinuxPortProperties method from jSSC (SerialPortList.java).
The purpose is to be able to select a specific USB/RS232 adapter connected to a Raspberry Pi, based on the idVendor or idProduct or serial number of the adapter.
jSerial – Serial.java: https://github.com/AnywhereSoftware...r/src/anywheresoftware/b4j/serial/Serial.java
jSSC – SerialPortList.java: https://github.com/gohai/java-simpl.../processing/src/java/jssc/SerialPortList.java
Method getLinuxPortProperties from line 368:
jSSC – SerialPortList.java – getLinuxPortProperties:
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;
}
I must say that programming in Java is not my strong side , but I have tried to find inspiration by looking at other examples where maps are used.
If I understand correctly, I should add following lines at the beginning of the jSerial library in order to use the map object:
jSerial:
import java.util.Map.Entry;
import anywheresoftware.b4a.objects.collections.Map;
and then the new method could be something like this:
Alternative 1:
jSerial - Alternative 1:
/**
* Returns a map with the properties of a port.
* To be used with Linux operating system.
*/
public Map<String, String> GetLinuxPortProperties(String portName) {
Map<String, String> mProps = new HashMap<String, String>();
mProps.Initialize();
mProps.getObject().putAll(SerialPortList.getLinuxPortProperties(portName));
return mProps;
}
Alternative 2:
jSerial - Alternative 2:
/**
* Returns a map with the properties of a port.
* To be used with Linux operating system.
* The keys are the properties names (strings) and the values are the properties values (strings).
*/
public Map GetLinuxPortProperties(String portName) {
Map mProps = new Map();
mProps.Initialize();
for (Entry<String, String> e : SerialPortList.getLinuxPortProperties(portName).getObject().entrySet()) {
// or getObject().SerialPortList.getLinuxPortProperties(portName).entrySet() ??
mProps.Put(e.getKey(), e.getValue());
}
return mProps;
}
Could someone tell me if I'm on the right way with one of these two alternatives, or if I'm completely lost?
Thank you in advance for your help
Last edited: