Go Down

Topic: jSerialComm Serial communication with Arduino UNO fails (Read 514 times) previous topic - next topic

Brianna1

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:

Code: [Select]
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:

Code: [Select]
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 experience? The new code:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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.

PaulS

Opening the serial port resets the Arduino. The Serial Monitor app doesn't allow you to type data and send it to the Arduino before the reset is complete. Your code DOES send data before the Arduino finishes resetting.

Try making your app be patient, and give the Arduino time to reset before you start sending it data. How long? Well, the best thing to do is have the Arduino send something that indicates that it is ready, before you start sending data. Then, the answer is "Until the Arduino says Ready!".
The art of getting good answers lies in asking good questions.

Coding Badly


@Brianna1 has been vanquished by Greppy the Autoban Bot.


Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

Coding Badly

#4
May 01, 2019, 08:06 pm Last Edit: May 01, 2019, 08:08 pm by Coding Badly

@Brianna1's second post contained spam.  The forum's bot automatically blocked the second post and banned @Brianna1.

The current spammer strategy goes something like this: Given the behaviour, @Brianna1 is very likely human.  They find a very old post, possibly rework it, then post it anew.  The unsuspecting victim (@PaulS) replies turning the thread into something that appears innocuous.  The spammer follows-up ("Thanks! Problem solved!"; "I don't understand. Please continue.") with a post that contains a poorly hidden spammy link.

The variation is to not include the link right away but wait (sometimes months) then edit the second post to include the link.

"Greppy" is a twist on the method used to identify the spammy link.  The method uses regular expressions.  grep is the often used command line tool for regular expression pattern matching.

Autoban Bot is just that.  The robot that automatically purges and bans.

So, "Greppy the Autoban Bot" is our faithful robot that uses regular expression pattern matching (like "grep") to automatically ban.


Robin2

@Brianna1's second post contained spam.
If it was genuine SPAM then that's fine. I was concerned that it may have mistakenly thought it was spam - which would have been very unfair to the OP.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Coding Badly


I appreciate your concern.  I share it.  We do occasionally have false-positives.  For example, "streaming" is a tough keyword.

In this case, @Brianna1 was trying to sell us low quality fried chicken.  And, if they are genuinely not a spammer, they can send a personal message to any of the moderators for reconsideration.


Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

pert

Here's the original, legitimate question that the spammer copied:
https://stackoverflow.com/questions/52822793/jserialcomm-serial-communication-with-arduino-uno-fails

Although I'm often feel like the sheer incompetence of many spammers makes them even more annoying, this more intelligent approach to spam is especially vile, since it can result in well-meaning people wasting a lot of time trying to provide assistance to a seemingly valid question.

PaulS

Quote
And, if they are genuinely not a spammer, they can send a personal message to any of the moderators for reconsideration.
How do they do that, when they've been banned?

Edit: speeling is not my strong suit.
The art of getting good answers lies in asking good questions.

Coding Badly

How do they do that, when they've been banned?
There is banned and there is "banned".

We no longer have the better version.


Go Up