Page 1 of 1

serial communication with java

Posted: Fri Sep 28, 2018 9:35 pm
by tony1812
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);

void loop() {
  if (Serial.available()) {;
    switch (num) {
      case '1':
          digitalWrite(LED_w, HIGH);
          digitalWrite(LED_w, LOW);

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

        case '3':
        // return a message from arduino to Mac
          Serial.println("Hey Mac, I can hear you.");
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( );
        String num;
        System.out.print("Enter a number 1-3: ");
        num = );
        Arduino  a = new Arduino();
        //while(i<10) {
            try {
            } catch(InterruptedException e) {

Re: serial communication with java

Posted: Sat Sep 29, 2018 1:01 pm
by Andyroo
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 ... ge-Example

Re: serial communication with java

Posted: Sat Sep 29, 2018 4:35 pm
by tony1812
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?

Re: serial communication with java

Posted: Sat Sep 29, 2018 5:03 pm
by Andyroo
Did the Arduino jar come from

If so it looks like all the methods are at ... 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

Re: serial communication with java

Posted: Sat Sep 29, 2018 6:15 pm
by tony1812
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. :)