Updated: How do you open an XBee device with Java on Linux?

I’ve got an XBee RF modem, and I’m using the XBee java api to write code for it.

On my windows 7 64bit box using Eclipse with JRE 1.7 64bit, I can successfully connect to the device using the boilerplate example code (after updating the COM port to my actual COM port).


	// TODO Replace with the serial port where your sender module is connected to.
	private static final String PORT = "COM2";
	// TODO Replace with the baud rate of your sender module.
	private static final int BAUD_RATE = 9600;

When I change the PORT variable on my ubuntu 14 64bit box using Eclipse with JRE 1.8 32bit, I see the following error in the console:

 |  XBee Java Library Receive Data Sample  |

WARNING:  RXTX Version mismatch
	Jar version = RXTX-2.2pre1
	native lib Version = RXTX-2.2pre2
com.digi.xbee.api.exceptions.InvalidInterfaceException: No such port: dev/ttyUSB0
	at com.digi.xbee.api.connection.serial.SerialPortRxTx.open(SerialPortRxTx.java:163)
	at com.digi.xbee.api.XBeeDevice.open(XBeeDevice.java:195)
	at com.redbird.ca.base.MainApp.main(MainApp.java:29)
Caused by: gnu.io.NoSuchPortException
	at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:269)
	at com.digi.xbee.api.connection.serial.SerialPortRxTx.open(SerialPortRxTx.java:161)
	... 2 more

I’ve tried:
PORT = “/dev/ttyUSB0”;
PORT = “dev/ttyUSB0”;
PORT = “ttyUSB0”;

etc, and consistently get that error.

I can successfully launch the XCTU config tool, and it sees the device attached as /dev/ttyUSB0, reads the config settings correctly, etc. I can push data to it from another modem and see packets received. So I don’t believe the issue is with the device itself, its configuration, or my machine’s ability to interact with it - those things all work using the XTCU tool.

What is the correct way to open an XBee port in java on linux?

It looks like that device isn’t even visible as a COM port… when I run the following code I get zero devices returned.

static void listPorts()
        java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() ) 
            CommPortIdentifier portIdentifier = portEnum.nextElement();

Ok. Solved it.

Turns out I was executing my Java with an elevated account, but it didn’t see that COM port in /dev until I ran it as root.

Noob question: how did you run it as root? I’m working with an Intel Edison, so I’m not running from the command line, but I can run shell commands from inside my code.