Pages: [1]   Go Down
Author Topic: Java and Arduino  (Read 1071 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup(void)
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}
void loop(void)
{


 
 
 
while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
Serial.println(val);
//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);
break;
case 'z'://Move Backwards
reverse (leftspeed,rightspeed);
break;
case 'a'://Turn Left
left (leftspeed,rightspeed);
break;
case 'd'://Turn Right
right (leftspeed,rightspeed);
break;
default:
stop();
break;
}

Java Code

Code:
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];
builder.append(ch);
}
//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()
{
   try{
//rs232.initComm();
       this.initComm();
//rs232.start();
       this.start();
//rs232.setDCB();
       this.setDCB();
//rs232.setTS();
       this.setTS();
//rs232.setEVT();
       this.setEVT();
//rs232.openComm("COM4");
       this.openComm("COM4");
       //this.openComm("Port_#0002");
       

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

     JOptionPane.showMessageDialog(null, e);
   }
    }

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


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

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Crete, Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 15
HelloArduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()smiley-wink

For example:
Code:
String command = "w";
byte[] bytes= command.getBytes();
write(bytes);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: