Here is the code that I am implementing for Enumeration of devices. I am able to detect and display all the Serial devices connected.But suppose I connected another device and then try to recall this function, It is always showing the devices that are connected during first run of the code.
Code Snipet:
public void Listports() {
Enumeration ports = null;
ports = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
{
while (ports.hasMoreElements()) {
portId = (CommPortIdentifier) ports.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
}
}
}
}
i.e. For Example, in the first Call of the function , COM1 and COM3 are displayed.
Now suppose, a Serial Device connected is loaded on COM27. So If we re-run the code it is displaying only COM1 and COM3, and no COM27.
Another scenario, Before my first run of the code a Serial device is loade on COM27. Now on first run it shows COM1,COM3,COM27. Now remove the COM27 device and re-run the above piece of code, It still shows COM27 is connected.
Any Help in this regard is greatly Appreciated.
Thanks,
Abhi
Comm port API
you just get a list op comm ports, you can then for example try if the port is owned aka used by some application with isCurrentlyOwned()
Basicly just iterating a list doesnt tell you anyhting in the list until you further test it.
Related
I've created a Java GUI which lists all serial ports in a drop down menu from which the user selects the correct port and clicks connect. Connection to an Arduino is then established and the user is able to perform some actions. I get the available ports using Fazecast JSerialComm:
SerialPort[] ports = SerialPort.getCommPorts();
I grab the ports and put the results into the drop down. This works flawlessly BUT only when the Arduino is plugged into the Mac BEFORE launching my Java GUI. Is there a way to detect a hotplugged device in Java? I already thought of getting the com ports periodically (every second or so) but to me that does seem to be a very elegant solution.
update:
I found this answered by another user:
Serial communication manager have APIs to find serial port names like COMxx dynamically. Just connect your USB-USRT IC and SCM library will tell you the device node for it. Just google for Serial communication manager. It is hosted on GitHub.
src
You might do this by checking if the port is opened or not after trying to open it using:
SerialPort[] serialPorts = SerialPort.getCommPorts();
SerialPort liveSerialPort = null;
for (SerialPort p: serialPorts) {
p.openPort();
if (p.isOpen()) {
liveSerialPort = p;
System.out.println("HERE opened port = " + liveSerialPort.getSystemPortName());
break;
}
}
I am trying to establish a serial communication with a USB driven hardware. Unfortunately, the hardware is not recognized in the device manager under the listed COM ports. Therefore, it does not appear in my list and I don't know its COM port number. Instead the device is listed as another type of hardware.
How can I still connect with it?
I am currently using the following code to open my USB ports.
public ArrayList<String> getPorts() {
CommPortIdentifier serialPortId;
ArrayList<String> portID = new ArrayList<String>();
Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
String portString= new String(serialPortId.getName());
portID.add(portString);
}
}
return portID;
}
How can I still connect with it?
You need a java binding to whatever driver the hardware actually uses, like LibUSB or hidapi.
I am trying to communicate with Edito (Seiko) KSM347 printer through Serial Port. I have successfully printed data through echo "test" > /dev/usb/lp1 or Java's code:
FileOutputStream fs = new FileOutputStream("/dev/usb/lp1");
PrintStream ps = new PrintStream(fs);
ps.write(cmd); // cmd is bytes with corresponding commands
ps.flush();
It works properly too. But I need to send data and get response. In other projects I have been using RXTX library which was working good with for example Huawei MU609 modems. Now this library does not see these /dev/usb/ ports.
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// this list contains only /dev/ttyS0 and /dev/ttyUSB0 devices.
How to force RXTX to use these ports? Even setting -Dgnu.io.rxtx.SerialPorts does not work. Please help :)
I'm writing a java program, and right now I have a setup-file which contains a COM port number. which has to be changed if the device changes COM port number.
This is not very user friendly. Therefore I want to be able to get a list of COM port ID's and let the user select the right device by its ID. I've tried googling, but without much success.
By ID I mean if you check the Device Manager: "COM Port ID (COM<#>)". Check the with red marked text seen in the following picture:
I have tried the following libraries:
javax.comm - CommPortIdentifier,getPortIdentifiers();
jssc - SerialPortList.getPortNames();
But I have been unable to find out if it is possible to get the COM port ID, as the two above methods just return the number of the COM Port. Does anyone know of a way to get the COM port IDs?
I used rxtxcomm.jar and rxtxSerial.dll to communicate with an Arduino. This snippet should get you the available ports:
#SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = portEnum.nextElement();
System.out.println(currPortId.getName() + " - " + currPortId.getCurrentOwner());
}
Here's an article with some further details: https://blog.henrypoon.com/blog/2010/12/25/installing-rxtx-for-serial-communication-with-java/
iam just simply trying to know what are the input devices and output devices connected to the system. Do there any api to get information about input/output devices using java?
EDIT
independent of any operating system.
Please suggest.
To find what usb devices are connected to the system you can use jUSB. This article has more in-depth information on how to use the api. In particular, to find all usb devices (slightly modified from the article):
Host host = HostFactory.getHost();
// Obtain a list of the USB buses available on the Host.
Bus[] bus = host.getBusses();
// Traverse through all the USB buses.
for (int i = 0; i < bus.length; i++) {
// Access the root hub on the USB bus and obtain the number of USB ports available on the root hub.
Device root = bus[i].getRootHub();
int totalPorts = root.getNumPorts();
// Traverse through all the USB ports available on the
// root hub. It should be mentioned that the numbering
// starts from 1, not 0.
for (int j=1; j<=total_port; j++) {
// Obtain the Device connected to the port.
Device device = root.getChild(j);
if (device != null) {
// USB device available, do something here.
}
}
}
Similarly, you can use the api for MIDI systems to find what MIDI devices are connected, see the java tutorials for more information.
I think you should try commandline via java Runtime.getRuntime().exec(command);
According to this site http://michaelminn.com/linux/command_line
Following command should return this:
lspci: Lists information about devices connected to the internal PCI busses.
lspci -vv: Full dump of information.
so
String command="lspci";
And from windows you may need to download DevCon command! http://support.microsoft.com/kb/311272/EN-US
so
String command="devcon find";