3 of 6
added 4423 characters in body

How to fix JavaFX rxtxSerial.dll Serial Port Error in Windows 10?

The program runs fine in windows 7, but in windows 10 the program run fine until data is received through the serial port.

Then I get the below error: (I am the using IntelliJ IDE)

In SerialEvent

A fatal error has been detected by the Java Runtime Environment:

 EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180005b00, pid=12340, tid=6016

JRE version: Java(TM) SE Runtime Environment (10.0.1+10) (build 10.0.1+10)
Java VM: Java HotSpot(TM) 64-Bit Server VM (10.0.1+10, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)
Problematic frame:
C  [rxtxSerial.dll+0x5b00]

No core dump will be written. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as:
C:\Users\jochu\IdeaProjects\WITSReader\hs_err_pid12340.log

If you would like to submit a bug report, please visit:
  http://bugreport.java.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.

I have tried several versions of rxtxSerial.dll

At this point I am thinking it is not an issue with my code as much as in issue with a library or maybe even the java SDK 10.0.1.

Update: I also have the same issue in Java SDK 11.0.1 with Javafx SDK 11.0.1

Below is the code I use which causes the error. (only when run on windows 10) rxtxSerial.dll and RXTXcomm.jar need to be linked libraries.

import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

public class Main {

    static CommPortIdentifier[] portId = new CommPortIdentifier[10];
    static Enumeration portList;
    static InputStream inputStream;
    static SerialPort serialPort;
    static String stringBuffer = "";

    public static void main(String[] args) {

        //manually change for comport selection to keep code simple
        int selectedPort = 0;

        // Checking for ports
        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("portList... " + portList);
        int comPortCounter = 0;
        while (portList.hasMoreElements()) {
            portId[comPortCounter] = (CommPortIdentifier) portList.nextElement();
            System.out.println(portId[comPortCounter].getName());
            System.out.println("port identified is Serial.. " + portId[comPortCounter].getPortType());
            comPortCounter++;
            if (comPortCounter > 9){
                comPortCounter = 9;
            }
        }

        // opening the port
        try{
            serialPort = (SerialPort) portId[selectedPort].open("SerialTest",500);
            System.out.println("Serial port open: " + portId[selectedPort].getName());
        } catch (PortInUseException e){
            System.out.println("Port in use error");
            return;
        }

        // initiate listening on the port
        try {
            inputStream = serialPort.getInputStream();
            System.out.println("Input Stream... " + inputStream);
        } catch (IOException e) {
            System.out.println("IO Exception");
            return;
        }

        // create the listener
        try {
            serialPort.addEventListener(new ListenerTest());
        } catch (TooManyListenersException e) {
            System.out.println("Too many Listener exception");
            return;
        }
        serialPort.notifyOnDataAvailable(true);

        // set COM port parameters (default 9600, 8, 1 , NONE)
        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // no handshaking or other flow control
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

            // timer on any read of the serial port
            serialPort.enableReceiveTimeout(500);

            System.out.println("Serial port parameters have been set");
        } catch (UnsupportedCommOperationException e) {
            System.out.println("UnSupported comm operation");
            return;
        }
    }

    //Listener Class for the serial port.
    static public class ListenerTest implements SerialPortEventListener {

        @Override
        public void serialEvent(SerialPortEvent event) {

            switch (event.getEventType()) {
                case SerialPortEvent.DATA_AVAILABLE:

                    System.out.println("In SerialEvent");

                    // Wets Serial Port Read
                    try {
                        while (inputStream.available() > 0) {
                            byte[] byteBuffer = new byte[1024];
                            int length = -1;
                            length = inputStream.read(byteBuffer);
                            stringBuffer += new String(byteBuffer, 0, length);
                        }

                        System.out.println(stringBuffer);

                    } catch (IOException e) {
                        System.out.println("IO Exception in SerialEvent()");
                    }

                    break;
            }
        }
    }
    
}

I am hoping someone may have run into this error before me, and found a solution.

Any help will be very much appreciated!