gnu.io.SerialPort Java Examples

The following examples show how to use gnu.io.SerialPort. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SerialSketchUploader.java    From arduino-remote-uploader with GNU General Public License v2.0 8 votes vote down vote up
public void openSerial(String serialPortName, int speed) throws PortInUseException, IOException, UnsupportedCommOperationException, TooManyListenersException {
	
	CommPortIdentifier commPortIdentifier = findPort(serialPortName);
	
	// initalize serial port
	serialPort = (SerialPort) commPortIdentifier.open("Arduino", 2000);
	inputStream = serialPort.getInputStream();
	serialPort.addEventListener(this);

	// activate the DATA_AVAILABLE notifier
	serialPort.notifyOnDataAvailable(true);

	serialPort.setSerialPortParams(speed, SerialPort.DATABITS_8, 
			SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
	serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
}
 
Example #2
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Sets the number of stop bits.
 *
 * @param stopbits the new number of stop bits setting.
 */
public void setStopbits(double stopbits) throws IllegalArgumentException {
    if (!SerialParameterValidator.isStopbitsValid(stopbits)) {
        throw new IllegalArgumentException("stopbit value '" + stopbits + "' not valid");
    }

    if (stopbits == 1) {
        m_Stopbits = SerialPort.STOPBITS_1;
    } else if (stopbits == 1.5) {
        m_Stopbits = SerialPort.STOPBITS_1_5;
    } else if (stopbits == 2) {
        m_Stopbits = SerialPort.STOPBITS_2;
    } else {
        m_Stopbits = SerialPort.STOPBITS_1;
    }
}
 
Example #3
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Converts an <code>int</code> describing a flow control type to a <code>String</code>
 * describing a flow control type.
 * 
 * @param flowControl
 *            An <code>int</code> describing a flow control type.
 * @return A <code>String</code> describing a flow control type.
 */
String flowToString(final int flowControl){
	switch (flowControl) {
	case SerialPort.FLOWCONTROL_NONE:
		return "None"; //$NON-NLS-1$
	case SerialPort.FLOWCONTROL_XONXOFF_OUT:
		return "Xon/Xoff Out"; //$NON-NLS-1$
	case SerialPort.FLOWCONTROL_XONXOFF_IN:
		return "Xon/Xoff In"; //$NON-NLS-1$
	case SerialPort.FLOWCONTROL_RTSCTS_IN:
		return "RTS/CTS In"; //$NON-NLS-1$
	case SerialPort.FLOWCONTROL_RTSCTS_OUT:
		return "RTS/CTS Out"; //$NON-NLS-1$
	default:
		return "None"; //$NON-NLS-1$
	}
}
 
Example #4
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Converts a <code>String</code> describing a flow control type to an <code>int</code> type
 * defined in <code>SerialPort</code>.
 * 
 * @param flowControl
 *            A <code>string</code> describing a flow control type.
 * @return An <code>int</code> describing a flow control type.
 */
private int stringToFlow(final String flowControl){
	if (flowControl.equals("None")) { //$NON-NLS-1$
		return SerialPort.FLOWCONTROL_NONE;
	}
	if (flowControl.equals("Xon/Xoff Out")) { //$NON-NLS-1$
		return SerialPort.FLOWCONTROL_XONXOFF_OUT;
	}
	if (flowControl.equals("Xon/Xoff In")) { //$NON-NLS-1$
		return SerialPort.FLOWCONTROL_XONXOFF_IN;
	}
	if (flowControl.equals("RTS/CTS In")) { //$NON-NLS-1$
		return SerialPort.FLOWCONTROL_RTSCTS_IN;
	}
	if (flowControl.equals("RTS/CTS Out")) { //$NON-NLS-1$
		return SerialPort.FLOWCONTROL_RTSCTS_OUT;
	}
	return SerialPort.FLOWCONTROL_NONE;
}
 
Example #5
Source File: SerialPortExample.java    From marine-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Init serial port and reader.
 */
private void init() {
	try {
		SerialPort sp = getSerialPort();

		if (sp != null) {
			InputStream is = sp.getInputStream();
			SentenceReader sr = new SentenceReader(is);
			sr.addSentenceListener(this);
			sr.start();
		}

	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #6
Source File: CULSerialConfigFactory.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public CULConfig create(String deviceType, String deviceAddress, CULMode mode, Dictionary<String, ?> config)
        throws ConfigurationException {
    int baudRate = 9600;
    final String configuredBaudRate = (String) config.get(KEY_BAUDRATE);
    Integer tmpBaudRate = baudrateFromConfig(configuredBaudRate);
    if (tmpBaudRate != null) {
        baudRate = tmpBaudRate;
        logger.info("Update config, {} = {}", KEY_BAUDRATE, baudRate);
    }

    int parityMode = SerialPort.PARITY_EVEN;
    final String configuredParity = (String) config.get(KEY_PARITY);
    Integer parsedParityNumber = parityFromConfig(configuredParity);
    if (parsedParityNumber != null) {
        parityMode = parsedParityNumber;
        logger.info("Update config, {} = {} ({})", KEY_PARITY, convertParityModeToString(parityMode), parityMode);
    }

    return new CULSerialConfig(deviceType, deviceAddress, mode, baudRate, parityMode);
}
 
Example #7
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Converts an <tt>int</tt> describing a flow control type to a
 * String describing a flow control type.
 *
 * @param flowcontrol the <tt>int</tt> describing the
 *            flow control type.
 * @return the <tt>String</tt> describing the flow control type.
 */
private String flowToString(int flowcontrol) {
    switch (flowcontrol) {
        case SerialPort.FLOWCONTROL_NONE:
            return "none";
        case SerialPort.FLOWCONTROL_XONXOFF_OUT:
            return "xon/xoff out";
        case SerialPort.FLOWCONTROL_XONXOFF_IN:
            return "xon/xoff in";
        case SerialPort.FLOWCONTROL_RTSCTS_IN:
            return "rts/cts in";
        case SerialPort.FLOWCONTROL_RTSCTS_OUT:
            return "rts/cts out";
        default:
            return "none";
    }
}
 
Example #8
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Converts a <tt>String</tt> describing a flow control type to the
 * <tt>int</tt> which is defined in SerialPort.
 *
 * @param flowcontrol the <tt>String</tt> describing the flow control type.
 * @return the <tt>int</tt> describing the flow control type.
 */
private int stringToFlow(String flowcontrol) {
    flowcontrol = flowcontrol.toLowerCase();
    if (flowcontrol.equals("none")) {
        return SerialPort.FLOWCONTROL_NONE;
    }
    if (flowcontrol.equals("xon/xoff out")) {
        return SerialPort.FLOWCONTROL_XONXOFF_OUT;
    }
    if (flowcontrol.equals("xon/xoff in")) {
        return SerialPort.FLOWCONTROL_XONXOFF_IN;
    }
    if (flowcontrol.equals("rts/cts in")) {
        return SerialPort.FLOWCONTROL_RTSCTS_IN;
    }
    if (flowcontrol.equals("rts/cts out")) {
        return SerialPort.FLOWCONTROL_RTSCTS_OUT;
    }
    return SerialPort.FLOWCONTROL_NONE;
}
 
Example #9
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Sets the parity schema from the given
 * <tt>String</tt>.
 *
 * @param parity the new parity schema as <tt>String</tt>.
 */
public void setParity(String parity) throws IllegalArgumentException {
    parity = parity.toLowerCase();
    int intParity = SerialPort.PARITY_NONE;

    if (parity.equals("none") || parity.equals("n")) {
        intParity = SerialPort.PARITY_NONE;
    } else if (parity.equals("even") || parity.equals("e")) {
        intParity = SerialPort.PARITY_EVEN;
    } else if (parity.equals("odd") || parity.equals("o")) {
        intParity = SerialPort.PARITY_ODD;
    } else {
        throw new IllegalArgumentException("unknown parity string '" + parity + "'");
    }

    setParity(intParity);
}
 
Example #10
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Sets the number of data bits.
 *
 * @param databits the new number of data bits.
 */
public void setDatabits(int databits) throws IllegalArgumentException {
    if (!SerialParameterValidator.isDataBitsValid(databits)) {
        throw new IllegalArgumentException("Databit '" + databits + "' invalid");
    }

    switch (databits) {
        case 5:
            m_Databits = SerialPort.DATABITS_5;
            break;
        case 6:
            m_Databits = SerialPort.DATABITS_6;
            break;
        case 7:
            m_Databits = SerialPort.DATABITS_7;
            break;
        case 8:
            m_Databits = SerialPort.DATABITS_8;
            break;
        default:
            m_Databits = SerialPort.DATABITS_8;
            break;
    }
}
 
Example #11
Source File: SerialParameters.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Constructs a new <tt>SerialParameters</tt> instance with
 * parameters obtained from a <tt>Properties</tt> instance.
 *
 * @param props a <tt>Properties</tt> instance.
 * @param prefix a prefix for the properties keys if embedded into
 *            other properties.
 */
public SerialParameters(Properties props, String prefix) {
    if (prefix == null) {
        prefix = "";
    }
    setPortName(props.getProperty(prefix + "portName", ""));
    setBaudRate(props.getProperty(prefix + "baudRate", "" + 9600));
    setFlowControlIn(props.getProperty(prefix + "flowControlIn", "" + SerialPort.FLOWCONTROL_NONE));
    setFlowControlOut(props.getProperty(prefix + "flowControlOut", "" + SerialPort.FLOWCONTROL_NONE));
    setParity(props.getProperty(prefix + "parity", "" + SerialPort.PARITY_NONE));
    setDatabits(props.getProperty(prefix + "databits", "" + SerialPort.DATABITS_8));
    setStopbits(props.getProperty(prefix + "stopbits", "" + SerialPort.STOPBITS_1));
    setEncoding(props.getProperty(prefix + "encoding", Modbus.DEFAULT_SERIAL_ENCODING));
    setEcho("true".equals(props.getProperty(prefix + "echo")));
    setReceiveTimeoutMillis(props.getProperty(prefix + "timeout", "" + 500));
}
 
Example #12
Source File: SerialFatekConnectionFactory.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
private int getParity() throws IOException {
    final Optional<String> parity = getParam("parity");
    switch (parity.map(String::toUpperCase).orElse("EVEN")) {
        case "NONE":
            return SerialPort.PARITY_NONE;
        case "ODD":
            return SerialPort.PARITY_ODD;
        case "EVEN":
            return SerialPort.PARITY_EVEN;
        case "MARK":
            return SerialPort.PARITY_MARK;
        case "SPACE":
            return SerialPort.PARITY_SPACE;
        default:
            throw new IOException("Unknown parity=" + parity);
    }
}
 
Example #13
Source File: RFXComSerialConnector.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void connect(String device)
        throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(device);

    CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

    serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.enableReceiveThreshold(1);
    serialPort.disableReceiveTimeout();

    in = serialPort.getInputStream();
    out = serialPort.getOutputStream();

    out.flush();
    if (in.markSupported()) {
        in.reset();
    }

    readerThread = new SerialReader(in);
    readerThread.start();
}
 
Example #14
Source File: SwegonVentilationSerialConnector.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void connect() throws SwegonVentilationException {

    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        in = serialPort.getInputStream();

        logger.debug("Swegon ventilation Serial Port message listener started");

    } catch (Exception e) {
        throw new SwegonVentilationException(e);
    }
}
 
Example #15
Source File: SerialPortManager.java    From SerialPortDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 添加监听器
 * 
 * @param port
 *            串口对象
 * @param listener
 *            串口存在有效数据监听
 */
public static void addListener(SerialPort serialPort, DataAvailableListener listener) {
	try {
		// 给串口添加监听器
		serialPort.addEventListener(new SerialPortListener(listener));
		// 设置当有数据到达时唤醒监听接收线程
		serialPort.notifyOnDataAvailable(true);
		// 设置当通信中断时唤醒中断线程
		serialPort.notifyOnBreakInterrupt(true);
	} catch (TooManyListenersException e) {
		e.printStackTrace();
	}
}
 
Example #16
Source File: RxtxChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
    final CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(remote.value());
    final CommPort commPort = cpi.open(getClass().getName(), 1000);
    commPort.enableReceiveTimeout(config().getOption(READ_TIMEOUT));
    deviceAddress = remote;

    serialPort = (SerialPort) commPort;
}
 
Example #17
Source File: SerialLinkFactory.java    From Ardulink-2 with Apache License 2.0 5 votes vote down vote up
@Override
public LinkDelegate newLink(SerialLinkConfig config)
		throws NoSuchPortException, PortInUseException,
		UnsupportedCommOperationException, IOException {
	CommPortIdentifier portIdentifier = CommPortIdentifier
			.getPortIdentifier(config.getPort());
	checkState(!portIdentifier.isCurrentlyOwned(),
			"Port %s is currently in use", config.getPort());
	final SerialPort serialPort = serialPort(config, portIdentifier);

	StreamConnection connection = new StreamConnection(
			serialPort.getInputStream(), serialPort.getOutputStream(),
			config.getProto());

	ConnectionBasedLink connectionBasedLink = new ConnectionBasedLink(
			connection, config.getProto());
	Link link = config.isQos() ? new QosLink(connectionBasedLink)
			: connectionBasedLink;

	waitForArdulink(config, connectionBasedLink);
	return new LinkDelegate(link) {
		@Override
		public void close() throws IOException {
			super.close();
			serialPort.close();
		}
	};
}
 
Example #18
Source File: EpsonProjectorSerialConnector.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void connect() throws EpsonProjectorException {

    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);

        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();

        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();

        out.flush();
        if (in.markSupported()) {
            in.reset();
        }

        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }

}
 
Example #19
Source File: SerialConnector.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 **/
public void open() {

    try {

        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();

        serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
        serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

        setSerialEventHandler(this);

        connected = true;

    } catch (NoSuchPortException noSuchPortException) {
        logger.error("open(): No Such Port Exception: ", noSuchPortException);
        connected = false;
    } catch (PortInUseException portInUseException) {
        logger.error("open(): Port in Use Exception: ", portInUseException);
        connected = false;
    } catch (UnsupportedCommOperationException unsupportedCommOperationException) {
        logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
        connected = false;
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
        connected = false;
    } catch (IOException ioException) {
        logger.error("open(): IO Exception: ", ioException);
        connected = false;
    }
}
 
Example #20
Source File: ModbusSlaveEndpointTestCase.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEqualsSameSerial2() {
    ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint(new SerialParameters("port1", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, true, 500));
    ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint(new SerialParameters("port1", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, true, 500));
    Assert.assertEquals(e1, e2);
}
 
Example #21
Source File: ModbusSlaveEndpointTestCase.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * even though different echo parameter & baud rate, the endpoints are considered the same due to same port
 */
@Test
public void testEqualsSameSerial3() {
    ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint(new SerialParameters("port1", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, true, 500));
    ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint(new SerialParameters("port1", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, false, 500));
    Assert.assertEquals(e1, e2);
    Assert.assertEquals(e1.hashCode(), e2.hashCode());
}
 
Example #22
Source File: ModbusSlaveEndpointTestCase.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEqualsDifferentSerial() {
    ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint(new SerialParameters("port1", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, true, 500));
    ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint(new SerialParameters("port2", 9600,
            SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE, Modbus.DEFAULT_SERIAL_ENCODING, true, 500));
    Assert.assertNotEquals(e1, e2);
    Assert.assertNotEquals(e1.hashCode(), e2.hashCode());
}
 
Example #23
Source File: EiscpSerial.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean connect(String serialPortName) {

    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);

        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();

        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();

        out.flush();
        if (in.markSupported()) {
            in.reset();
        }

        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event
        // loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
        return true;
    } catch (Exception e) {
        logger.error("serial port connect error", e);
        e.printStackTrace();
        return false;
    }

}
 
Example #24
Source File: SerialEvent.java    From Serial with GNU General Public License v3.0 5 votes vote down vote up
public void handleEvent() {
	try {
		CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(SerialConf.WINDOWS_PORT);
		
		if (portId.isCurrentlyOwned()) { //端口已开启
			System.out.println("Port busy!");
		} else {
			CommPort commPort = portId.open("whatever it's name", SerialConf.TIME_OUT);
			if (commPort instanceof SerialPort) {
				serialPort = (SerialPort) commPort;
				//设置参数
				serialPort.setSerialPortParams(SerialConf.BAUD, 
												SerialPort.DATABITS_8, 
												SerialPort.STOPBITS_1, 
												SerialPort.PARITY_EVEN);
				in = serialPort.getInputStream(); //接收数据
				serialPort.notifyOnDataAvailable(true);
				serialPort.addEventListener(this);
			} else {
				commPort.close();
				System.out.println("the port is not serial!");
			}
		}
	} catch (Exception e) {
		serialPort.close();
		System.out.println("Error: SerialOpen!!!");
		e.printStackTrace();
	}
}
 
Example #25
Source File: SerialPortParameters.java    From nordpos with GNU General Public License v3.0 5 votes vote down vote up
public static int getStopBits(String sStopBitsParam) {
    switch (sStopBitsParam) {
        case "1":
            return SerialPort.STOPBITS_1;
        case "2":
            return SerialPort.STOPBITS_2;
        default:
            return SerialPort.STOPBITS_1;
    }
}
 
Example #26
Source File: SerialRXTXComm.java    From meshnet with GNU General Public License v3.0 5 votes vote down vote up
public SerialRXTXComm(CommPortIdentifier portIdentifier, Layer3Base layer3) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException, TooManyListenersException{

		if (portIdentifier.isCurrentlyOwned()) {
			throw new IOException("Port is currently in use");
		} else {
			CommPort commPort = portIdentifier.open(this.getClass().getName(),
					TIME_OUT);

			if (commPort instanceof SerialPort) {
				SerialPort serialPort = (SerialPort) commPort;
				serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
						SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

				inStream = serialPort.getInputStream();
				outStream = serialPort.getOutputStream();

				new SerialReceiver().start();

				/*serialPort.addEventListener(this);
				serialPort.notifyOnDataAvailable(true);*/

			} else {
				throw new IOException("This is not a serial port!.");
			}
		}
		
		this.layer2 = new Layer2Serial(this, layer3);
	}
 
Example #27
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Default constructer. Sets parameters to no port, 9600 baud, no flow control, 8 data bits, 1
 * stop bit, no parity.
 */
public SerialParameters(){
	this(
		"", //$NON-NLS-1$
		9600, SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8,
		SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
	
}
 
Example #28
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets data bits.
 * 
 * @param databits
 *            New data bits setting.
 */
public void setDatabits(final String databits){
	if (databits.equals("5")) { //$NON-NLS-1$
		this.databits = SerialPort.DATABITS_5;
	}
	if (databits.equals("6")) { //$NON-NLS-1$
		this.databits = SerialPort.DATABITS_6;
	}
	if (databits.equals("7")) { //$NON-NLS-1$
		this.databits = SerialPort.DATABITS_7;
	}
	if (databits.equals(Messages.SerialParameters_4)) {
		this.databits = SerialPort.DATABITS_8;
	}
}
 
Example #29
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets data bits as a <code>String</code>.
 * 
 * @return Current data bits setting.
 */
public String getDatabitsString(){
	switch (databits) {
	case SerialPort.DATABITS_5:
		return "5"; //$NON-NLS-1$
	case SerialPort.DATABITS_6:
		return "6"; //$NON-NLS-1$
	case SerialPort.DATABITS_7:
		return "7"; //$NON-NLS-1$
	case SerialPort.DATABITS_8:
		return "8"; //$NON-NLS-1$
	default:
		return "8"; //$NON-NLS-1$
	}
}
 
Example #30
Source File: SerialParameters.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets stop bits.
 * 
 * @param stopbits
 *            New stop bits setting.
 */
public void setStopbits(final String stopbits){
	if (stopbits.equals("1")) { //$NON-NLS-1$
		this.stopbits = SerialPort.STOPBITS_1;
	}
	if (stopbits.equals("1.5")) { //$NON-NLS-1$
		this.stopbits = SerialPort.STOPBITS_1_5;
	}
	if (stopbits.equals("2")) { //$NON-NLS-1$
		this.stopbits = SerialPort.STOPBITS_2;
	}
}