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)
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
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);
}