Real-time control from Java

Hi! An Arduino newbie here - in fact still waiting for my hardware to arrive.

What I really want to do is program the ports from a real-time Java program, so that I can do analog / digital IO from the host Java program (and have the user interact with it), not just in an uploaded on-chip program.

I presume that I have to use the USB serial port to do the communications, but what about the software at each end of this link? My ideal would be to find that there is an Arduino program which responds to a real-time interaction protocol, and a set of Java classes which provide the host-end of that protocol.

Is there any such software around?

I presume that the development / uploader system has some of the ingredients I would need. I've found the .java source code of some classes that seem to have the right names, but is there any documentation which would help me understand where to start with these classes?



Hi Chris,

.... What I really want to do is program the ports from a real-time Java program, so that I can do analog / digital IO from the host Java program (and have the user interact with it), not just in an uploaded on-chip program.

I presume that I have to use the USB serial port to do the communications, but what about the software at each end of this link?

What do you mean by "Real-time" something like this Real-time computing - Wikipedia then,
sorry for the bad news, but your real-time ambitions already stop inside the USB-scheduler of the operating system.
There is a realtime Linux and realtime Java, but no realtime USB-handling on either of them.

But if "Real-time" to you simply means "do something so fast, the user won't notice any delay from button click to action"
we would need to know what you want to be happening on a users input. Just to see if the arduino is fast enough for this.

I don't need 'real time' as in inflight-control of aircraft etc :wink:
USB serial port delays are OK.

What I'm getting at is that the program logic needs to be running in the host computer, with most of the time the Arduino just acting as a 'smart peripheral'. The host will decide which pins to take high / low, etc, the host will need to know the pin states, and analog input voltages.

So what I'm looking for is, effectively, some means of providing to a host Java program the equivalent APIs to pinMode(), digitalRead(), digitalWrite(), analogRead(), analogWrite() with all the parameters decided by the host Java program and each API call be transmitted along the Serial link. The Arduino would have 'slave' software which would implement the instruction received from the host and send a response.

There will be some additional logic needed in the Arduino itself to deal with the few really time-critical functions, e.g. 'take pin N high for M millisecs then low again' or 'keep adjusting output voltage Vo until input voltage Vi is Vref'. I would want to invoke these functions as some form of 'remote procedure call'.

I suppose all I'm really looking for is your confirmation that I've understood the Arduino architecture correctly and that this is all possible. I can do the implementation of the protocol myself - I've done similar things before over an async character-oriented connection.

Additional thought: maybe it would help if I describe the application…

This is a model railway control system I have in mind. The Arduino is pulsing the points (switchover) motors to change track direction and applying the engine drive voltage to different track segments.

The user is controlling the layout using a GUI interface implemented in Java - one which may have ‘track circuit feedback’ derived from sensing the current drawn by the engine motors - to show where the engines are on the layout.

So, the user clicks on the on-screen image of some points, the host Java software decides that points motor N needs to be activated, it passes the instruction to pulse pin N down to the Arduino, the Arduino pulses the pin, the pin is connected to a drive FET, which applies the power to the points motor. When the pulse has finished the Arduino sends an acknowledgment back to the host.

In software terms, the host sends a character sequence such as ‘P5,200’ (pulse pin 5 for 200 ms) down the USB serial link. The software I shall write for the Arduino receives these from the USB serial link, decodes these characters, calls digitalWrite(5,HIGH), delay(200), digitalWrite(5,LOW) then sends ‘OK’ back to the host.

Is there any reason why software such as this would not work?


Me again!

One think I can't understand, for example, is whether the Arduino can read incoming bytes from the serial port? I can see the serial.println() function, but no equivalent function. I think I saw somewhere in the forums that one can read inbound serial data by inspecting the state of a pin, but to use this to decode a 9600 baud data stream sounds like hard work!

So - (how) can I read incoming bytes from the serial stream?

And can I read them in a non-blocking' way?


surely there is a serial read command, just check the reference here:

I think what you want is to let the Arduino read the state of whatever digital and analog inputs you need to read, then wrap the readings up in some format that your Java app. can understand. Then send the data over the serial line (The USB line is a serial line in disguise). This is quite easy. Asuming of course that your Java app. has acces to serial communication.

You can of course also go the other way, send serial info from the PC to Arduino to control the state of various output pins.

This way the Arduino board is just reading and setting input / output and the main chunk of processing is going on in the Java app.

EDIT: As for nonblocking, i dont think so. Arduino has only one thread of execution, but it is possible to use a few interrupts.


Ah! The software handbook I had found was not complete. I can now see the function. Also I can see the Serial.available() function - which avoids the need for a non-blocking read - the point, of course, being that by knowing how many characters are available I don't end up waiting for characters that have not yet been sent.

It looks as if Arduino can do all that I want:)

All I need now is for the hardware to arrive.

Many thanks for all your help.


you may want to take a look at this: Arduino Playground - HomePage

processing is a sweet, easy to use platform/framework based on java. there is also a way of using a library in java where you call arduino.digitalRead(pin) directly in java and the library communicates with the arduino, and the arduino does it.

i've never used it personally, but it looks like what you're looking for.

This appears to be exactly what I was looking for.

Many thanks!


It sounds like this may lead to something like what you want:


As I understand it, Firmata essentially turns the Arduino into a fancy input/output peripheral for your PC.


Yes - that too looks helpful - thanks.

However the list of host languages which support the protocol used by Firmata is somewhat 'exotic'.

The protocol seems well documented, so I think I may implement it in Java for myself - unless it is already present and accessible in the 'Programming' environment (which appears to be built using Java).