All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gnu.io.factory.DefaultSerialPortFactory Maven / Gradle / Ivy

Go to download

A fork of the RXTX library with a focus on ease of use and embeddability in other libraries.

There is a newer version: 5.2.1
Show newest version
package gnu.io.factory;

import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

public class DefaultSerialPortFactory implements SerialPortFactory {

	private SerialPortRegistry portRegistry;
	
	/**
	 * Constructor
	 */
	public DefaultSerialPortFactory() {
		this.portRegistry = new SerialPortRegistry();
	}
	
	/* (non-Javadoc)
	 * @see gnu.io.factory.SerialPortFactory#createSerialPort(java.lang.String)
	 */
	@Override
	public  T createSerialPort(String portName, Class expectedClass) throws PortInUseException, NoSuchPortException, UnsupportedCommOperationException{
		SerialPortCreator portCreator = this.portRegistry.getPortCreatorForPortName(portName, expectedClass);
		if(portCreator != null) {
			return portCreator.createPort(portName);
		}
		throw new NoSuchPortException(portName + " can not be opened.");
	}
	
	@Override
	public SerialPort createSerialPort(String portName)
			throws PortInUseException, NoSuchPortException, UnsupportedCommOperationException {
		SerialPortCreator portCreator = this.portRegistry.getPortCreatorForPortName(portName, SerialPort.class);
		if(portCreator != null) {
			return portCreator.createPort(portName);
		}
		throw new NoSuchPortException(portName + " can not be opened.");
	}
	
	/**
	 * Gets the {@link SerialPortRegistry} to register/unregister {@link SerialPortCreator}s.
	 * @return
	 */
	public SerialPortRegistry getPortRegistry() {
		return portRegistry;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy