Go Down

Topic: Java and Arduino (Read 1 time) previous topic - next topic


Hi guys, I hav been trying to develop a program to control my arduino robot in java, using the javax.comm library I had difficulties getting it to work so I decided to try the jcomm library which seems slightly easier to use, I have managed to get it to connect to the board, and when I trigger my events to move the robot the tx and rx LED's blink, indicating that there is communication between the two, however the robot does not respond, the big difficulty I am having I think has something to do with the microcontroller not understanding the data being recieved! my sketch on the arduino expects a char, but I am unable to send the data as a char I can only send it as a byte array   (byte[]) could anyone tell me how I might be able to modify my java code or indeed my Arduino sketch to get them to communicate effectively, I am even reciecing data back from the Arduino but it just makes no sense, (a whole bunch of special characters) 
Code: [Select]
void setup(void)
int i;
pinMode(i, OUTPUT);
void loop(void)

while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
//char monitor = monitor + val;
//Serial.println (monitor);
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
switch(val) // Perform an action depending on the command
case 'w'://Move Forward
forward (leftspeed,rightspeed);
case 'z'://Move Backwards
reverse (leftspeed,rightspeed);
case 'a'://Turn Left
left (leftspeed,rightspeed);
case 'd'://Turn Right
right (leftspeed,rightspeed);

Java Code

Code: [Select]
package arduinocontrol;

import jcomm.*;
import javax.swing.*;
import java.lang.Object;
import java.nio.Buffer;
import java.nio.ByteBuffer;

* @author Corey
public class Control extends CommRS232 {
    public void onRead(byte [] data)
StringBuilder builder = new StringBuilder();
for(int i = 0; i < data.length; i++)
char ch = (char)data[i];
//JOptionPane.showMessageDialog(null, builder);
System.out.println("Received data: "+builder.toString());


  //Control  rs232 = new Control();

  DCB dcb = new DCB();
Timeouts ts = new Timeouts();
EvtMask evt = new EvtMask();

public void setDCB()
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.DCBlength = 28;
dcb.fBinary = 1;
dcb.fOutxCtsFlow = 1;

public void setTS()
ts.ReadIntervalTimeout = 0;
ts.ReadTotalTimeoutConstant = 10;
ts.ReadTotalTimeoutMultiplier = 0;
ts.WriteTotalTimeoutMultiplier = 10;
ts.WriteTotalTimeoutConstant = 10;

public void setEVT()
evt.EvtMask = evt.EV_RXCHAR;

public void startCon()

JOptionPane.showMessageDialog(null,"Connection to COM4 Establishd successfully");
   catch(Exception e){

     JOptionPane.showMessageDialog(null, e);

public void stopCon()
    JOptionPane.showMessageDialog(null,"Connection to COM4 Terminated successfully");
    }public void move (char sByteTobeSent)

byte[] bytes = ByteBuffer.allocate(4).putInt(sByteTobeSent).array();



bytes and chars are the same size, and hold the same range of values. So, that is not the issue.

Random indenting and commented out code makes your program very hard to read.

This, though, needs some explaining:
Code: [Select]
byte[] bytes = ByteBuffer.allocate(4).putInt(sByteTobeSent).array();
Why are you converting a single byte to an integer? You have one byte to send ('w', 'a', 's', or 'd') that you seem to take great pains to obscure and mangle and get wrong. Whats wrong with simply sending sByteTobeSent? Or, is sByteTobeSent not a byte, in spite of its name?
The art of getting good answers lies in asking good questions.


I had the same problem recently. This happens because Arduino understands ASCII decimals and when try to print them it prints characters not readable. From your java code you need to convert your value into a string and then call the getBytes() method and store into byte array. Then send the byte array. This works for me. I think you do not need the second line begore the end (byte[] bytes = ByteBuffer.allocate(4).putInt(sByteTobeSent).array();)

For example:
Code: [Select]

String command = "w";
byte[] bytes= command.getBytes();


Thanks, I will give it a try and let you guys know how it goes!


Hey, thanks Paul S and Mandim, finally got it to work, had to change the recieving data type to a char as well!
much appreciated

Go Up