Search code examples
javauser-interfacearduinoserial-portrxtx

receive data in JAVA via rxtx library and SerialPortEvent displaying string text in jTextArea


now this is the issue , I have an Arduino uno r3 that sends data via serial port , I have a java gui code to receive the data in bytes[] array , convert it and store in String st and display it in jTextArea . the problem is that the jtextArea doesn't display any thing , probably considering that string st value is null , but if I used the famous System.out.print(st) the result is displayed correctly in the console. i don't know whats wrong , iam posting the piece of code responsible for getting data from serial port below , the synchronized serialEvent method, any help would be greatly appreciated , help me please :) please note that jTextArea1 is declared as private in the same class and String st is declared as public in the same class thanks very much

public synchronized void serialEvent(SerialPortEvent oEvent) { 
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {           
        try {                 
                int available = input.available();
            byte[] chunk = new byte[available];
            input.read(chunk, 0, available);
            st = new String(chunk);
            jTextArea1.append(st);                                
        }catch (IOException e) {System.out.println("IO Error Occurred: " + e.toString());}

Solution

  • Ok I got it ! I had to create a jPanel at first inside the JFrame and add all the components into it instead of adding the components inside the JFrame directly ! now it works like a charm ! thank you very much for your concern .