Java Code Examples for gnu.io.SerialPortEventListener

The following are top voted examples for showing how to use gnu.io.SerialPortEventListener. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: uyariSistemi   File: UI_uyari.java   View source code 10 votes vote down vote up
public void initialize() {
        System.setProperty("gnu.io.rxtx.SerialPorts", getComPortName());        
        CommPortIdentifier portId = null;        
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if(currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (currPortId.getName().equals(txtComPortName.getText())) {
                    System.out.println(txtComPortName.getText());
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
            
            JOptionPane.showMessageDialog(null," Portuna bağlı cihaz yok!","Hata",JOptionPane.ERROR_MESSAGE);
            System.out.println("Porta bağlı cihaz yok!");
            return;
        }
System.out.println(portId);
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);

            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            serialPort.addEventListener((SerialPortEventListener) this);
           serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
 
Example 2
Project: SerialPortDemo   File: SerialPortManager.java   View source code 5 votes vote down vote up
/**
 * 添加监听器
 * 
 * @param port
 *            串口对象
 * @param listener
 *            串口监听器
 * @throws TooManyListeners
 *             监听类对象过多
 */
public static void addListener(SerialPort port,
		SerialPortEventListener listener) throws TooManyListeners {
	try {
		// 给串口添加监听器
		port.addEventListener(listener);
		// 设置当有数据到达时唤醒监听接收线程
		port.notifyOnDataAvailable(true);
		// 设置当通信中断时唤醒中断线程
		port.notifyOnBreakInterrupt(true);
	} catch (TooManyListenersException e) {
		throw new TooManyListeners();
	}
}
 
Example 3
Project: openhab-hdl   File: SerialConnector.java   View source code 5 votes vote down vote up
/**
 * Set the serial event handler
 */
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(serialPortEventListenser);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException tooManyListenersException) {
		logger.error("open(): Too Many Listeners Exception: ", tooManyListenersException);
    }
}
 
Example 4
Project: contexttoolkit   File: SerialConnection.java   View source code 5 votes vote down vote up
public boolean addEventListener(SerialPortEventListener serialPortEventListener){
	if (serialPort != null) {
		try {
			serialPort.addEventListener(serialPortEventListener);
			serialPort.notifyOnDataAvailable(true);
			return true;
		} catch (TooManyListenersException e) {
			System.err.println(e.toString());
		}
	}
	return false;
}
 
Example 5
Project: openhab2-addons   File: IT100BridgeHandler.java   View source code 5 votes vote down vote up
/**
 * Set the serial event handler.
 *
 * @param serialPortEventListenser
 */
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(serialPortEventListenser);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException tooManyListenersException) {
        logger.error("setSerialEventHandler(): Too Many Listeners Exception: {}",
                tooManyListenersException.getMessage());
    }
}
 
Example 6
Project: muse-x   File: ArduinoConnection.java   View source code 5 votes vote down vote up
public void addListener(SerialPortEventListener serialPortEventListener) {
    try {
        serialPort.addEventListener(serialPortEventListener);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: uia.comm4j   File: RS232.java   View source code 5 votes vote down vote up
/**
 * Connect.
 * @param portName RS232 port name.
 * @param baudrate Baud rate.
 * @param dataBits Data bits.
 * @param stopBits Stop bits.
 * @param parity Parity.
 * @return Success or not.
 * @throws Exception
 */
public boolean connect(String portName, int baudrate, int dataBits, int stopBits, int parity) throws Exception {
    this.monitor = this.protocol.createMonitor(this.aliasName);
    this.monitor.setController(this);

    if (this.started) {
        return true;
    }

    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if (portIdentifier.isCurrentlyOwned()) {
        return false;
    }

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

    this.serialPort = (SerialPort) commPort;
    this.serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity);

    this.in = this.serialPort.getInputStream();
    this.out = this.serialPort.getOutputStream();
    this.serialPort.addEventListener(new SerialPortEventListener() {

        @Override
        public void serialEvent(SerialPortEvent evt) {
            messageReceived();
        }
    });
    this.serialPort.notifyOnDataAvailable(true);

    this.started = true;

    return true;
}
 
Example 8
Project: opennmszh   File: LoopbackEventTest.java   View source code 5 votes vote down vote up
/**
 * Register event handler for data available event
 *
 * @param eventHandler Event handler
 */
public void addDataAvailableEventHandler(
        SerialPortEventListener eventHandler) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(eventHandler);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException ex) {
        System.err.println(ex.getMessage());
    }
}
 
Example 9
Project: gsn   File: SerialWrapper.java   View source code 5 votes vote down vote up
public void addEventListener ( SerialPortEventListener listener ) {
	try {
		sPort.addEventListener( listener );
	} catch ( TooManyListenersException e ) {
		sPort.close( );
		logger.warn( e.getMessage( ) , e );
	}
}
 
Example 10
Project: openhab1-addons   File: SerialConnector.java   View source code 5 votes vote down vote up
/**
 * Set the serial event handler
 */
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(serialPortEventListenser);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException tooManyListenersException) {
        logger.error("open(): Too Many Listeners Exception: ", tooManyListenersException);
    }
}
 
Example 11
Project: OpenNMS   File: LoopbackEventTest.java   View source code 5 votes vote down vote up
/**
 * Register event handler for data available event
 *
 * @param eventHandler Event handler
 */
public void addDataAvailableEventHandler(
        SerialPortEventListener eventHandler) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(eventHandler);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException ex) {
        System.err.println(ex.getMessage());
    }
}
 
Example 12
Project: EasyVote   File: VCDSerialPort.java   View source code 2 votes vote down vote up
/**
 * F�gt den Listener der Instanz hinzu. Es kann dabei nur einen Listener geben, sonst wird eine Exception geworfen.
 * 
 * @param spel
 * @throws TooManyListenersException
 */
public void addPortEventListener(SerialPortEventListener spel) throws TooManyListenersException{
		serialPort.addEventListener(spel);
	
}
 
Example 13
Project: tools-libraries   File: SerialPortWrapper.java   View source code 2 votes vote down vote up
/**
 * Add a port listener to the serial port. Only one listener is allowed at
 * the same time. The port must be opened before a listener can be added.
 * 
 * @param listener is the listener to add.
 * @throws TooManyListenersException is thrown if another listener was
 *            previously added.
 */
public void addEventListener(SerialPortEventListener listener) throws TooManyListenersException
{
   serialPort.addEventListener(listener);
}