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

  • Home
  • SEARCH
  • 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 3214144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:01:33+00:00 2026-05-17T15:01:33+00:00

I just joined StackOverflow after having found many great answers here in the past.

  • 0

I just joined StackOverflow after having found many great answers here in the past. Here’s my first question:

EDIT: I feel bad… my first question to StackOverflow turned out to be a “wild goose chase”. The problem is in the data, not the code reading the data. I was looking for a solution in the wrong place! I will explain in a comment below.

Please disregard this question.

In my application, data arrives in fragments (it is serial data). I want to pass the data along to a method that will parse it. But I only want to pass along properly terminated lines. The lines will be terminated by ‘\r’ (return char). Sometimes a fragment of data comes in that has multiple terminated lines. But sometimes a fragment of data comes in that doesn’t yet have a terminator (the terminator will arrive later).

My goal is to be able to send only properly terminated lines (complete lines) to dataProcessor.logSerialData. (The code works without this requirement, but I could write much cleaner code if I could meet this requirement. The parsing logic — not shown here — would be easier to maintain.)

Here is my code that DOES work (but does not meet my new requirement):

case SerialPortEvent.DATA_AVAILABLE:
            try
            {
                int len = -1;
                while ((len = in.read(readBuffer)) > 0)
                {
                    if (len > 0)
                    {
                        String s = new String(readBuffer, 0, len);
                        if (dataProcessor != null)
                        {
                            dataProcessor.logSerialData(s);
                        }
                        else
                        {
                            Logger.log("ERROR: packetReceiver is null");
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                Logger.log(ex);
                return;
            }

Here is an example of one of the many things I tried that does NOT work. It somehow causes errors in the data stream.

private static final String REGEX = ".*\\r";
private Pattern p = Pattern.compile(REGEX);

public void serialEvent(SerialPortEvent e)
{
    byte[] readBuffer = new byte[11000];//tried many values for buffer size

    switch (e.getEventType())
    {
        case SerialPortEvent.DATA_AVAILABLE:
            try
            {
                int len = -1;
                while ((len = in.read(readBuffer)) > 0)
                {
                    if (len > 0)
                    {
                        String input = new String(readBuffer, 0, len);
                        Matcher m = p.matcher(input);
                        int count = 0;
                        while (m.find())
                        {
                            count++;
                            String terminatedLine = m.group();
                            if (!terminatedLine.isEmpty())
                            {
                                completedLines.add(terminatedLine);
                            }
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                Logger.log(ex);
                return;
            }
            for (String line : completedLines)
            {
                dataProcessor.logSerialData(line);
            }

Any suggestions? (In fact, any idea why the new code introduces errors into the data stream? Every variation of the working code that I have tried does not work correctly. I have tried regex and non-regex methods and any other things. The only code that works is the working example shown above. Serial data processing seems very tricky! I could use some help.

  • 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-17T15:01:34+00:00Added an answer on May 17, 2026 at 3:01 pm

    None of the various methods of reading by lines worked. The original method worked only because it did not look for line terminators.

    Upon seeing that none of the “sure fire” methods worked predictably, I did further investigation. It seems that there is some inconsistency in the incoming data with regard to line terminators. Java sees terminators where there should not be any, and misses terminators that are there.

    After spending several days on this, I think I’m just going to go back to the original method that worked without looking for line terminators.

    Sorry for the bogus question.

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

Sidebar

Related Questions

I just joined a team to manage a asp.net 4 website with 1000+ pages
I have just joined new company and my responsibility is to create defect report.
Possible Duplicate: PHP headers already sent So I just joined Hostgator.com, and was wondering,
Little bite background: I joined a software company three years ago after graduated with
I am currently developing a little webapp with iWebKit I joined a UIWebView. And
In the code below, the two lists are joined on Index. But either list
I'm working with Unity 3 and my total level size will be just a
I've got three DataTables that I need to join together, and use the joined
I'm trying to use the following LINQ to SQL in my code: (from s
I have a column value stored in the database as: a:2:{i:0;s:2:US;i:1;s:2:19;} I want to

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.