简体   繁体   中英

jSerialComm Serial communication with Arduino UNO fails

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM