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 8376681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:29:02+00:00 2026-06-09T15:29:02+00:00

This is my code, I’m using rxtx . public void Send(byte[] bytDatos) throws IOException

  • 0

This is my code, I’m using rxtx.

public void Send(byte[] bytDatos) throws IOException {
        this.out.write(bytDatos);
    }

    public byte[] Read() throws IOException {

        byte[] buffer = new byte[1024];

        int len = 20;

        while(in.available()!=0){
            in.read(buffer);
        }

        System.out.print(new String(buffer, 0, len) + "\n");

        return buffer;
    }

the rest of code is just the same as this, i just changed 2 things.

InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();

They are global variables now and…

(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();

not exist now…

I’m sending this (each second)

Send(("123456789").getBytes());

And this is what i got:

123456789123
456789
123456789
1234567891
23456789

can anybody help me?

EDIT

Later, i got the better way to solve it. Thanks, this was the Read Code

public byte[] Read(int intEspera) throws IOException {

        try {
            Thread.sleep(intEspera);
        } catch (InterruptedException ex) {
            Logger.getLogger(COM_ClComunica.class.getName()).log(Level.SEVERE, null, ex);
        }//*/

        byte[] buffer = new byte[528];

        int len = 0;


        while (in.available() > 0) {
            len = in.available();
            in.read(buffer,0,528);
        }

        return buffer;
    }

It was imposible for me to erase that sleep but it is not a problem so, thanks veer

  • 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-06-09T15:29:04+00:00Added an answer on June 9, 2026 at 3:29 pm

    You should indeed note that InputStream.available is defined as follows…

    Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

    As you can see, this is not what you expected. Instead, you want to check for end-of-stream, which is indicated by InputStream.read() returning -1.

    In addition, since you don’t remember how much data you have already read in prior iterations of your read loop, you are potentially overwriting prior data in your buffer, which is again not something you likely intended.

    What you appear to want is something as follows:

    private static final int MESSAGE_SIZE = 20;
    
    public byte[] read() throws IOException {
      final byte[] buffer = new byte[MESSAGE_SIZE];
      int total = 0;
      int read = 0;
      while (total < MESSAGE_SIZE
                && (read = in.read(buffer, total, MESSAGE_SIZE - total)) >= 0) {
        total += read;
      }
      return buffer;
    }
    

    This should force it to read up to 20 bytes, less in the case of reaching the end of the stream.

    Special thanks to EJP for reminding me to maintain the quality of my posts and make sure they’re correct.

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

Sidebar

Related Questions

This code byte b = Byte.parseByte(10000000, 2); throws an exception in Java. This should
This code compiles and runs with either a public implicit bool cast (commented out
This code does not compile. public class Diamond { public static void diamondOfAsterisks(String *
This code is not pretty, but I want to print out the last generated
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
This code throws parse error, which I do not understand why. function t(){ return
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This code works properly in my localhost. I am using xampp 1.7.3. but when
This code does not seem to compile, I just need to write something to
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer

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.