Cant get serial port in my new binding plugin

Hi,

I have recently started playing around with creating an OH2 plugin for EnOcean.
I have wanted to get a feel for how this would work and then decide if I would actually commit to it.
Now the first problem that I have is that I can’t seem to access the serial port. The USB300 device is plugged in and I can see that I have the /dev/ttyUSB0 device in my manjaro linux (which is basically arch linux).
When trying to open the port with:

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");

I get an exception that says this port does not exist.

When I try

Enumeration comports = CommPortIdentifier.getPortIdentifiers();

I get an empty return.

The obvious problem of missing permissions does not seem to be the problem as I have both added the user to the group of ttyUSB0 as well as changed the permissions to 666 on that device.

What am I missing here?

Hi jaykay,

just did pretty much the same as you…
Did you add it to the Java VM?

Something like: EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB0" in /etc/default/openhab2

Just with your ports that should do the trick.

Bests

1 Like

Hi benice,

that worked wonders. I am very new to Java and as such didn’t know about the need to do that.

Now that it finds the com port there is a different problem though.

When calling:

CommPort commPort = portIdentifier.open(SerialPortFactory.class.getName(), timeout);

in the EnOcean library that I am using, I get a InvocationTargetException in the invokeMethod method of the BaseMethod class.
This exception seems to wrap a NoSuchMethodError for the “open” method.

After a bit of googling this seems to stem from a conflict between the rxtx lib that is being used by the EnOcean library and the nrjavaserial library that is being used by OH. I found this info here: https://github.com/NeuronRobotics/nrjavaserial/issues/54
Now my problem is that I have no idea how to solve this.
I tried simply remove the rxtx*.jar file from the directory of the library but that didn’t do anything.
My lack of java development shows here.

Should I create a new thread for this issue?

Ohh man :smiley: New to Java and directly in the mess of serial support libraries? And then not just in a normal environment but in a OSGI environment?

I just imported the serial-package via MANIFEST.MF -> Import-Package: gnu.io, …
And then used the implementation via com.neuronrobotics.nrjavaserial_3.12.0.OH.jar file…

How do you include the jar file? You cannot just use add to build-path in Eclipse. (As it is then missing in the produced jar-file -> via mvn package).

You are right, it is a pretty steep learning curve for me right now.

OK so here the setup that I have:

  • Created the standard OH2 development environment with eclipse.
  • Followed the tutorial for creating a new plugin.
  • Cloned the git repo of the enj library from here: https://github.com/dog-gateway/enj-library
  • Called mvn clean package and mvn eclipse:eclipse -Declipse.pde for this package
    After that the enocean.library package magically appeared in my eclipse workspace in which I have the development setup for OH2.
    This package included the rxtx***.jar file as well.

Now I use this package in my plugin and try to create a connection.
The library tries all that port stuff that I posted previously and then gets that exception.
My guess is that the lookup for the class name of CommPortIdentifier first returns the nrjavaserial version and not the rxtx version, but the enj library is compiled against the rxtx version. But that is just what I deduce with my C++ knowledge. I have no idea how actual “linking” works in java.

I have not “used” or imported any packages or jar files manually at all.
When you say import the serial-packe via MANIFEST.MF, which MANIFEST.MF do you mean? That of the plugin or that of the library or what? And why would I need to do that in my plugin when the enj library already has it’s own version of the rxtx library?

Thank you very much for your help!

Not worked with the enj-lib as I implemented my own serial protocol…so I guess somebody else is better suited to answer.

But I just had a look how enj-libs MANIFEST is written…
Did you check your produced jar (from enj), do you have the lib folders inside it with the rxtx jar in?
If no, this is the issue :smiley:
If yes, I would first try to delete the bold part from: Import-Package: com.google.common.base;version=“14.0”,com.google.commo
n.collect;version=“14.0”,gnu.io
If this is also not helping, I would try to specify the version of the gnu.io package

Good luck :wink:

Okay your post was really helpful and made me play around with the enj package.
I tried removing the gnu.io part from the manifest file.
Then I rebuilt it with maven, which was probably stupid. After I rebuilt it, the deleted part in the MANIFEST.MF file returned.
Then I checked the pom.xml file and deleted the dependency to the rxtx lib. After that the build with maven failed because obviously it couldn’t find the types in the rxtx library.
Then I added this dependency:

<dependency>
    <groupId>com.neuronrobotics</groupId>
    <artifactId>nrjavaserial</artifactId>
    <version>3.12.1</version>
</dependency>

And now it seems to work. I no longer get the exception during code execution.
Apparently my guess was correct that the two libraries rxtx and nrjavaserial are conflicting here.
My question now is: Is this the proper way to do it? It seems to me like i just manually changed stuff in the library, so that it now depends on a different library. It seems pretty hacky to me. What would be the right way to do this?

Thank you so much for your time and help! You are awesome!