Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:44:16+00:00 2026-05-26T22:44:16+00:00

Context: I am reading data from a serial port at 115.2 Kbaud. The read

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:44:16+00:00Added an answer on May 26, 2026 at 10:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am guessing that even reading from shared data in openmp causes some parallel
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
I am writing to file in android and read from the same file using
is there a C#/.NET library or otherwise existing code that can read persisted data
I have a ms sql 2008 table with data from a weather datalogger. table
I want to read (only) records from data/data/com.android.providers.telephony/databases/mmssms.db (I know no official API, etc.
I have a DAL project, with an entity data model context bound with EF
I'm making an application that get data from webservice , insert it into a
I have an application that saves its context to XML. In this application, there
At the moment I'm coding a web application that imports image data from Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.