Unable to find serial ports - java

Using Rxtx API.
I am 100% sure there is a serial port available. On Debian, I can see /dev/ttyUSB3 exists when the device is connected.
Tried running getPortIdentifiers() and got nothing when I tried iterating over the hashset
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB3");
Also did not work. I got:
Exception Caused by: gnu.io.NoSuchPortException
What am I doing wrong? I am fairly sure rxtx was set up correctly.

The problem here is with how RXTX defines ports. By default, it will only look for certain serial ports(on Linux, /dev/ttySX). There are several options you have to fix this:
Set the gnu.io.rxtx.SerialPorts property when you start Java(either on the command line, or through code. See the Rxtx documentation for more details)
Use a different serial port library that lets you select serial ports manually.
For #2, the following are libraries that I know of:
RXTX
JSSC
PureJavaComm
JavaSerial (disclaimer: this is my library)

Related

Issues on COM-Communication with Qualcomm SURF-xxxx

im a Student worker for a automobile r&d Company. Im currently working on a Java application that is workling like QXDM but also offers additional Features. I'm stuck with the communication over the DM (Diagnostic mode) port. I tried to use PuTTy for that. Putty and also my application were able to connect to that Port. Now since I got the official dmss documentation, im still not able to get a reply on that port. Do you guys have had the same Problem or at least a similar one and may provide me some help finding the issue? To Switch the phone in diagnostic mode there is a AT-Command. But even though if I connect via puTTy and write down the command, I dont get a reply in putty neither the following diagnostic requests will work.
Does anyone know how to use puTTy to give AT Commands?
The Qualcomm device is connected via USB Dongle using Telit Drivers. Using Telit Serial Diagnostics Interface COM Port.
Using QXDM everything is working fine.

Specify COM port file in java

In Unix like operating systems, we can access serial ports through files such as /dev/ttyUSB0 or something. And according to this question, filenames such as COM1: can be used to access the serial ports. What is the java alternative for such file names? I don't want to use Serial Communication liberaries.
Edit
What I want my code to look like is this.
String INPUT_PORT_FILE_NAME = linux?"/dev/ttyUSB0":"<File name of comport>"
File in = new File(INPUT_PORT_FILE_NAME)
What I want is the widows alternative to a device file.
EDIT
I am on a linux machine, and I want to enable my code to be ported easily!
Yes, on Linux there is access to serial port for instance through device files /dev/ttyS0, /dev/ttyUSB0 and others. It really depends on hardware/chips used to communicate and even distributions.
If same hardware is used in your program it can be partly achieved. When I worked with Serial Comm libraries and real physical serial ports in Linux I used port numbers in config so number 3 meant n=3, so opens "COM"+(1+n) on windows or "/dev/ttyS"+n on linux. Maybe similar can be used for you to accessing port on /dev/ttyUSB"+n
But there is no grantee that port 2 will be /dev/ttyS1 and COM2 on the same computer after dual boot.
The way not using Serial Comm libraries is hard way and do not recommend it if you want portability in java. I recommend different port config depending on operating system.

How to communicate with arduino and java in serial port?

I was trying to send a message to Arduino (to test if the Arduino received any message the motor will turn on), the following code works if I run on Terminal
echo 1 > /dev/ttyACM0
But I tried this in Java
Runtime.getRuntime().exec("echo 1 > /dev/ttyACM0");
Anyone knows how to execute in Arduino Serial port?
I used the RXTX lib for Java serial connections.
Very similar code to what worked for me is shown here:
https://embeddedfreak.wordpress.com/2008/08/08/how-to-open-serial-port-using-rxtx/
Try RXTX library, this is Java wrapper for the serial port. Project website.
Example: source
Java doesn't provide support for serial communications out of the box. You need to use a 3rd party library or extension.
Oracle do provide one - Java Communications API
http://www.oracle.com/technetwork/java/index-jsp-141752.html
I have used this in the past with AVR Micro-controllers. There are several tutorials on the net to get it up and running.

CommPortIdentifier.getPortIdentifiers with zero ports on Linux

i am trying to connect serial port on ubuntu. However, It doesn't work for me. I succesfully run the same project on Windows just with different drivers. The problem is that I can't load any ports while I am using this:
CommPortIdentifier.getPortIdentifiers(); // i am using rxtx 2.1.7
It always return zero ports. I would like to use port ttyS0 whichworks great with minicon so i am sure that port is not blocked and the machine is not broken.
Anyone has a reason for this ?
It was just becouse low priviligies. I had to add myself to a group which is supposed to work with ttyS0.
I used this command
sudo chmod 666 /dev/ttyUSB0
I had the same problem and it worked the moment after I used this command. Like Smolda said, it is a permission problem.
if nothing helps, you should consider to add this line to your java code:
System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/yourtty");
did it for me. (Only if you work with the RXTX library)

How do I receive SNMP traps on OS X?

I need to receive and parse some SNMP traps (messages) and I would appreciate any advice on getting the code I have working on my OS X machine. I have been given some Java code that runs on Windows with net-snmp. I'd like to either get the Java code running on my development machine or whip up some Python code to do the same.
I was able to get the Java code to compile on my OS X machine and it runs without any complaints, including none of the exceptions I would expect to be thrown if it was unable to bind to socket 8255. However, it never reports receiving any SNMP traps, which makes me wonder whether it's really able to read on the socket. Here's what I gather to be the code from the Java program that binds to the socket:
DatagramChannel dgChannel1=DatagramChannel.open();
Selector mux=Selector.open();
dgChannel1.socket().bind(new InetSocketAddress(8255));
dgChannel1.configureBlocking(false);
dgChannel1.register(mux,SelectionKey.OP_READ);
while(mux.select()>0) {
Iterator keyIt = mux.selectedKeys().iterator();
while (keyIt.hasNext()) {
SelectionKey key = (SelectionKey) keyIt.next();
if (key.isReadable()) {
/* processing */
}
}
}
Since I don't know Java and like to mess around with Python, I installed libsnmp via easy_install and tried to get that working. The sample programs traplistener.py and trapsender.py have no problem talking to each other but if I run traplistener.py waiting for my own SNMP signals I again fail to receive anything. I should note that I had to run the python programs via sudo in order to have permission to access the sockets. Running the java program via sudo had no effect.
All this makes me suspect that both programs are having problem with OS X and its sockets, perhaps their permissions. For instance, I had to change the permissions on the /dev/bpf devices for Wireshark to work. Another thought is that it has something to do with my machine having multiple network adapters enabled, including eth0 (ethernet, where I see the trap messages thanks to Wireshark) and eth1 (wifi). Could this be the problem?
As you can see, I know very little about sockets or SNMP, so any help is much appreciated!
Update: Using lsof (sudo lsof -i -n -P to be exact) it appears that my problem is that the java program is only listen on IPv6 when the trap sender is using IPv4. I've tried disabling IPv6 (sudo ip6 -x) and telling java to use IPv4 (java -jar bridge.jar -Djava.net.preferIPv4Stack=true) but I keep finding my program using IPv6. Any thoughts?
java 16444 peter 34u IPv6 0x12f3ad98 0t0 UDP *:8255
Update 2: Ok, I guess I had the java parameter order wrong: java -Djava.net.preferIPv4Stack=true -jar bridge.jar puts the program on IPv4. However, my program still shows no signs of receiving the packets that I know are there.
The standard port number for SNMP traps is 162.
Is there a reason you're specifying a different port number ? You can normally change the port number that traps are sent on/received on, but obviously both ends have to agree. So I'm wondering if this is your problem.
Ok, the solution to get my code working was to run the program as java -Djava.net.preferIPv4Stack=true -jar bridge.jar and to power cycle the SNMP trap sender. Thanks for your help, Brian.

Categories