Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know the basics of Arduino serial communications and connection with a Java application in Windows.

Can anybody provide me with a sample Java program that upon running just makes an Arduino pin high as well as the corresponding Arduino sketch, and some explanation on how the Arduino communicates with the Java app?

I am now starting with Arduino, and I have some experience with Java

I found this code on the net - will this work?

Java code

package arduino;

import jssc.SerialPort;
import jssc.SerialPortException;

public class Arduino {

public static void main(String[] args) throws InterruptedException {

    SerialPort serialPort = new SerialPort("COM3");
    try {
        System.out.println("Port opened: " + serialPort.openPort());
        System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
        System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("o".getBytes()));
        Thread.sleep(1000);
        System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("f".getBytes()));
        System.out.println("Port closed: " + serialPort.closePort());
    } catch (SerialPortException ex) {
        System.out.println(ex);
    }
  }
}

Arduino

  void setup()   {                
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8,HIGH);
  delay(500);
  digitalWrite(8,LOW);
  }

  int incomingByte = 0;

  void loop()                     
  {
 // send data only when you receive data:
    if (Serial.available() > 0) {
           int text = Serial.read();
           Serial.println(text);
           if (text == 'o') {

            digitalWrite(8,HIGH);

           }

           if (text = 'f') {

            digitalWrite(8,LOW);

           }
     }

  }
share|improve this question
    
One word: Firmata. Google it. Try it. – Majenko Mar 25 at 16:03
up vote 0 down vote accepted

I managed to finish my project and here is the code

Useful information:

To prevent reset on serial port open and close

https://tushev.org/articles/arduino/22 /* Add a 10-100 μF capacitor between reset and GND

 JAVA

 /* FIRST IMPORT JSSC LIBRARY */

package arduino;

import java.util.Scanner;
import jssc.SerialPort;
import jssc.SerialPortException;

public class Arduino {

public static void main(String[] args) throws InterruptedException,     SerialPortException {

    Scanner scan = new Scanner(System.in);
    int nummer = 0;
 SerialPort serialPort = new SerialPort("COM4");


        while (true) {
            menu();
             if (scan.hasNextInt()) {
            nummer = scan.nextInt();
        } else {
            System.out.println("verkeerde ingave");
        }
        switch (nummer) {
            case 0:
                serialPort.openPort();//Open serial port
        Thread.sleep(1000);
        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
                break;
            case 1:
                 serialPort.openPort();//Open serial port
        Thread.sleep(1000);
        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("b".getBytes());//Write data to port
         serialPort.writeBytes("c".getBytes());//Write data to port
           serialPort.closePort();//Close serial port
                break;

                 case 2:
                serialPort.openPort();//Open serial port
        Thread.sleep(1000);
        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("d".getBytes());//Write data to port
           serialPort.closePort();//Close serial port
                break;
                 case 3:
                serialPort.openPort();//Open serial port
        Thread.sleep(1000);
        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("e".getBytes());//Write data to port
           serialPort.closePort();//Close serial port
                break;
        }


}
}

public static void menu() {
    final String ANSI_CLS = "\u001b[2J";
    final String ANSI_HOME = "\u001b[H";
    System.out.print(ANSI_CLS + ANSI_HOME);
    System.out.flush();
    System.out.println("");  
    System.out.println("    ************************************************************************************");
    System.out.println("    *      ***       ********   *******   **    **  *****  **      **      ******      *");
    System.out.println("    *     ** **      **    **   **    **  **    **   ***   ****    **    ***    ***    *");
    System.out.println("    *    **   **     *** ****   **     ** **    **   ***   ** **   **  **          **  *");
    System.out.println("    *   *********    **   **    **     ** **    **   ***   **  **  **  **          **  *");
    System.out.println("    *  **       **   **    **   **    **  **    **   ***   **   ** **    ***    ***    *");
    System.out.println("    * **         **  **     **  *******     ****    *****  **    ****      ******      *");
    System.out.println("    ************************************************************************************");
    System.out.println("");
    System.out.println("    By Emmanouil Perselis");
    System.out.println("");
    System.out.println("What would you like to do?");
    System.out.println("0.---");
    System.out.println("1.Toggle lights");
    System.out.println("2.Toggle air");
    System.out.println("3.---");
    System.out.println("4.---");

}
 }

ARDUINO SCETCH

 int led = 13;
 int led1 = 2;
 int led2 = 4;
 int led3 = 7;
 int led4 = 8;
 void setup() {   
   Serial.begin(9600);  
   pinMode(led, OUTPUT);   
   pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 
    pinMode(led3, OUTPUT); 
    pinMode(led4, OUTPUT);
 }

 void loop() {
   char i= Serial.read();

   if(i=='a'){////////////////
     if(digitalRead(led)){////////////
   digitalWrite(led, LOW);  ////////////
  }else if(!digitalRead(led))//////////
   digitalWrite(led, HIGH);  //////////
 }

  if(i=='b'){////////////////
     if(digitalRead(led1)){////////////
   digitalWrite(led1, LOW);  ////////////
  }else if(!digitalRead(led1))//////////
   digitalWrite(led1, HIGH);  //////////
 }


 if(i=='c'){////////////////
     if(digitalRead(led2)){////////////
   digitalWrite(led2, LOW);  ////////////
  }else if(!digitalRead(led2))//////////
   digitalWrite(led2, HIGH);  //////////
 }

 if(i=='d'){////////////////
if(digitalRead(led3)){////////////
   digitalWrite(led3, LOW);  ////////////
  }else if(!digitalRead(led3))//////////
   digitalWrite(led3, HIGH);  //////////
 }


 if(i=='e'){////////////////
if(digitalRead(led4)){////////////
   digitalWrite(led4, LOW);  ////////////
  }else if(!digitalRead(led4))//////////
   digitalWrite(led4, HIGH);  //////////
 }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.