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!
Here is an excerpt of my code:
public class RxTxSerialData
{
private static SerialPort serialPort;
private StringProperty serialData = new SimpleStringProperty();
Label dataFromController = new Label();
// setter and getter for serial data
public final String getSerialData()
{
return serialData.get();
}
public final void setSerialData(String srlData)
{
serialData.set(srlData);
}
public final StringProperty serialDataProperty()
{
return serialData;
}
//*** method for receiving data from ยต-controller
public void receiveSerialDataFromController()
{
SerialPort serialPort = new SerialPort("/dev/ttyACM0");
String stringFromController = dataFromController.getText();
try
{
if (serialPort != null && serialPort.isOpened())
{
serialPort.purgePort(4); //tx_clear
serialPort.purgePort(8); //rx_clear
serialPort.closePort();
}
serialPort.openPort();
serialPort.purgePort(4); //tx_clear
serialPort.purgePort(8); //rx_clear
try {
Thread.sleep(3000);
}catch(InterruptedException ie)
{
Logger.getLogger(RxTxSerialData.class.getName()).log(Level.SEVERE, null, ie);
}
serialPort.setParams
(
SerialPort.BAUDRATE_115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE
);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
PortReader portReader = new PortReader(serialPort);
serialPort.addEventListener(portReader, SerialPort.MASK_RXCHAR);
serialPort.writeBytes(stringFromController.getBytes());
}catch(SerialPortException se)
{
Logger.getLogger(RxTxSerialData.class.getName()).log(Level.SEVERE, null, se);
}
}
private class PortReader implements SerialPortEventListener
{
SerialPort serialPort;
public PortReader(SerialPort serialPort)
{
this.serialPort = serialPort;
}
@Override
public void serialEvent(SerialPortEvent srlEvent)
{
if(srlEvent.isRXCHAR() && srlEvent.getEventValue() > 0)
{
try
{
String buffer = serialPort.readString(srlEvent.getEventValue());
setSerialData(buffer);
Platform.runLater(new Runnable() {
@Override
public void run() {
setSerialData(buffer);
}
});
}catch (SerialPortException ex)
{
System.out.println("Error" + ex);
}
}
}
}
}