Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two SerialPort instances for the same port name #47

Closed
GoogleCodeExporter opened this issue Dec 20, 2015 · 3 comments
Closed

Two SerialPort instances for the same port name #47

GoogleCodeExporter opened this issue Dec 20, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

I have 2 eclipse views (ViewPart), one displays sensor data and the other 
displays images.  Can each of view create a SerialPort instance and listen to 
the same port?

View#1 -> new SerialPort("/dev/tty.serial-rn42-RNI-SPP");
View#1 -> new SerialPort("/dev/tty.serial-rn42-RNI-SPP");

Original issue reported on code.google.com by cagiva...@gmail.com on 30 Aug 2013 at 1:20

@GoogleCodeExporter
Copy link
Author

By default jSSC use TIOCEXCL for exclusive access to serial port resource, in 
this case you can't open same port twice (except root, or you can disable 
TIOCEXCL by setting JSSC_NO_TIOCEXCL JVM property). But it's really bad 
practice, you can not get the same data from one serial port by using 2 new 
SerialPort objects. If one object read data from serial port buffer readed data 
is purged from buffer.

You should create some serial port provider for reading data, and providing it 
to any ViewPart you need.

Original comment by scream3r.org@gmail.com on 30 Aug 2013 at 4:49

@GoogleCodeExporter
Copy link
Author

Hi Alexey,

Thanks for confirming that. Below is the initial draft of the potential 
provider that it might help others with similar case scenario.

Thanks again for such wonderful API!


----------------------------------
import java.util.ArrayList;
import java.util.List;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class SerialConnectionProvider {
    private static final SerialConnectionProvider INSTANCE = new SerialConnectionProvider();
    private static List<SerialPortEventListener> consumerListeners;
    private static SerialPort serialPort;

    private SerialConnectionProvider() {
        consumerListeners = new ArrayList<SerialPortEventListener>();
    }

    public static SerialConnectionProvider getInstance(String port, SerialPortEventListener consumerListener) throws SerialPortException {
        if (serialPort == null) {
            serialPort = new SerialPort(port);
            serialPort.openPort();
            serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
            serialPort.addEventListener(new SerialPortListener());  
        }
        consumerListeners.add(consumerListener);

        return SerialConnectionProvider.INSTANCE;
    }

    static class SerialPortListener implements SerialPortEventListener {
        public void serialEvent(SerialPortEvent event) {
            if (event.isRXCHAR() && event.getEventValue() > 10) {
                for (SerialPortEventListener listener : consumerListeners) {
                    listener.serialEvent(event);
                }
            }
        }
    }

    public synchronized boolean disconnect(SerialPortEventListener listener) throws SerialPortException {
        boolean result = true;
        if (serialPort.isOpened() && consumerListeners.size() == 1) {
            result = serialPort.closePort();
        }
        consumerListeners.remove(listener);
        return result;
    }

    public SerialPort getSerialPort() {
        return serialPort;
    }
}

Original comment by cagiva...@gmail.com on 30 Aug 2013 at 5:18

@GoogleCodeExporter
Copy link
Author

Original comment by scream3r.org@gmail.com on 12 Sep 2014 at 2:31

  • Changed state: Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant