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()); } }
/** * 添加监听器 * * @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(); } }
/** * 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); } }
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; }
/** * 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()); } }
public void addListener(SerialPortEventListener serialPortEventListener) { try { serialPort.addEventListener(serialPortEventListener); serialPort.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { e.printStackTrace(); } }
/** * 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; }
/** * 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()); } }
public void addEventListener ( SerialPortEventListener listener ) { try { sPort.addEventListener( listener ); } catch ( TooManyListenersException e ) { sPort.close( ); logger.warn( e.getMessage( ) , e ); } }
/** * 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); }
/** * 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); }