[Engineering] Arduino to Java Serial Communication

Arduino is awesome, but it is pretty much useless as a data acquisition (DAQ) device if we can only read the data in the Arduino IDE. I remember back in Freshman year, worked on a wireless board for my Intro to Engineering project. Back then, not knowing anything really from programming, I decided that it would be cool to make a wireless remote game controller like the Wii controller. So I dug right into it, using the manufacturer’s sample program to get the data I need, write it into a text file, import it into a game I wrote with Game Maker… omg, the errors, the lags. The TA tried to help but it was hopeless, lol. Then I did it again with the Space Hopper project, if I only know how to read Joystick data in Java…

Anyway, did a bit of coding today and this is what I got after messing around for couple hours. Most time was spent trying to get Arduino to send the data out correctly. C++ is just… not my thing. Will probably write a Java Class for it to make it even easier to use.

[WARNING: I REALLY HAVE NO IDEA REALLY WHAT I AM DOING AND SOMEHOW THIS WORKS! THIS PAGE IS JUST TO DOCUMENT MY NOTES!]

Alright, so first, we need a Serial Comm library for Java call jSSC (java-simple-serial-connector). This is the easiest one I found, so I decided to mess around with this one first.

jSSC Google Project Page
http://code.google.com/p/java-simple-serial-connector/

After installing the library in Netbeans by adding the jssc.jar file to your project, we are ready to code! Posted below are the source codes I used in my messing around.

So hardware-wise, just hook the Arduino to your computer through a USB. Make sure you don’t run the Java code and the Arduino Serial Monitor at the same time. You can only have one program use the port at a time.

Explanation of the changing everything to String is in part #2.

Arduino to Java Serial Communication 2

 

 

Arduino Code:


//Yu Hin Hau
//5/17/2012
//Arduino to Java

//Define special symbols
char start_char = '@';
char end_char = '#';
char sep_char = ':';
//Define data variables
//Note: these values are max range
//for int and long in Arduino
char name[] = "Arduino Uno";
int data1 = 32767;
long data2 = 2147483647;
//Temporary String holder
String temp;

void setup() {
 //Start Serial Port
 Serial.begin(9600);
}

void loop() {
//Data Export Loop
startStream();
writeStream(name);
writeStream(data1);
writeStream(data2);
endStream();
}

//Methods to Convert Everything to String
//Then Send out through Serial Port

void startStream()
{
 Serial.write(start_char);
 Serial.flush();
}

void endStream()
{
 Serial.write(end_char);
 Serial.flush();
}

void sepStream()
{
 Serial.write(sep_char);
 Serial.flush();
}

void writeStream(int data)
{
 temp = String(data);

 byte charBuf[temp.length()];
 temp.getBytes(charBuf,temp.length()+1);

 Serial.write(charBuf,temp.length());
 Serial.flush();
 sepStream();

}

void writeStream(long data)
{
 temp = String(data);

 byte charBuf[temp.length()];
 temp.getBytes(charBuf,temp.length()+1);

 Serial.write(charBuf,temp.length());
 Serial.flush();
 sepStream();
}

void writeStream(char string[])
{

 Serial.write(string);
 Serial.flush();
 sepStream();
}

Java Code


import jssc.SerialPort;
import jssc.SerialPortList;
import jssc.SerialPortException;

public class ArduinoComm {

//Declare Special Symbol Used in Serial Data Stream from Arduino
 final static String start_char = "@";
 final static String end_char = "#";
 final static String sep_char = ":";

public static void main(String[] args) throws InterruptedException {

//Display Available Ports -- ideas: let user choose?
 String[] portList = SerialPortList.getPortNames();
 for(int i = 0; i < portList.length; i++)
 System.out.println(portList[i]);

//Define Serial Port # -- can be found in Device Manager or Arduino IDE
 SerialPort serialPort = new SerialPort("COM11");
 try {

//Open Serial Port
 serialPort.openPort();

 //Define Parameter -- can be found in Device Manager
 // baudRate, iataBits, stopBits, parity
 serialPort.setParams(9600,8,1,0);

byte[] buffer;

//Filter out bad data from Arduino initialization
 buffer = serialPort.readBytes(200);

//Retrieve data from Arduino -- read 100 bytes
 buffer = serialPort.readBytes(100);

//Convert bytes into String
 String dataStream = new String(buffer);

//Isolate Data Stream using symbols defined earlier
 dataStream = dataStream.substring(dataStream.indexOf(start_char)+1);
 dataStream = dataStream.substring(0,dataStream.indexOf(end_char)-1);

 //Retrieve data sent by Arduino in form of Strings
 String[] data = dataStream.split(sep_char);

//Display obtained data
 for(int i = 0; i < data.length; i++)
 System.out.println(data[i]);

}
 catch (SerialPortException ex) {

 //Display Errors
 System.out.println(ex);

}
}
}

Advertisements

7 thoughts on “[Engineering] Arduino to Java Serial Communication

  1. Pingback: [Engineering] Arduino to Java Communication 2 | Billwaa's Blog

  2. Hi Billawa,

    The scm is an alternative library to rxtx/javaxcom.
    Wiki : http://www.embeddedunveiled.com/
    Repository : https://github.com/RishiGupta12/serial-communication-manager
    Video : https://www.youtube.com/watch?v=fYLQbelGunQ

    It supports RS-232 control signals handshaking, monitoring and has been ported to Linux, MAC, Solaris and Windows operating system. It is consistent, portable, efficient, reliable, testable, extensible, modifiable, scalable library.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s