I already said:
All my arduino codes work fine on the serial monitor.
It means I always test on the serial monitor before testing with the java program ![]()
Since I couldn't solve the problem I turned to JSSC library which is quite similar to RXTX.
Now it works fine and I can command arduino from java. That's the code for java :
import jssc.SerialPort;
import jssc.SerialPortException;
/**
*
* @author scream3r
*/
public class Write {
/**
* @param args the command line arguments
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
SerialPort serialPort = new SerialPort("COM2");
try {
System.out.println("Port opened: " + serialPort.openPort());
System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
Thread.sleep(3000);
for (int i = 0; i<10; i++){
if (i%2 == 0){
System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("1".getBytes()));
}
else {
System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("0".getBytes()));
}
Thread.sleep(1000);
}
System.out.println("Port closed: " + serialPort.closePort());
}
catch (SerialPortException ex){
System.out.println(ex);
}
}
}
And that's the Arduino code :
int r ;
int led = 13 ;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop(){
if (Serial.available()){
r = Serial.read() ;
if (r == 49){
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}
}
Thank you all for your help ![]()