0

I am reading values from RS32 port(weigh indicator) into my java swing application.The values are getting displayed on the console.What I want is to display those values in a jlabel in a frame with little modification on the values read from port. But I am unable to set values to jlabel. Please help.

My class is:

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
         super();
    }

    void connect (String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() ){
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

            if ( commPort instanceof SerialPort ) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.FLOWCONTROL_NONE);

                InputStream in = serialPort.getInputStream();
                (new Thread(new SerialReader(in))).start();

            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }

    public class SerialReader implements Runnable {
        InputStream in;
        BufferedReader reader;

        public SerialReader ( InputStream in ) {
            this.in = in;
            this.reader = new BufferedReader(new InputStreamReader(in));
        }

        public void run () {
            String line = null;

            try{
               while ((line = reader.readLine()) != null) {
                   System.out.println("Read line with " + line.length() + " characters: \"" + line + "\"");
                   lblRead.setText(line);
               }
            }
            catch ( IOException e ) {
                e.printStackTrace();
            }            
        }

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
                try {
                    (new NewJFrame()).connect("/dev/ttyUSB0");
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel lblRead;
    // End of variables declaration
}
silent_killer
  • 45
  • 3
  • 10
  • 1
    Where's `initComponents()`? Also, you should modify swing components only in the event dispatch thread, so `lblRead.setText(line);` should be wrapped with `invokeLater()`. (Or, even better, use a [SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for the background task). – kiheru Oct 04 '13 at 09:48
  • 1
    Oh, and please be specific about what the problem is. "i am unable to set values to jlabel" does not even tell whether you have trouble compile time or when the program is run, let alone the error message. – kiheru Oct 04 '13 at 09:54
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Always copy/paste error & exception output. – Andrew Thompson Oct 04 '13 at 10:06
  • there isn't any initcomponents(). I am getting these output on console: Read line with 5 characters: ":250" Read line with 4 characters: "0250" Read line with 2 characters: "40" Read line with 5 characters: ":250" Read line with 4 characters: "0250" Read line with 2 characters: "40" Read line with 5 characters: ":250" Read line with 4 characters: "0250" Read line with 2 characters: "40" Read line with 5 characters: ":250" i want to display these values in jlabel. – silent_killer Oct 04 '13 at 10:24
  • 1
    There should be. Initialize `lblRead` there, and add it to the frame. With code above you should be getting an NPE after every line read, so I suspect it still does not match what you're running. – kiheru Oct 04 '13 at 10:31
  • i have created a frame with a label. – silent_killer Oct 04 '13 at 10:34
  • Maybe, but not with the code you have posted. `lblRead` is never initialized, nor is the resulting `JLabel` added to the frame. – kiheru Oct 04 '13 at 10:39
  • did you use any command first to get ports in ubuntu? in my case ubuntu does show any ports neither parallel nor serial.. kindly help me.. – Java Man May 21 '15 at 06:07
  • I got this error when I ran your code. `java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path` – Java Man May 21 '15 at 06:22

1 Answers1

1

Instead of a new Thread(), extend SwingWorker. You can read the serial port in your doInBackground() implementation, publish() interim results, and update your labels in process(). Counter is a simple example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045