MeetPi
Posts: 9
Joined: Tue Nov 27, 2018 1:23 pm

Serial Communication (Javafx + JSSC)

Fri Mar 15, 2019 10:56 am

Hello!

I'm trying to establish a serial communication between a Raspberry Pi 3B+ (with 2018-11-13-raspbian-stretch version) and an eval. board (Atmel SAMC21 Xplained Pro).

Both boards are physically USB cable connected.

The eval board holds data that is supposed to be transmitted continuosly to the Raspberry Pi 3B+. (infinite loop).

The Raspberry Pi 3B+ on the other end is running a (Javafx + JSSC) UI program that receives the data stream from the eval board. The incoming data is to be displayed, thus updating the displayed data.

But here is my problem:
At (Javafx) programm start data is received, but only once! Although the eval board (seems) to send data continuosly.

My Raspberry Pi seems not to fetch/receive the data. So the data on display is not updated.
(Data streams in infinitely, as it should, when I fireup minicom on my Raspberry Pi terminal, with the eval board connected to the Raspberry Pi)

Since I'm relatively new to this, so I understand that the fault can be in my code. But there is also a chance that something is missing in my Raspberry Pi setup.

So my question, before I post any code:
Do I need to install some kind of software on my Raspberry Pi in order to be able to establish a (stabile/reliable) serial communication between the two boards? If "yes", then I would be glad to know which ones (and perphaps) how to get them installed.

Thank you very much in advance for your time and for your help!

naveen999
Posts: 1
Joined: Thu Mar 28, 2019 10:45 am

Re: Serial Communication (Javafx + JSSC)

Thu Mar 28, 2019 10:57 am

Need some help with Javafx + JSSC.

I'm trying to establish a serial communication between a Raspberry Pi 3B+ and an eval. board (Atmel SAMC21 Xplained Pro).

Here is a symbolic representation of my hardware setup:

Raspberry Pi 3B+ with Javafx program to display incoming data [USB]<---------->[USB] eval. board with data

(My Raspberry Pi operating under 2018-11-13-raspbian-stretch version)

Using Javafx (Eclipse Version: 2018-09 (4.9.0))and JSSC (Version 2.7.0) I've written a routine that fetches (or should continuosly fetch) data from the eval. board and display it on a UI screen that connected to the Raspberry Pi. (The Javafx UI program runs on the Raspberry Pi.)

These are the problems I'm facing:

1) At (Javafx)program start, data is sent but once to the Raspberry Pi. The displayed data is not updated, although the eval. board keeps sending data.

2) The buffer is not cleared for new data. So often only part of the data is displayed statically, (meaning incoming data is not displayed).

Have tried a few things/suggestions to solve the problem without any success.

When I fireup minicom on the Raspberry Pi terminal, the data streams in just as expected.

Anyone has an idea what I'm doing wrong? (I'm a rookie in coding).

Every nudge into the right direction would be very much appreciated!

https://www.besanttechnologies.com/trai ... in-chennai

https://www.besanttechnologies.com/trai ... -bangalore

https://www.besanttechnologies.com/trai ... in-chennai

User avatar
Aptron_Noida
Posts: 1
Joined: Tue Jun 11, 2019 7:44 am
Location: Noida, India
Contact: Website Facebook Twitter

Re: Serial Communication (Javafx + JSSC)

Tue Jun 11, 2019 7:54 am

It's a series of examples to program Java + JavaFX + jSSC(java-simple-serial-connector) on Windows 10 with NetBeans IDE. Run on Windows 10, to communicate with Arduino Board. And finally show how to remote deploy on Raspberry Pi using NetBeans IDE Remote Java SE Platform.

JavaFX + java-simple-serial-connector + Arduino

This example implement a simple Java application using java-simple-serial-connector library (jSSC) , with JavaFX user interface, send bytes to Arduino Esplora via USB.

Code: Select all

package javafx_jssc;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;

public class JavaFX_jSSC extends Application {
    
    ObservableList<String> portList;
    
    private void detectPort(){
        
        portList = FXCollections.observableArrayList();

        String[] serialPortNames = SerialPortList.getPortNames();
        for(String name: serialPortNames){
            System.out.println(name);
            portList.add(name);
        }
    }
    
    @Override
    public void start(Stage primaryStage) {
        
        detectPort();

        final ComboBox comboBoxPorts = new ComboBox(portList);
        final TextField textFieldOut = new TextField();
        Button btnSend = new Button("Send");
        
        btnSend.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {

                if(comboBoxPorts.getValue() != null && 
                    !comboBoxPorts.getValue().toString().isEmpty()){
                    
                    String stringOut = textFieldOut.getText();
                    
                    try {
                        SerialPort serialPort = 
                            new SerialPort(comboBoxPorts.getValue().toString());                        
                        
                        serialPort.openPort();
                        serialPort.setParams(
                                SerialPort.BAUDRATE_9600,
                                SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                        serialPort.writeBytes(stringOut.getBytes());
                        serialPort.closePort();
                        
                    } catch (SerialPortException ex) {
                        Logger.getLogger(
                            JavaFX_jSSC.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }else{
                    System.out.println("No SerialPort selected!");
                }
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(
                comboBoxPorts, 
                textFieldOut, 
                btnSend);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}


Arduino code in Esplora side (same as in the post "Serial communication between Arduino Esplora and PC").
#include <Esplora.h>
#include <TFT.h>
#include <SPI.h>

int prevSw1 = HIGH;
int incomingByte = 0;
String charsIn = "";
char printout[20];  //max char to print: 20
 
void setup() {
  
    EsploraTFT.begin();  
    EsploraTFT.background(0,0,0);
    EsploraTFT.stroke(255,255,255);  //preset stroke color
     
    //Setup Serial Port with baud rate of 9600
    Serial.begin(9600);
    
    //indicate start
    Esplora.writeRGB(255, 255, 255);
    delay(250);
    Esplora.writeRGB(0, 0, 0);
    
}
 
void loop() {
    int sw1 = Esplora.readButton(SWITCH_1);
    if(sw1 != prevSw1){
      if(sw1 == LOW){
        Serial.println("Hello from Arduino Esplora");
      }
      prevSw1 = sw1;
    }
    
    while (Serial.available()) {
      char charRead = Serial.read();
      charsIn.concat(charRead);
    }
    if(charsIn != ""){
      Serial.println("How are you, " + charsIn);
      charsIn.toCharArray(printout, 21);
      EsploraTFT.background(0,0,0);
      EsploraTFT.text(printout, 0, 10);
      charsIn = "";
    }
}
More Information is available: http://aptronnoida.in/best-java-training-in-noida.html
http://aptronnoida.in

NaviRaj
Posts: 2
Joined: Tue Jun 18, 2019 12:34 pm

Re: Serial Communication (Javafx + JSSC)

Tue Jun 18, 2019 12:37 pm

I am working on a simple fingerprint scanner that is connected to my Arduino Uno. I have connected my circuit via usb to my computer, so that I can read serially the data that Arduino is processing for the fingerprint scanner. I am in the process of making a simple UI for the project using JavaFX. Thus far, I have made a few stages.

The first is the home stage which has a single button on it. Basically, clicking this button opens the serial port and listens for 1 of 2 things, the enroll/verify mode (physical buttons on circuit, which after pressed send the strings "Enrollment" or "Verification" to the Serial). After pressing the enrollment, a new stage opens up and since the Arduino is already prepped to enroll the print, I don't have to read the Serial again. After scanning the print, I fill out a form, and hit a submit button which takes the fingerprint id that was created by the fingerprint scanner and links it with one of the fields in the form (behind the scenes). Then the stage closes, and I begin to re-read the Serial.

This is where the problem begins.

I am looking for which window to open, but how can I open the window (depending on which physical button is pressed on circuit) first, then re-read the Serial for whether the user was verified, and then change things on the window?

It may seem complicated, I am aware, which is why some of the code is below.

submitForm.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent e)
{
if(firstNameTextField.getText().length()>0 &&
lastNameTextField.getText().length()>0 &&
gpnTextField.getText().length() == 8)
{
enrollStage.hide();
try {
//System.out.println("winfwnif ");
if(readEnroll(sp)==true)
{
id++;
}
Employee employee = new Employee(Long.parseLong(gpnTextField.getText()), id);
pw = new PrintWriter(file);
pw.println(employee.getFP() + "," + employee.getGPN());
pw.close();
} catch (FileNotFoundException | SerialPortException | InterruptedException e2) {

e2.printStackTrace();
}
firstNameTextField.setText("");
lastNameTextField.setText("");
gpnTextField.setText("");
enrollStage.hide();
home.show();
try
{
home.close();
int mod = checkMode(sp);
if(mod == 1)
{
enrollStage.show();
}
else if(mod == 2)
{

boolean f = false;
//Thread.sleep(2000);
synchronized(verificationStage)
{
verificationStage.show();
Thread.sleep(2000);
f = readVerify(sp);

}
if(f)
{
userImageIV.setImage(new Image("/images/temp.png", 250, 400, true, false));
thumbIV.setImage(new Image("/images/greenThumb.png", 150, 300, true, false));
}

}
}
catch(SerialPortException | InterruptedException err)
{

}
}
}
});
This is the function I use to read the verification output from the Arduino.

public boolean readVerify(SerialPort s) throws SerialPortException, InterruptedException
{
while (true)
{
if (sp.getInputBufferBytesCount() > 0)
{ //check size of buffer
String x = s.readString();
System.out.println(x);
if(x.contains("Verified"))
{
return true;
}
}
Thread.sleep(100);
}

}

NaviRaj
Posts: 2
Joined: Tue Jun 18, 2019 12:34 pm

Re: Serial Communication (Javafx + JSSC)

Tue Jun 18, 2019 12:40 pm

I am working on a simple fingerprint scanner that is connected to my Arduino Uno. I have connected my circuit via usb to my computer, so that I can read serially the data that Arduino is processing for the fingerprint scanner. I am in the process of making a simple UI for the project using JavaFX. Thus far, I have made a few stages.

The first is the home stage which has a single button on it. Basically, clicking this button opens the serial port and listens for 1 of 2 things, the enroll/verify mode (physical buttons on circuit, which after pressed send the strings "Enrollment" or "Verification" to the Serial). After pressing the enrollment, a new stage opens up and since the Arduino is already prepped to enroll the print, I don't have to read the Serial again. After scanning the print, I fill out a form, and hit a submit button which takes the fingerprint id that was created by the fingerprint scanner and links it with one of the fields in the form (behind the scenes). Then the stage closes, and I begin to re-read the Serial.

This is where the problem begins.

I am looking for which window to open, but how can I open the window (depending on which physical button is pressed on circuit) first, then re-read the Serial for whether the user was verified, and then change things on the window?

It may seem complicated, I am aware, which is why some of the code is below.

Code: Select all

submitForm.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent e) 
        {
            if(firstNameTextField.getText().length()>0 && 
               lastNameTextField.getText().length()>0 && 
               gpnTextField.getText().length() == 8)
            {
                enrollStage.hide();
                try {
                    //System.out.println("winfwnif ");
                    if(readEnroll(sp)==true)
                    {
                        id++;
                    }
                    Employee employee = new Employee(Long.parseLong(gpnTextField.getText()), id);
                    pw = new PrintWriter(file);
                    pw.println(employee.getFP() + "," + employee.getGPN());
                    pw.close();
                } catch (FileNotFoundException | SerialPortException | InterruptedException e2) {

                    e2.printStackTrace();
                }   
                firstNameTextField.setText("");
                lastNameTextField.setText("");
                gpnTextField.setText("");
                enrollStage.hide();
                home.show();
                try
                {
                    home.close();
                    int mod = checkMode(sp);
                    if(mod == 1)
                    {
                        enrollStage.show();
                    }
                    else if(mod == 2)
                    {

                        boolean f = false;
                        //Thread.sleep(2000);
                        synchronized(verificationStage)
                        {
                            verificationStage.show();
                            Thread.sleep(2000);
                            f = readVerify(sp);

                        }
                        if(f)
                        {
                            userImageIV.setImage(new Image("/images/temp.png", 250, 400, true, false));
                            thumbIV.setImage(new Image("/images/greenThumb.png", 150, 300, true, false));
                        }

                    }
                }
                catch(SerialPortException | InterruptedException err)
                {

                }
            }
        }
    });
https://www.zuaneducation.com/digital-m ... ng-courses
https://www.zuaneducation.com/big-data-training-chennai
https://www.zuaneducation.com/data-scie ... ng-chennai

Return to “Java”