private void initSerialPort(String name, int baud) throws Exception { if (serialPort != null && serialPort.isOpen()) { closePort(); } serialPort = SerialPort.getCommPort(name); serialPort.setParity(SerialPort.NO_PARITY); serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT); serialPort.setNumDataBits(8); serialPort.addDataListener(this); serialPort.setBaudRate(baud); }
@Override public void connect() { SerialPort[] serialPorts = SerialPort.getCommPorts(); _logger.debug("Number of serial port available:{}", serialPorts.length); for (int portNo = 0; portNo < serialPorts.length; portNo++) { _logger.debug("SerialPort[{}]:[{},{}]", portNo + 1, serialPorts[portNo].getSystemPortName(), serialPorts[portNo].getDescriptivePortName()); } // create an instance of the serial communications class _serialPort = SerialPort.getCommPort(_config.getPortName()); _serialPort.openPort();//Open port if (!_serialPort.isOpen()) { _logger.error("Unable to open serial port:[{}]", _config.getPortName()); _config.setStatus(STATE.DOWN, "ERROR: Unable to open!"); return; } _serialPort.setComPortParameters( _config.getBaudRate(), 8, // data bits SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); // create and register the serial data listener _serialPort.addDataListener(new SerialDataListenerjSerialComm(_serialPort, _config, _parser, _queue)); _logger.debug("Serial port initialized with {}", _config); _config.setStatus(STATE.UP, "Connected Successfully"); }
@Override public void connect() { SerialPort[] serialPorts = SerialPort.getCommPorts(); _logger.debug("Number of serial port available:{}", serialPorts.length); for (int portNo = 0; portNo < serialPorts.length; portNo++) { _logger.debug("SerialPort[{}]:[{},{}]", portNo + 1, serialPorts[portNo].getSystemPortName(), serialPorts[portNo].getDescriptivePortName()); } // create an instance of the serial communications class _serialPort = SerialPort.getCommPort(_config.getPortName()); _serialPort.openPort();//Open port if (!_serialPort.isOpen()) { _logger.error("Unable to open serial port:[{}]", _config.getPortName()); _config.setStatus(STATE.DOWN, "ERROR: Unable to open!"); return; } _serialPort.setComPortParameters( _config.getBaudRate(), 8, // data bits SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); // create and register the serial data listener _serialPort.addDataListener(new SerialDataListenerjSerialComm(_serialPort, _config, _parser, _queue)); _logger.debug("Serial port initialized with {}", _config); _config.setStatus(STATE.UP, "Connected Successfully"); }
/** * Returns a JSerialComm implementation for the given comms port * * @param commPort Comms port e.g. /dev/ttyAMA0 * @return JSerialComm implementation */ public static AbstractSerialConnection getCommPort(String commPort) { SerialConnection jSerialCommPort = new SerialConnection(); jSerialCommPort.serialPort = SerialPort.getCommPort(commPort); return jSerialCommPort; }
/** * Returns a JSerialComm implementation for the given comms port * * @param commPort Comms port e.g. /dev/ttyAMA0 * @return JSerialComm implementation */ public static AbstractSerialConnection getCommPort(String commPort) { SerialConnection jSerialCommPort = new SerialConnection(); jSerialCommPort.serialPort = SerialPort.getCommPort(commPort); return jSerialCommPort; }
@Override protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { JSerialCommDeviceAddress remote = (JSerialCommDeviceAddress) remoteAddress; SerialPort commPort = SerialPort.getCommPort(remote.value()); if (!commPort.openPort()) { throw new IOException("Could not open port: " + remote.value()); } commPort.setComPortTimeouts( SerialPort.TIMEOUT_READ_BLOCKING, config().getOption(READ_TIMEOUT), 0); deviceAddress = remote; serialPort = commPort; }
@Override public void open() throws IOException { if (serialPort == null) { serialPort = SerialPort.getCommPort(parameters.getPortName()); if (serialPort.getDescriptivePortName().contains("Bad Port")) { serialPort = null;
@Override public void open() throws IOException { if (serialPort == null) { serialPort = SerialPort.getCommPort(parameters.getPortName()); if (serialPort.getDescriptivePortName().contains("Bad Port")) { serialPort = null;
public static SerialPort jSerialPort(String portName, SerialConfig config) { SerialPort serialPort = SerialPort.getCommPort(portName); serialPort.setFlowControl(config.getFlowControl().val); serialPort.setComPortParameters( config.getBaud().val, config.getDataBits().val, config.getStopBits().val, config.getParity().val ); return serialPort; }
public synchronized void connect() throws Exception { disconnect(); serialPort = SerialPort.getCommPort(portName); serialPort.openPort(0); serialPort.setComPortParameters(baud, dataBits.mask, stopBits.mask, parity.mask); serialPort.setFlowControl(flowControl.mask); if (setDtr) { serialPort.setDTR(); } if (setRts) { serialPort.setRTS(); } serialPort.setComPortTimeouts( SerialPort.TIMEOUT_READ_SEMI_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING, 500, 0); }