4

I am trying to create a program that can both transmit and receive via a USB to serial communication. I have learned by research of two files that I will need: a .jar file containing the actual library, and from what I understand the librxtxSerial.jnilib, which is a driver?

From the following website I have learned that both of these need to be present in my project in order to begin the serial communication process.

http://rxtx.qbang.org/wiki/index.php/Download

I am using the Eclipse IDE on a mac and cannot seem to get the correct configuration of files in order to make this work. There are no clear answers anywhere on the net and I am hoping that somebody can help clear this up for everybody.

Greenonline
  • 1,330
  • 8
  • 23
  • 31
Tyler
  • 43
  • 1
  • 3

2 Answers2

6

I've used NeuronRobotics nrjavaserial with great success on Linux and Mac OS/X. It embeds a native library into the jar file and then presents a Java interface. It is derived from RXTX and so I'm hoping it won't be a huge change for your project.

MAJOR EDIT

In the interest of saving you some time, here's what I'm doing currently. Note that I'm not using an IDE at this time - this is all command line.

import gnu.io.NRSerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Arrays;

public class PulseOximeter {
    public static void main( String[] argv ) throws IOException, UnsupportedCommOperationException {
        NRSerialPort serial = new NRSerialPort("/dev/rfcomm0", 115200);
        serial.connect();

        DataInputStream ins = new DataInputStream(serial.getInputStream());

        // read the first 10000 bytes a byte at a time
        for( int i = 0; i < 10000; i++ ) {
            int b = ins.read();
            if( b == -1 ) {
                System.out.println( "got EOF - going to keep trying" );
                continue;
            }
        }

        serial.disconnect();
    }
}

and the maven file to build it:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>tld.domainname</groupId>
    <artifactId>bluetooth</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <java.min.version>1.8</java.min.version>
        <maven.min.version>3.2.0</maven.min.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <resource.directory>src/main/resources</resource.directory>
    </properties>
    <prerequisites>
        <maven>${maven.min.version}</maven>
    </prerequisites>
    <dependencies>
        <dependency>
            <groupId>com.neuronrobotics</groupId>
            <artifactId>nrjavaserial</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>${resource.directory}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${java.min.version}</source>
                    <target>${java.min.version}</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <compilerArgument>-Xlint:all</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This is code for the Linux version - the biggest change will be the serial port name (the "/dev/rfcomm0") part. I don't have my Mac here so I'm sorry I forget the name of the Mac device but it is in /dev - something with Bluetooth if I remember correctly.

All this sample does is read bytes off of the serial (USB) port. Note that Java makes this a little more fun in that they come back as integers even if they are really bytes so you'll have some fun with any bit manipulations you need to do.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Thanks for the direction, I am following the instructions for mac OSx, so far i have built the jar. I am rather confused though because after "gradle build" i was given 3 jars: nrjavaserial-3.12.0-javadoc.jar, nrjavaserial-3.12.0-sources.jar, and nrjavaserial-3.12.0.jar. From this point i am unsure how to continue, I'm new to this type of work and "building the native code" goes right over my head. Is there any more help you can offer? – Tyler May 19 '16 at 16:48
  • Sorry to have put you through that - you can use Maven to download the pre-built jar - http://mvnrepository.com/artifact/com.neuronrobotics/nrjavaserial/3.12.1. What is your build environment like? – stdunbar May 19 '16 at 16:51
  • No worries, it was just a few hoops to jump through. By build environment I'm thinking you mean IDE, etc so I'm using eclipse mars.2 on mac OSx 10.11.5 El Capitan. I think i understand how to create a custom library in eclipse via add Jar but I'm not sure if there is anything else i need to add to the project. – Tyler May 19 '16 at 18:34
  • While not exactly what you're looking for, I included what I've done to this point in the edit above. – stdunbar May 19 '16 at 19:13
  • Thanks so much! Appreciate all your work it was a huge help. – Tyler May 19 '16 at 20:03
1

I faced the same problem a few years ago when I was setting up a Java application to receive bio-data from an arduino.

I wrestled and wrestled with the RXTX libraries and was never able to get them working properly (on both Linux and Windows). This seems to be a common problem (Stable alternative to RXTX).

My advice: move away from the RXTX libraries.

An alternative library I found that worked great and was easy to implement is the Java Simple Serial Connector (JSSC) library found at https://github.com/scream3r/java-simple-serial-connector.
And here are a few examples: http://www.javaprogrammingforums.com/java-se-api-tutorials/5603-jssc-library-easy-work-serial-ports.html


As far as setting up in eclipse, you should be able to include the jar in the following manner:

Right click your project -> Configure Build Path -> Add External JARs -> select the jar you downloaded.
Now in package explorer you should see a "Referenced Libraries" item that, when expanded, shows the jar you added.


Lastly, it's worth your time to look into tools like Maven or Gradle. They greatly simplify adding external libraries.

Cheers!

Community
  • 1
  • 1
Seanimus
  • 457
  • 4
  • 16