0

I am developing an application using the java-simple-serial-connector API to read and write to serial port. I am facing a problem when I try to read data bytes and assign them to a global String variable (sReader). sReader does not get the whole string and its length is random each time. I am sure that I am receiving all of the bytes because when I append string sBuffer to Output (Output.append) the whole string is displayed always. Please take a look at my sample code below:

public class SerialPortTest extends javax.swing.JFrame {

    private String sReader = "";
    private JTextArea outputTextArea;
    private JLabel outputLabel;

    private class Reader implements SerialPortEventListener {

        private String sBuffer = "";

        @Override
        public void serialEvent(SerialPortEvent spe) {

            if (spe.isRXCHAR() || spe.isRXFLAG()) {

                if (spe.getEventValue() > 0) {

                    try {

                        //Read chars from buffer
                        byte[] bBuffer = serialPort.readBytes(spe.getEventValue());
                        sBuffer = new String(bBuffer);

                        SwingUtilities.invokeAndWait(
                                new Runnable() {

                                    @Override
                                    public void run() {

                                        sReader = sBuffer;//does not assign full string

                                        outputLabel.setText(sBuffer);//does not set full string

                                        outputTextArea.setText(sBuffer);//does not set full string

                                        outputTextArea.append(sBuffer);//This works! Why the above don't work?

                                    }
                                });

                    } catch (SerialPortException | InterruptedException | InvocationTargetException ex) {
                    }
                }
            }
        }
    }
}
jadrijan
  • 1,438
  • 4
  • 31
  • 48
  • 1
    Note that `Output` is an instance variable and, by convention, should begin with a lowercase letter. – DNA Feb 28 '12 at 20:56
  • thanks for noticing that, it was an accident as I was quickly typing the example – jadrijan Feb 28 '12 at 20:58

1 Answers1

1

I'm assuming that you want access to sReader from some other portion of the code. In that case consider making sReader volatile and also assigning it before making the Runnable. Also, if sBuffer is only used to make a String to use in the Runnable consider making it a final local variable.

private volatile String sReader = "";
...

final String sBuffer = new String(bBuffer);
sReader = sBuffer;

SwingUtilities.invokeAndWait(
...
Clint
  • 8,988
  • 1
  • 26
  • 40
  • That is exactly why I need the sReader string ...unfortunately using your advice I did not fix the problem :( – jadrijan Feb 28 '12 at 21:45
  • 1
    Then consider that you are receiving multiple serialEvent calls and that you are seeing the fragments of the calls. Perhaps sReader should be a StringBuilder that you append to. – Clint Feb 28 '12 at 22:36