Communication Protocol with Java


I realized a Java program to communicate with the card without using the Arduino software. But, I haven't the communication protocol USB ( code source) for my program. I don't know what instruction send and write in my program, to control servo? Where is the serial code source of this software?


The Arduino board is recognized as a serial port by the Operating System. All you have to do from Java or whatever language you happen to code with is to be able to write and read from a serial port.

The problem is that I do not know what direction / line of code to write in my Java code in Eclipse to drive my two servo motors? I get to send data to the card and I get but this is not the right values!! Looking for source code source on google but I can not find what I want! Have you information???

Take a look at Bitlash at -- Bitlash is an Arduino sketch that interprets commands you send over the serial port.

Here's a blog post showing how to use Bitlash on the Arduino to drive servos by sending commands from a program on the PC:

Happy hacking,


I think you are a bit confused.

The PC sends bytes to Arduino by writing to the serial port. How to do this is independent from Arduino.

The Arduino gets those bytes by using (this function gives you one byte at a time).

To respond to the PC, the Arduino uses Serial.print(...).

On the PC side, you have to manage bytes coming in from the serial port. Again, how to do this is a matter of how you code on the PC, not on the Arduino.

You'll have to design a (simple) protocol to make the two programs talk to each other. Then on the Arduino you'll have to write a routine that gets the serial bytes and parses the commands.


PC to Arduino: *A0#

Arduino to PC: *A0690#


In plain english: PC: give me the value of analog channel 0 Arduino: the value of analog channel 0 is 690

Details: * = start of message

= end of message

A = command byte (in this example A means "analog channel value") 0 = command parameter (in this example, the channel number) 690 = command result (in this example, current value analog read)

Using two special chars as start and end of frame makes the serial protocol decoding routine simple (imho). Also, using plain ascii instead of binary numbers makes things easier because: - you can use standard string manipulation functions - you can test the protocol via serial monitor by writing the messages by hand


The easiest way is to program the card with the arduino software then drive the servos via my java program on Eclipse by changing just the value of my variables? You're thinking that it will work?

That's a good summary.

  1. Upload the servo.pde sketch to the Arduino. Verify you can talk to Bitlash using the serial monitor, and make the servos wiggle as you like using the command line interface to send, for example "servo(4, 120)".

  2. From your Java program, open the virtual serial port and send the same commands programmatically to the Arduino.


Using two special chars as start and end of frame makes the serial protocol decoding routine simple (imho).

Not only that, but if you don't see those two bytes, you can ignore the line as likely meaningless, and set up an error condition (or message) to handle such a situation.

This is useful if you are working in an electrically noisy environment and/or your system is "mission critical", in that if an error gets through and is interpreted wrong, it could cause problems (for instance, commands to a large semi-autonomous robot).


As an additional safety measure, one could reserve the last two chars before the end-of-frame marker to represent the “packet” checksum in hexadecimal notation.

Why do you think I use "Bitlash? I load my program into the Arduino board and after I close the Arduino software to control my servos directly with my Java program (with multiple window button). The problem is that the card get bytes but does not understand what it receives??? I do not know why???

And this program works with another card that I have on a robot but the problem is that I can not find the source code for the Arduino software.

Please post the arduino sketch, so we can help better.

This is the Arduino sketch: It works very well with the Arduino software!!!But no with Java sketch…

#include <Servo.h>

const int SERVO1=2;
const int SERVO2=3;
int ReceptionOctet=0; // variable de stockage des octets reçus par port série
int ReceptionNombre=0; // variable de calcul du nombre reçu par port série
int impulsion=1500;
Servo mon_servo1;
Servo mon_servo2;

void setup() {
pinMode(SERVO1, OUTPUT);
pinMode(SERVO2, OUTPUT);


void loop(){
if (Serial.available()>0) {
while (Serial.available()>0) {


if ((ReceptionOctet>=0)&&(ReceptionOctet<=9))
ReceptionNombre = (ReceptionNombre*10)+ReceptionOctet;

Serial.print ("Nombre recu= ");

if((ReceptionNombre>=500) && (ReceptionNombre<=2100)){
if (impulsion>2600)impulsion=2600;
if (impulsion<300)impulsion=300;
Serial.print ("Impulsion servomoteur1 = “);
Serial.println (” microsecondes ");
if((ReceptionNombre>=2101) && (ReceptionNombre<=3600)){
if (impulsion>(3600))impulsion=2600;
if (impulsion<(2101))impulsion=300;
Serial.print ("Impulsion servomoteur2 = “);
Serial.println (” microsecondes ");


delay (1000);


First of all, try to be precise when using terms like "arduino software" "sketch" etc. We are not in your head nor in your lab, so unless you are VERY precise in describing your setup and problems, it becomes difficult to try and decode your words. And since here we're all volounteer, someone may feel it's too much work on his side, and go away. /rant

The sketch you posted has nothing wrong at first sight. You just have to make sure that the java program emits the right bytes for it. Or else, you have to modify the Arduino sketch so that it understands the java program's protocol.


The problem is resolved I order my two servos independent manner in Java. It remains for me to establish a new protocol under Arduino to communicate with them (if someone has already done something like I am trying to get information).

Thank you