tony1812
Posts: 350
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

serial communication with java

Fri Sep 28, 2018 9:35 pm

Hello, for exercise purpose, I worte a simple code using java to communicate with Arduino.
In the Java code, I use arduino.jar and jserialcomm.jar, I could get java to send out command to arduino, 1 turns on a white led, 2 turns on a red led 3 read a string from arduino.
The first two work just fine, but I am not sure how to read the Arduino return message. I did google but doesn't seem to have any answers.

Arduino code:

Code: Select all

#define LED_w 12
#define LED_r 11
int num;
void setup() {
    pinMode(LED_w, OUTPUT);
    pinMode(LED_r, OUTPUT);
    Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {

    num=Serial.read();
    switch (num) {
      case '1':
          digitalWrite(LED_w, HIGH);
          delay(1000);
          digitalWrite(LED_w, LOW);
          delay(1000);
        break;

        case '2':
          digitalWrite(LED_r, HIGH);
          delay(1000);
          digitalWrite(LED_r, LOW);
          delay(1000);
        break;

        case '3':
        // return a message from arduino to Mac
          Serial.println("Hey Mac, I can hear you.");
        break;
    }
  }
}
Java code:

Code: Select all

package javaserial;

import arduino.*;
import java.util.Scanner;

public class JavaSerial {

/**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        int i=0;
        String duino_data;
        
        Scanner input = new Scanner( System.in );
        
        String num;
        System.out.print("Enter a number 1-3: ");
        num = input.next( );
        
        Arduino  a = new Arduino();
        a.setPortDescription("/dev/cu.usbmodem1D1330");
        a.openConnection();
                
        
        //while(i<10) {
            try {
              Thread.sleep(2000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }           
            System.out.println(num);    
            a.serialWrite(num);
            //i++;
            
        //} 
        a.closeConnection();
    }
}

Andyroo
Posts: 438
Joined: Sat Jun 16, 2018 12:49 am
Location: Side of the hill in Lincolnshire

Re: serial communication with java

Sat Sep 29, 2018 1:01 pm

I would have thought using the InputStream is the simplest though you may have to catch any exceptions for commas errors.

The Java example is at https://github.com/Fazecast/jSerialComm ... ge-Example
Need Pi spray - these things are breeding in my house...

tony1812
Posts: 350
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: serial communication with java

Sat Sep 29, 2018 4:35 pm

Thanks for the reply. Yes, I did read the link you sent, but I failed to relay it to these two .jars Arduino.jar and jserialcomm.jar.
In the Java code since I am using an arduino object -- Arduino a = new Arduino(); I wrote a.serialWrite(num) to send a number to Arduino, shouldn't there be a corresponding a.serialRead() method or some other name to receive data? and if there is indeed such a method, what is the syntax and how would I use it?

Andyroo
Posts: 438
Joined: Sat Jun 16, 2018 12:49 am
Location: Side of the hill in Lincolnshire

Re: serial communication with java

Sat Sep 29, 2018 5:03 pm

Did the Arduino jar come from https://sourceforge.net/p/javaarduinolibrary/?

If so it looks like all the methods are at https://sourceforge.net/p/javaarduinoli ... tructions/
String serialRead() - runs until there is no more data available in the serial to be read. then returns all of the data as a string. this may be an infinite loop depending on availability of data
String serialRead(int limit) - returns a string containing as many readings as the value of limit. recommended for reading
My concern is that neither of these seem to give a time out option - you may never come back from a read if no data is being sent to the Pi :roll:

If that’s not the source - where did you get it from :?: :D
Need Pi spray - these things are breeding in my house...

tony1812
Posts: 350
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: serial communication with java

Sat Sep 29, 2018 6:15 pm

Haha, I read so many tutorials I fogot where they come from. But definitely not the you links you sent. Thank you for that.I have been trying to look into the methods in Adriuno .jar, that really helps. :)

Return to “Java”