List all serial port in java
This code might help you. It will give you the list of connected devices at serial port.
Use https://fazecast.github.io/jSerialComm/ library to run this code.
public class Test {
public static void main(String[] args) {
SerialPort[] ports = SerialPort.getCommPorts();
List<String> list = new ArrayList<String>();
for (SerialPort port : ports) {
System.out.println(port.getSystemPortName());
list.add(port.getSystemPortName());
}
System.out.println("List of serial port is : "+list);
}
}
Unable to read serial data from COM port in Java
So it turns out that I have to use the 32-bit version of Java 1.8 for accessing serial ports. My IDE on Linux would not accept the 32-bit libraries on a 64-bit machine even when forced to (by changing the "jdk_home" path in netbeans.conf
file), hence I was facing problems accessing the serial port.
I switched my Operating System to Windows 7, though I'm still on a 64-bit system, since Netbeans IDE 8.1 on Windows can run in both 64-bit and 32-bit modes with the respective source libraries. So I installed JDK 1.8 (32-bit) for Windows and then ran my code in the IDE, after referring to Javax.comm API on 64-bit Windows for getting it to work. Now, data from the serial port is read properly.
Serial port unique identification in Java
Ok I found the solution...
Since I am interested in Linux only, I check the folder /dev/serial/by-id
for unique id for each device. The files there are links to the serial ports.
So...
Reading file names in that folder gives you id's.
Reading the target of each link gives you the port name.
Here is a piece of my working code:
HashMap<String, String> portsMap = new HashMap<>();
File serialFolder = new File("/dev/serial/by-id");
File[] listOfDevices = serialFolder.listFiles();
for (int i = 0; i < listOfDevices.length; i++)
{
if (Files.isSymbolicLink(listOfDevices[i].toPath()))
{
try
{
String portName = listOfDevices[i].getName();
String id = Files
.readSymbolicLink(listOfDevices[i].toPath())
.toString()
.substring(
Files.readSymbolicLink(listOfDevices[i].toPath())
.toString().lastIndexOf("/") + 1);
portsMap.put(id, portName);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Reading and writing to a serial port using shell and Java
Instead of screen you may use command cu from UUCP package.
To install UUCP package sudo apt-get install uucp
or sudo yum install uucp
.
Then use this command:static String command = "cu -l " + port + " -s " + baudRate;
Some explanation:
- screen -d detaches session (it runs in background) that's why you do not see any data.
- screen requires terminal which is not easy from java. See How to open a command terminal in Linux?
Serialport read write in java
See this wikibook, section Event Driven Serial Communication.
Related Topics
How to Support Both Ipv4 & Ipv6 on Java
How to Create Native Binaries for Your Java App
Sqlite in Android How to Update a Specific Row
Understand the R Class in Android
Cross-Platform Way to Open a File Using Java 1.5
How to Use Chef to Update-Alternatives for Java Using Execute
Problems with Java3D Lib Configuration
Android JSONobject - How to Loop Through a Flat JSON Object to Get Each Key and Value
Permission to Write to the Sd Card
Firebase No Properties to Serialize Found on Class
How to Find My Pid in Java or Jruby on Linux
How to Encode a Wav to a Mp3 on a Android Device