@PostConstruct public void connect() throws TooManyListenersException, NoSuchPortException { log.info("Connection PostConstruct callback: connecting ..."); serial = new NRSerialPort(serialPortProperties.getPortName(), serialPortProperties.getBaudRate()); serial.connect(); if (serial.isConnected()) { log.info("Connection opened!"); } else { throw new RuntimeException("Is not possible to open connection in " + serialPortProperties.getPortName() + " port"); } serial.addEventListener(this); serial.notifyOnDataAvailable(true); input = new BufferedReader(new InputStreamReader(serial.getInputStream())); }
@Override public void initialize() { portName = (String) getThing().getConfiguration().get("port"); if (portName != null) { serialPort = new NRSerialPort(portName, BAUD); if (serialPort.connect()) { updateStatus(ThingStatus.ONLINE); output = new OutputStreamWriter(serialPort.getOutputStream()); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Failed to connect to serial port " + portName); logger.debug("Failed to connect to serial port {}", portName); } } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Serial port name not configured"); logger.debug("Serial port name not configured"); } }
@Override protected void openConnection() throws IOException { if (isPortNameExist(portName)) { serialPort = new NRSerialPort(portName, LINK_SPEED); boolean isConnected = serialPort.connect(); if (!isConnected) { throw new IOException("Failed to connect on port " + portName); } logger.debug("Connected to {}", getConnectionName()); } else { throw new IOException("Serial port with name " + portName + " does not exist. Available port names: " + NRSerialPort.getAvailableSerialPorts()); } }
private boolean serialConnect() { logger.debug("Running connection setup"); try { logger.debug("Setting up socket connection to " + this.serialDevice); this.serialPort = new NRSerialPort(this.serialDevice, this.serialSpeed); this.serialPort.connect(); this.projectorReader = new BufferedReader(new InputStreamReader(this.serialPort.getInputStream())); this.projectorWriter = new PrintWriter(this.serialPort.getOutputStream(), true); logger.debug("Serial connection setup successfully!"); return true; } catch (Exception e) { logger.error("Unable to connect to device: " + this.serialDevice, e); } return false; }
@Override public void connect() throws IOException { if (isPortNameExist(portName)) { serialPort = new NRSerialPort(portName, baudRate); if (serialPort.connect() == false) { throw new IOException("Failed to connect on port " + portName); } logger.debug("Connected to {}", portName); } else { throw new IOException("Serial port with name " + portName + " does not exist. Available port names: " + NRSerialPort.getAvailableSerialPorts()); } }
private SerialPortGateway(String serialPortName) { this.serialPort = new NRSerialPort(serialPortName, 9600); this.serialPort.connect(); }
/** * Create a new instance */ public static IByteProvider create(NRSerialPort serialPort) { return new SerialPortByteProvider(serialPort); }
private boolean isPortNameExist(String portName) { return NRSerialPort.getAvailableSerialPorts().contains(portName); }
public static String[] getAvailableComPorts() { Set<String> set = NRSerialPort.getAvailableSerialPorts(); return set.toArray(new String[set.size()]); }
/** * Constructor * * @param serialPort */ private SerialPortByteProvider(NRSerialPort serialPort) { this.serialPort = serialPort; }
/** * Check if the Serial with the given name exist. * * @param portName * @return */ private boolean isPortNameExist(String portName) { return NRSerialPort.getAvailableSerialPorts().contains(portName); }