java类gnu.io.SerialPortEventListener的实例源码

UI_uyari.java 文件源码 项目:uyariSistemi 阅读 22 收藏 0 点赞 0 评论 0
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());
        }
    }
SerialPortManager.java 文件源码 项目:SerialPortDemo 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 添加监听器
 * 
 * @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();
    }
}
SerialConnector.java 文件源码 项目:openhab-hdl 阅读 27 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
SerialConnection.java 文件源码 项目:contexttoolkit 阅读 24 收藏 0 点赞 0 评论 0
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;
}
IT100BridgeHandler.java 文件源码 项目:openhab2-addons 阅读 19 收藏 0 点赞 0 评论 0
/**
 * 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());
    }
}
ArduinoConnection.java 文件源码 项目:muse-x 阅读 23 收藏 0 点赞 0 评论 0
public void addListener(SerialPortEventListener serialPortEventListener) {
    try {
        serialPort.addEventListener(serialPortEventListener);
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException e) {
        e.printStackTrace();
    }
}
RS232.java 文件源码 项目:uia.comm4j 阅读 22 收藏 0 点赞 0 评论 0
/**
 * 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;
}
LoopbackEventTest.java 文件源码 项目:opennmszh 阅读 26 收藏 0 点赞 0 评论 0
/**
 * 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());
    }
}
SerialWrapper.java 文件源码 项目:gsn 阅读 24 收藏 0 点赞 0 评论 0
public void addEventListener ( SerialPortEventListener listener ) {
    try {
        sPort.addEventListener( listener );
    } catch ( TooManyListenersException e ) {
        sPort.close( );
        logger.warn( e.getMessage( ) , e );
    }
}
SerialConnector.java 文件源码 项目:openhab1-addons 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
LoopbackEventTest.java 文件源码 项目:OpenNMS 阅读 23 收藏 0 点赞 0 评论 0
/**
 * 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());
    }
}
VCDSerialPort.java 文件源码 项目:EasyVote 阅读 25 收藏 0 点赞 0 评论 0
/**
 * 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);

}
SerialPortWrapper.java 文件源码 项目:tools-libraries 阅读 85 收藏 0 点赞 0 评论 0
/**
 * 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);
}


问题


面经


文章

微信
公众号

扫码关注公众号