0
votes

I'm working on a little project where I want to communicate with an Arduino UNO via the Serial Interface. To get used to the library (I'm using jSerialComm) I tried to write a simple example but even this simple program does not work as expected. The Arduino runs a simple sketch, that simply returns the value sent by the computer:

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

}

void loop() {
  if(Serial.available())
  Serial.println(Serial.read());
}

It works just fine when I send values to it via the built in serial monitor.

My Java code seems to be the problem but I cannot figure it out:

public static void main(String[] args){
    SerialPort port = SerialPort.getCommPort("COM5");
    port.setComPortParameters(9600,8,1,0);
    port.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING,0,0);
    System.out.println("Open port: " + port.openPort());
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Scanner in = new Scanner(port.getInputStream());
    PrintWriter out = new PrintWriter(port.getOutputStream(),true);

   out.println('a');
   out.flush();
    System.out.println("> w");

    while (in.hasNextLine())
        System.out.println("return: " +in.nextLine());
}

I would appreciate every kind of help. Thank's for every reply.

EDIT:

After playing around with the code I ended up getting an response after quite a few requests. Does anybody have an idea how to resolve this? The new code:

package sample;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;

import java.io.PrintWriter;
import java.util.Scanner;
public class Test {
    static boolean received;
    public static void main(String[] args) {
        SerialPort port = SerialPort.getCommPort("COM5");
        port.setComPortParameters(9600,8,1,0);
        port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER,0,0);
        System.out.println("Open port: " + port.openPort());
        Scanner in = new Scanner(port.getInputStream());
        PrintWriter out = new PrintWriter(port.getOutputStream(),true);
        port.addDataListener(new SerialPortDataListener() {
            @Override
            public int getListeningEvents() {
                return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
            }

            @Override
            public void serialEvent(SerialPortEvent serialPortEvent) {
                String input = "";

               input = in.nextLine();

                System.out.println("return: " + input);
                received=true;
            }
        });


int counter =0;
        while(!received) {
            System.out.println(counter);
            out.println(counter);
            out.flush();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            counter++;
        }
        out.println('w');
        System.out.println("w");
           /*     String input = in.nextLine();
                System.out.println("return: "+input+input.isEmpty());*/
    }
}

The code on the Arduino:

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

}
byte in;
int count=0;
void loop() {
  if(Serial.available()){
    Serial.print(Serial.parseInt());
    Serial.print('\n');
  }
}

The resulting console output:

Open port: true 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
w
return: 25
return: 0

This code should immediatly return the value sent by the computer but ist doesn't. It works just fine when I use the serial monitor built-in to the Arduino IDE and also with every other serial monitor I tried.

1
I use Javino. Javino is a middleware that provides a communication between Java and Arduino. Is easy and the documentation is well explained. sourceforge.net/projects/javinoFabian Brandão
Thank's for that hint, but I want my project to be platform independant, without any dependecies despite java. For Javino I would need to install python.SF23
You can improve your question a lot by explaining exactly what is expected, and what goes wrong. At the moment you just say "does not work as expected", but that's not much detail.jfowkes
I want to send a simple character to the Arduino and retrieve the values of some variables. The actual test code does nothing else than sendig every received data back immediatly but, as you can see from the console output, I don't get any data back the first times I send something.SF23

1 Answers

0
votes

I solved the problem using a while loop to send the request until I receive the desired response. The whole project can be found on GitHub: https://github.com/SF2311/ArduinoUI