MenarNinja / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

EventThread is never terminated #72

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I'm opening a port and listening to it. AddEventListener and so forth.
2. Then i use closePort or even removeEventListener() and then closePort()
3. The port is closed, but the EventThread for the specific port is never 
terminated.

What is the expected output? What do you see instead?

The Thread should terminate, according to the JavaDoc description, but it never 
does. I can't end my programs with hooks because this one thread never 
terminates.

What version of the product are you using? On what operating system?

I'm using jssc 2.8 on a RPi with Linux raspberrypi 3.12.22+

I'm listening to an arduino via the USB/serial interface. It sends around 20 
bytes every 20 ms with 115200 8N1

Please provide any additional information below.

Opening port and adding eventlistener:

    public final void startup() throws Exception {
        msgStart = MSG_START;
        msgEnd = MSG_END;
        msgLength = MSG_LENGTH;
        ardSerialPort = new SerialPort(PORT);
        ardSerialPort.openPort();
        ardSerialPort.setParams(BAUDRATE, DATABITS, STOPBITS, PARITY, RTS, CTS);
        int mask = SerialPort.MASK_RXCHAR;
        ardSerialPort.setEventsMask(mask);
        ardSerialPort.addEventListener(this);
        LOGGER.log(Level.INFO, "Serial Port opened: {0}", PORT);
    }

The serial Event:

    public final void serialEvent(final SerialPortEvent serialPortEvent) {
        if (serialPortEvent.isRXCHAR()) {
            while (serialPortEvent.getEventValue() > 0) {
                LOGGER.log(Level.FINEST, "byte received");
                try {
                    byte c = ardSerialPort.readBytes(1)[0];
                    serialReceiverQueue.put(c);
                } catch (InterruptedException e) {
                    LOGGER.log(Level.WARNING, "BlockingQueue was interrupted: {0}", e);
                } catch (SerialPortException e) {
                    LOGGER.log(Level.WARNING, "Byte could not be read from port. {0}", e);
                }
            }
        }
    }

Closing the port: here the EventThread should terminate, but it never does.

    public final void teardown() throws Exception {
        //boolean removed = ardSerialPort.removeEventListener();
        ardSerialPort.closePort();
        LOGGER.log(Level.INFO, "EventListener removed and Port {0} closed!", PORT);
    }

Code Snippet from JSSC 2.8:

    public boolean closePort() throws SerialPortException {
        checkPortOpened("closePort()");
        if(eventListenerAdded){
            removeEventListener();
        }
        boolean returnValue = serialInterface.closePort(portHandle);
        if(returnValue){
            maskAssigned = false;
            portOpened = false;
        }
        return returnValue;
    }

    public boolean removeEventListener() throws SerialPortException {
        checkPortOpened("removeEventListener()");
        if(!eventListenerAdded){
            throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER);
        }
        eventThread.terminateThread();
        setEventsMask(0);
        if(Thread.currentThread().getId() != eventThread.getId()){
            if(eventThread.isAlive()){
                try {
                    eventThread.join(5000);
                }
                catch (InterruptedException ex) {
                    throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_LISTENER_THREAD_INTERRUPTED);
                }
            }
        }
        methodErrorOccurred = null;
        eventListenerAdded = false;
        return true;
    }

********************************
Am i doing something wrong or is this a bug?

Original issue reported on code.google.com by maximili...@gmx.at on 25 Sep 2014 at 4:04