Please note that I am making some assumptions.
The first is that you have already made yourself familiar with your arduino by going through the getting started tutorials at http://arduino.cc/en/Guide/HomePage. This would mean that you have already installed the arduino tools and have tested the USB communication between the arduino and your PC using the arduino tool. BTW I am using version 17.
The next is that you are familiar with java. If not then buy a book. Or you could pay a university a crazy amount of money to teach you and then buy a book to teach yourself. That is pretty much what I did. Whatever. I don't feel resentful about that at all. Anyway...
I am using eclipse for the java side of the development and the arduino tool for the sketch code. Since you are familiar with java and you understand the arduino tool then that shouldn't be a problem. I am also running everything under windows.
First thing that you need to do to start is install a communication library for your Java environment. You can use the Sun libraries or you can use the rxtx communication libraries. The Sun stuff is better documented and probably better supported but the arduino tool uses the rxtx and it seems to work well. The arduino tool also comes with all of the required rxtx libraries and it turns out it is pretty easy to install so I went with the rxtx. To use them I just copied the rxtxSerial.dll from the arduino directory into the bin directory of the jre that I am using (ex: c:/program files/java/jre6/bin). I also included the RXTXcomm.jar file from the ardurino tools lib directory in my class path. If everything is set up right, the first time that you run your java application you will see a message like the following:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
If you don't see that then you will probably see an error about the serial libraries not being found which may mean that you do not have your dll in the correct place. Make sure that you are using the jre that you think you are ( in eclipse you would look under Window->Preferences->Java->Installed JREs and see which one is checked ). If not then look at the rxtx link above or try using the sun api instead.
So here is some code. First is the sketch code the arduino will run. Basically communicating via serial is built into the arduino. It is how you program the thing. This program starts the conversation. It just sits there saying hello until somethng responds. Then it responds based on what it receives.
const int MAX_CMD_LEN = 128;
char cmd[MAX_CMD_LEN];
int quiet = 0;
void setup() {
Serial.begin(9600);
// start conversation
while (Serial.available() <= 0) {
Serial.println("hello");
delay(300);
}
}
void loop() {
readln(cmd);
if (strcmp(cmd,"") == 0) {
Serial.println("ping");
delay(1000);
readln(cmd);
if ( strcmp(cmd,"pong") != 0 ) {
// there was a problem so do something like flash a red led
Serial.println("error!");
}
} else if (strcmp(cmd, "hello") == 0) {
Serial.println("How are you?");
} else if (strcmp(cmd, "How are you?") == 0) {
Serial.println("I am working...");
delay(3000);
Serial.println("good bye.");
} else if (strcmp(cmd, "ping") == 0) {
Serial.println("pong");
}
}
void readln( char buffer[] ) {
char c;
int i = 0;
while( true ) {
if ( Serial.available() ) {
quiet = 0;
c = Serial.read();
if ( c == 13 ) { // carriage return
Serial.read(); // line feed
break;
}
buffer[i++] = c;
if ( i == (MAX_CMD_LEN-1) ) break;
} else {
if ( quiet > 10000 ) break;
quiet ++;
}
delay(10);
}
buffer[i] = NULL;
}
Some things to note about the sketch code. I have set the max length of a string to be received at 128 characters. Should be enough but if it isn't then we will change it. All communication is ended by sending a CR/LF. The arduino does this automatically with println(). In the Java code below we do it by appending the (char)13 + (char)10 on to all of the strings that are going to be sent. The readln has a timeout of sorts. If things are quiet for a while it will ping the java app. It should get a pong back and if it doesn't then something might have gone wrong. The quiet on the line could be a normal situation.
The Java class does almost exactly the same thing as the arduino sketch. Super simple. Listen on the port specified and respond. Note the com port that my machine decided to use was COM3. Yours might be different. Look in your control panel device manager under USB controllers.
package com.stacybro.arduino;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
public class Communicate {
static String CRLN = "" + (char)13 + (char)10;
public static void main(String[] args) {
InputStream input;
OutputStream output;
CommPortIdentifier portId = null;
SerialPort port = null;
try {
portId = CommPortIdentifier.getPortIdentifier("COM3");
port = (SerialPort)portId.open("testing", 4000);
input = port.getInputStream();
output = port.getOutputStream();
port.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
while ( true ) {
String inStr = readln(input);
String outStr;
System.out.println("From Arduino:" + inStr );
if ( inStr.equals("hello")) {
outStr = "hello" + CRLN;
System.out.println("Sending:"+ outStr);
output.write(outStr.getBytes());
} else if ( inStr.equals("How are you?" ) ) {
outStr = "I am functional." + CRLN;
System.out.println("Sending:"+ outStr);
output.write(outStr.getBytes());
Thread.sleep(5000);
outStr = "How are you?" + CRLN;
System.out.println("Sending:"+ outStr);
output.write(outStr.getBytes());
} else if ( inStr.equals("ping" ) ) {
outStr = "pong" + CRLN;
System.out.println("Sending:"+ outStr);
output.write(outStr.getBytes());
} else {
outStr = "I am testing" + CRLN;
System.out.println("Sending:"+ outStr);
output.write(outStr.getBytes());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
port.close();
}
}
static String readln(InputStream input) throws IOException {
String inStr = "";
while ( true ) {
// should probably do something here to do a timeout
if (input.available() > 0) {
int inChar = input.read();
if ( inChar == 13 ) { // carriage return
input.read(); // line feed
break;
}
inStr += (char)inChar;
}
}
return inStr;
}
}
Thanks to Silveira at http://silveiraneto.net/2009/03/01/arduino-and-java/. It is what got me started.
Saturday, January 9, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment