Reputation: 43
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.
Upvotes: 4
Views: 4612
Reputation: 17455
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.
Upvotes: 6
Reputation: 567
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!
Upvotes: 1