Read data from Serial Port

Hello,

i use the vaadin 7 framework. Now I wirite a custom component, that read the inputstream from the serial device “COM” on Windows Server.
My Problem:
The Textfild (bold text) don’t refresh, when a SerialPortEvent is presented.
Thanks.

Patrick

My code:

public class MyReport extends CustomComponent implements SerialPortEventListener
...

private SerialPort serialPort;
    /** The port we're normally going to use. */
    private static final String PORT_NAMES[] = { 
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyACM0", // Raspberry Pi
            "/dev/ttyUSB0", // Linux
            "COM4", // Windows
    };
    

    public MyReport() {
        buildMainLayout();
        setCompositionRoot(mainLayout);
        
        cmdCom.setImmediate(true);
        cmdCom.addClickListener(this);
        
        System.setProperty("gnu.io.rxtx.SerialPorts", "COM4");

        Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();

        // First, Find an instance of serial port as set in PORT_NAMES.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum
                    .nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }
        
    }
...
@Override
    public void buttonClick(ClickEvent event) {
        try {
            // open serial port, and use class name for the appName.
            serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);

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

            // open the streams
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            //setOutput(serialPort.getOutputStream());

            // add event listeners
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    
    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }
    
    @Override
    public void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine = input.readLine();
                System.out.println(inputLine);
                [b]
text.setValue(inputLine);
                if(inputLine.equals("LED 1 ON")){
                    cmdLED.setCaption("ON");
                }else{
                    cmdLED.setCaption("OFF");
                }
[/b]
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
        
    }

Just for clarify. If your serialEvent is fired from other thread, you have to use UI.access method to update UI compoment:
https://vaadin.com/book/-/page/advanced.push.html#advanced.push.running

As Anatoly mentioned before you should use another thread for reading from the serial port, but in real world scenario you need some kind of event messaging for non-blocking io. You can easy do that with incoming Spring addon for the Vaadin, which implements EventBus.
https://github.com/peholmst/vaadin4spring

Hello Anatoly,
hello Lukasz,

thank you for your information . May you tell me how I implemented this methodes ( for example my problem) .

Patrick