Question

Context: I am reading data from a serial port at 115.2 Kbaud. The read data is printed using a PrintWriter that I then have appending to a JTextArea.

Everything works well, but the text in the JTextArea does not appear until the method sending the stream from the serial port to my PrintWriter finishes. I'd like it to display closer to real-time, as I will at times be receiving upwards of 20-30 MB of text at a time, and how the general flow of text changes as the program executes would be valuable.

I am using the PrintWriter to JTextArea method here. I think the solution probably has to do with Threads and PipedWriter/PipedReader, but every attempt I've made to implement that has failed miserably.

Thank you for your help.

//code calling method;  VerifierReader does not inherit from Reader 
//or any such class. it's wholly homegrown. I send it the PrintWriter 
//as out, telling it to output there


verifierInstance=new VerifierReader("COM3", verifierOutputLocString.getText());
verifierInstance.setSysOutWriter(out);
verifierInstance.readVerifierStream();

// and the relevant code from VerifierReader

public void setSysOutWriter (PrintWriter outWriter) {
        sysOutWriter=new PrintWriter(outWriter);            

    }
public void readVerifierStream() throws SerialPortException, 
InterruptedException{

    try{
        sysOutWriter.println("Listening for verifier...");
                    //sysOutWriter.flush();

        verifierPort.addEventListener(new verifierListener());

        lastReadTimer=System.currentTimeMillis();

        while(verifierPort.isOpened()) {

            Thread.sleep(1000);
            //System.out.println(timeOut);
            if( ((long)(System.currentTimeMillis()-lastReadTimer))>timeOut){
                sysOutWriter.println("Finished");
                verifierPort.removeEventListener();
                verifierPort.closePort();

            }
        }
    }

    finally {

        if (verifierPort.isOpened()) {
            verifierPort.closePort();
        }

        bfrFile.close();

    }
}








private class verifierListener implements SerialPortEventListener{

    String outBuffer;




    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR()){//If data is available
            timeOut=200;
            lastReadTimer=System.currentTimeMillis();
            if(event.getEventValue() > 0){//Check bytes count in the input buffer

                try {
                    byte[] buffer = verifierPort.readBytes(event.getEventValue());
                    outBuffer=new String(buffer);

                    bfrFile.print(outBuffer);
                    sysOutWriter.print(outBuffer);
                        //bfrFile.flush();
                        //sysOutWriter.flush();
                }
                catch (SerialPortException ex) {
                    sysOutWriter.println(ex);
                }

            }
        }



    }
}

Edit:

I've attempted what was recommended below, and have made the following changes:

 private class VerifierTask extends SwingWorker<Void, String> {



    public VerifierTask() throws IOException, SerialPortException, InterruptedException{
        verifierInstance= new VerifierReader(streamReader);
        verifierInstance.setReaderIO("COM3", verifierOutputLocString.getText());
        verifierInstance.readVerifierStream();

    }


    @Override
    protected Void doInBackground() throws IOException{
        int charItem;
        char[] charBuff = new char[10];
        String passString;

        while ((charItem = streamReader.read(charBuff, 0, 10)) !=-1) {

            passString = new String(charBuff);
            publish(passString);
        }


        return null;
    }

    @Override 
    protected void process(List<String> outList) {

        for (String output : outList) {
            outputArea.append(output);
        }

    }



}

was added, and I changed my button to immediately invoke a new instance of the VerifierTask class, in addition to making VerifierReader implement a PipedWriter for output (with all of that being Strings).

I'm not sure what I'm doing wrong here. When this code is executed the Java process just freezes indefinitely.

Am I assuming correctly that a VerifierReader created in any VerifierTask thread is tied to that thread, and thus my thread.sleep and while(true) statements no longer pose a problem?

Was it helpful?

Solution

Don't call Thread.sleep or do while (true) on the main Swing event thread, the EDT. Ever. Instead do this sort of thing in a background thread such as one provided via a SwingWorker. You would use the publish/process method pair to get intermediate results to your JTextArea.

For more on this, please check out the tutorial: Concurrency in Swing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top