In this part of the lab, you'll work with sample code that shows how to receive messages in Java sent by your Arduino using serial communication. The Arduino transmits (TX) messages across the serial bus; the Java program receives (RX) them and prints them to the console.
In the steps below, text highlighted in yellow denote values you will need to change to update your setup.
Take the following code and place in a file named SimpleSerialRX.java.
/*
* Adapted from Yu Hin Hau's code
* https://billwaa.wordpress.com/2012/05/18/engineering-arduino-to-java-communication-2/
*
* Simple example of Java and Arduino Communication
* Receives messages using the serial bus and prints them out.
*/
import java.util.Scanner;
import jssc.SerialPort;
import jssc.SerialPortList;
import jssc.SerialPortException;
public class SimpleSerialRX
{
/**** SETTINGS -- change to match your setup! *******/
// change the following to match the serial port you use for the Arduino
final static String SERIAL_PORT_ADDRESS = "/dev/tty.usbmodem1421";
// change to match the baud rate you're using from Arduino
final static int BAUD_RATE = 19200;
/**** END SETTINGS *********************************/
//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 = ":";
private static SerialPort serialPort;
private static Scanner reader;
public static void main(String[] args)
{
//Initialize Port
serialPort = new SerialPort( SERIAL_PORT_ADDRESS );
//Create Scanner to read from Console
reader = new Scanner(System.in);
try
{
System.out.println( "about to open port" );
//Open Serial Port and set Parameters
serialPort.openPort();
serialPort.setParams( BAUD_RATE, 8, 1, 0);
// begin main loop
while ( true )
{
// try to receive a message from the serial port
readFromArduino();
}
}
catch (SerialPortException ex)
{
System.out.println(ex);
}
}
/**
* RX (receive) data from the serial port; print it out.
**/
public static void readFromArduino()
{
byte[] buffer;
try
{
System.out.println( "reading from arduino" );
//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)
{
System.out.println(ex);
}
}
}
Follow the same steps from the previous part to update the settings and compile your code:
Update the values of the constants to match your setup. Notice the settings area at the top of the class file.
The SERIAL_PORT_ADDRESS should match the port you select in the Arduino application.
The BAUD_RATE must match that of the Arduino sketch (you should not have to change this).
Compile your code. This class relies on the JSSC (Java Simple Serial Connector) library, which is included in the Arduino environment (versions 1.5.6+). Therefore, in order to compile your java class, you'll need to include this library in the class path.
First, determine the path of the library; it is actually stored as a JAR within your Arduino application. To see the actual file, right click your Arduino application in the Finder and select Show Package Contents. Within the resulting directory structure, navigate to Contents -> Resources -> Java. Within this directory, you should see a file name jssc****.jar, where the **** indicates the version number. Right-click that file and select Get Info. In the resulting window, look for the path to the file (next to "Where").
You will need to use this path along with the -cp flag when compiling your java program. For example, using the above path, I would compile my code with the command:
javac -cp /Applications/Arduino1.6.0.app/Contents/Resources/Java/jssc-2.8.0.jar *.java
Confirm that your code compiled -- you should see a SimpleSerialRX.class class file.
Copy and upload the following Arduino sketch. It transmits(TX) messages to the Serial bus once per loop iteration. The message that is sent is simply the number of the loop iteration.
/*
* Adapted from Yu Hin Hau's code
* https://billwaa.wordpress.com/2012/05/18/engineering-arduino-to-java-communication-2/
*
* Simple example of Java and Arduino Communication
* Sends messages using the serial bus.
*/
// settings
int serialBaudRate = 19200;
int LED = 13;
//Define special symbols
char start_char = '@';
char end_char = '#';
char sep_char = ':';
// tx, rx data
String temp; // temp memory for transmitting data
int messageNum = 0; // sent with number of loop iteration
void setup()
{
// for communication
Serial.begin(serialBaudRate);
// for LED output
pinMode(LED, OUTPUT);
}
/**
* At each loop iteration, send/transmit (tx) a message with the next number.
**/
void loop()
{
// send one message
transmitNextMessage();
}
/**
* Send a message with the next number.
**/
void transmitNextMessage()
{
// begin the message, includes sending a special start character
startStream();
// main message
writeStream(messageNum++);
// end the message, includes sending a special end character
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();
}
As with the previous part of the lab:
Notice the BAUD_RATE value matches the constant from the Java file -- it is important that they agree on the communication rate!
Upload your sketch to the Arduino.
Do not open the Serial monitor, as it will interfere with the java program's ability to access the bus.
Now, it's time to test out receiving messages in Java. Remember that the Java program relies on the JSSC library. Therefore, you need to again include the JAR in the classpath; in addition, you will need to include the directory with the class file of your java program. Here, we assume the current directory, denoted by the dot (.), and append it to the path by using the OS-dependent path separator ( : for *nix systems, including Mac OS X).
Run your java program with the java command and -cp flag, as in
java -cp /Applications/Arduino1.6.0.app/Contents/Resources/Java/jssc-2.8.0.jar:. SimpleSerialRX
You should see numbers being printed out -- these are the messages being received from the Arduino. Notice that you do not see every message. Because of the speed at which messages are being sent from the Arduino, the Java program does not see each one; they do not get stored on the bus.