1

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);
 }
 }
 }
Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Mar 25, 2016 at 15:40
1
  • 1
    One word: Firmata. Google it. Try it. Commented Mar 25, 2016 at 16:03

2 Answers 2

1

I had this problem with communication with Arduino Uno (I think that the Arduino Uno, and other boards, with this type of scheme, suffer this problem).

The DTR line on Arduino Uno is connected with RESET, including when you use the Arduino IDE Serial Monitor port, such that the parameter of DTR is false. Yeah, you can fix this with capacitor or catch place RESET EN on board. But when you set setting you can just write:

comport3.setParams(9600, 8, 1, SerialPort.PARITY_NONE, false, false); 

The last parameter is DTR and its default is true, the second to last parameter is RTS (it defaults to true too). I set both of these parameters to false and my board reads commands from the computer without resetting.

I write this for other people who had this problem.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
answered Jun 18, 2017 at 19:04
0

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); //////////
 }
}
answered Apr 25, 2016 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.