JSSC and arduino communication

Hi folks,

I got a problem over here. I installed JSSC plugin to eclipse. And the problem is how i should communicate? I mean i got a code on Eclipse:

import jssc.SerialPort;
import jssc.SerialPortException;

public class Main {

    public static void main(String[] args) {
        SerialPort serialPort = new SerialPort("COM1");
        try {
            serialPort.openPort();//Open serial port
            serialPort.setParams(SerialPort.BAUDRATE_9600, 
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
            serialPort.writeBytes("a".getBytes());//Write data to port
            serialPort.closePort();//Close serial port
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}

Arduino code:

int led = 13;

void setup() {   
  Serial.begin(9600);  
  pinMode(led, OUTPUT);     
}

void loop() {
  char i= Serial.read();
  if(i=='a'){
  digitalWrite(led, HIGH);  
  }             
}

I have no idea why, but led is just not turning on ;(

I have no idea why, but led is just not turning on

Ok, here's a clue:

            serialPort.openPort();//Open serial port

which resets the Arduino.

            serialPort.setParams(SerialPort.BAUDRATE_9600, 
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
            serialPort.writeBytes("a".getBytes());//Write data to port
            serialPort.closePort();//Close serial port

Jam some data at the Arduino while the bootloader is waiting for a new sketch, then close the serial port, resetting the Arduino again.

void loop() {
  char i= Serial.read();
  if(i=='a'){
  digitalWrite(led, HIGH);  
  }             
}

Read the serial data that isn't there. Well, that will never be an 'a', will it?

But when i don't use serialPort.openPort(), i get an error:

jssc.SerialPortException: Port name - COM4; Method name - setParams(); Exception type - Port not opened.

But when i don't use serialPort.openPort(), i get an error

Well, of course you do. Who said to not open the port?

What you need to do is WAIT after opening the port, for the Arduino to finish resetting.

You mean like using Thread.sleep(2000), should 2 seconds long enough?

A better way is to have the Arduino transmit a message from setup() such as

Serial.println("I'm ready");

and then have the Java code wait until it receives that message before sending anything.

This approach is in my demo code that I pointed you to in the other Thread about RxTx.

...R

PS. Its still not clear to me why you are persevering with Java when you find it so hard?

Could somebody give me example code or something?

Got it work. Thread closed ;) Thank you guys

rat97: Got it work. Thread closed ;) Thank you guys

HI, can you show me your code please?

studanka: HI, can you show me your code please?

In case you get no better reply the JRuby example in this demo may be of some help. (JRuby uses the JVM)

...R

I'm having the exact same problem :/

Here's my java code:

 SerialPort serialPort = new SerialPort("COM4");
        
          try {
            serialPort.openPort(); // Abre a porta série
            Thread.sleep(3000);
            serialPort.setParams(SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);         
            
            String s = "Test";
            byte[] a = s.getBytes();
            serialPort.writeBytes(a);
            
            serialPort.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }

And here's my Arduino code:

byte a;
void setup() {
  Serial.begin(9600);

}

void loop() {

  delay(1500);

  int x = Serial.available();
  Serial.println(x);
  
  if(Serial.available() > 0){
     a = Serial.read();
     Serial.println(a);
  }
}

It keeps saying that i have nothing to read from the SerialPort :s

My guess is that that this line is the problem

serialPort.closePort();

Your PC program should open the serial port, allow time for the Arduino to reset before trying to send data and then keep the serial port open until the PC program is completely finished with the Arduino.

In my programs I get my Arduino to send "Arduino is ready" from setup() and my PC program waits until it receives that. Look at the link in Reply #9

...R

Thank you Mr Robin :)

hello i have same problem plz can anyone give me the exat code for it

VineetJ0shi: hello i have same problem plz can anyone give me the exat code for it

In what way is the advice earlier in this Thread not suitable for you?

What have you tried? Post your code.

...R