Java Code Examples for gnu.io.SerialPort#addEventListener()

The following examples show how to use gnu.io.SerialPort#addEventListener() . 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: 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 3
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 4
Source File: ObdAdapterClient.java    From java-obd-adapter with Apache License 2.0 5 votes vote down vote up
private void prepareSerialPort() {
//		String wantedPortName = "/dev/pts/34";
		String wantedPortName = "/dev/ttyUSB0";
		System.setProperty("gnu.io.rxtx.SerialPorts", wantedPortName);
		Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();

		CommPortIdentifier portId = null;

		while (portIdentifiers.hasMoreElements()) {
			CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
			if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
					pid.getName().equals(wantedPortName)) {
				portId = pid;
				break;
			}
		}

		try {
			// open serial port, and use class name for the appName.
			SerialPort serialPort = (SerialPort) portId.open(ObdAdapterClient.class.getName(), TIME_OUT);

			// set port parameters
			serialPort.setSerialPortParams(DATA_RATE,
					SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);

			// open the streams
			is = serialPort.getInputStream();
			os = serialPort.getOutputStream();

			// add event listeners
			serialPort.addEventListener(this);
			serialPort.notifyOnDataAvailable(false);
		} catch (Exception e) {
			log.error("cannot connect to serial port", e);
		}
	}
 
Example 5
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 6
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 7
Source File: SerialDiagnosticTransport.java    From java-obd-adapter with Apache License 2.0 4 votes vote down vote up
@Override
	public void open() {
		log.info("opening serial transport");
		String wantedPortName = "/dev/ttyUSB0";
//		String wantedPortName = "/dev/rfcomm1";
		System.setProperty("gnu.io.rxtx.SerialPorts", wantedPortName);
		Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();

		CommPortIdentifier portId = null;

		while (portIdentifiers.hasMoreElements()) {
			CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
			log.info("pid {}", pid);
			if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
					pid.getName().equals(wantedPortName)) {
				portId = pid;
				break;
			}
		}

		if (portId == null) {
			return;
		}

		try {
			// open serial port, and use class name for the appName.
			SerialPort serialPort = (SerialPort) portId.open(SerialDiagnosticTransport.class.getName(), TIME_OUT);

			// set port parameters
			serialPort.setSerialPortParams(DATA_RATE,
					SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);

			serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);
			serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_OUT);

			// open the streams
			is = serialPort.getInputStream();
			os = serialPort.getOutputStream();

			// add event listeners
			serialPort.addEventListener(this);
			serialPort.notifyOnDataAvailable(false);
			log.info("serial transport opened");
		} catch (Exception e) {
			log.error("cannot connect to serial port", e);
		}
	}