實驗物理技術:2009/02-06
登入

http://www.arduino.cc/playground/Interfacing/Java

Arduino and Java

via Dave Brink (dcb)

Overview

The IDE itself is written in Java and can communicate via the serial port, and even remembers which port/baud rate you used last time. We will leverage the arduino classes and files to get a very simple java application working.

Requirements

An arduino installation (I used 0012 on windows), a simple sketch loaded and confirmation in the IDE Serial monitor that it works (exit arduino after confirmation to save the com port info to the preferences.txt). Here is the arduino sketch I used for testing:

void setup(){

  Serial.begin(9600); 

}

void loop(){

 Serial.println("Hello world"); 

delay (1000); }

Serial in Java, leveraging Arduino classes/files

First things first, you need to be able to compile/run java.

I am not going to teach you java and am not in a position to test anything but windows. If anyone else can then please do so and discuss on forum for possible additions. I am however going to relate a procedure that allowed me to compile a simple Java program using only what came with my arduino installation and run it and have it communicate with the above arduino script.

Setup

So lets make our own copy of run.bat because it has most of those nice environment settings already sorted out. Then modify it to suit our needs.

1. go to a command prompt.

2. cd to your arduino directory (i.e. type "cd \arduino-0012")

3. copy run.bat testrun.bat

4. notepad testrun.bat and make the following changes:

 a. add . to the beginning of the classpath (thats a period) 

set CLASSPATH=.;java\lib\rt.jar;etc...

 b. change JAVA_HOME\java processing.app.Base 

to: rem JAVA_HOME\java processing.app.Base

 c. on a new line after rem JAVA_HOME\java processing.app.Base 

add: %1 %2 %3

5. save and exit testrun.bat

Now we can use testrun.bat to compile and or run Java.

Edit the new Java file

from the command prompt type: notepad Test.java (say Yes to create new file prompt) and cut and paste the following program:

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.InputStream;

import java.io.OutputStream;

import processing.app.Preferences;

public class Test {

    static InputStream input; 
static OutputStream output;
	public static void main(String[] args) throws Exception{ 
		Preferences.init(); 
System.out.println("Using port: " + Preferences.get("serial.port"));
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier( 
Preferences.get("serial.port"));
SerialPort port = (SerialPort)portId.open("serial madness", 4000); 
input = port.getInputStream();
output = port.getOutputStream();
port.setSerialPortParams(
Preferences.getInteger("serial.debug_rate"),
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
while(true){
while (input.available()>0)
System.out.print((char)(input.read()));
}
}

}

Save and exit notepad.

Compile the new Java file

We will use our testrun to compile it (ignore warnings but not errors) type: testrun jikes Test.java

if you do a "dir /od" (directory ordered by date) you should see the new Test.class file.

Run your new Java program

type: testrun java Test

and you should start seeing "Hello world" on your console. Hit ctrl+C when you had your fill.

C:\arduino-0012>testrun java Test

Using port: COM5

Stable Library

=========================================

Native lib Version = RXTX-2.1-7

Java lib Version = RXTX-2.1-7

Hello world

Hello world

Hello world

Hello world

Hello world

Terminate batch job (Y/N)? y

Notes

This is a simple connectivity example, you can also use the outputstream to send strings or numbers to the arduino as well. Also there are better Java development environments besides notepad and the command line (Eclipse comes to mind) but having to install all of that just for a proof of concept is a bit much, and the arduino does come with everything you need to get started with Java, at least in my experience. Other platforms should have something similiar to run.bat, perhaps it is a .sh file.


0最後修改紀錄: 2009/04/14(Tue) 18:31:02


尚未登入. (登入)

since 2011/06/20 18:23