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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:10:47+00:00 2026-06-02T03:10:47+00:00

I’m building a swing interface in Java, I build a middleware which keeps reading

  • 0

I’m building a swing interface in Java, I build a middleware which keeps reading the serial port and saving what’s coming to String, here it’s how I’m doing this:

public class RFID {
  private static RFIDReader rReader;
  private static boolean state;

  public RFID(RFIDReader rReader) {
    this.rReader = rReader;
    this.state = true;
  }

  public void connect(String portName) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if (portIdentifier.isCurrentlyOwned()) {
        System.out.println("Error: Port is currently in use");
    } else {
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

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

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

        } else {
            System.out.println("Error: Only serial ports are handled by this example.");
        }
    }
}

public static class SerialReader implements Runnable {

    InputStream in;

    public SerialReader(InputStream in) {
        this.in = in;
    }

    public void run() {
        byte[] buffer = new byte[1024];
        int len = -1;
        String code;
        try {
            while (state == true && (len = this.in.read(buffer)) > -1) {
                code = new String(buffer, 0, len);
                if (code.length() > 1)
                    rReader.setCode(code);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void finish(){
    state = false;
}

public static class SerialWriter implements Runnable {

    OutputStream out;

    public SerialWriter(OutputStream out) {
        this.out = out;
    }

    public void run() {
        try {
            int c = 0;
            while ((c = System.in.read()) > -1) {
                this.out.write(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

So when I try to print what code is storing, it shows like this:

AC000F9
3
BB

What actually should look like this:

AC000F93BB

What am I doing wrong here ? This conversion from byte[] to String is not right ?

EDIT:
I need to read a string with 10 characters at total.

  • 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-02T03:10:49+00:00Added an answer on June 2, 2026 at 3:10 am

    Don’t use an InputStream to read from – use a Reader of some description. If you need to start with an InputStream, wrap it in an InputStreamReader. I would change your constructor to accept a Reader though, and allow clients to pass whatever reader they want in. That will make it easier to test (as you can just use a StringReader).

    EDIT: Note – if you do need to create a reader from a binary InputStream, be explicit in your choice of Charset (encoding). So use the InputStreamReader constructor overload which specifies one. Even if you want to use the platform default, I’d be explicit about that to make it clear what you’re doing.

    EDIT: Beyond “where the string conversion happens” it’s unclear what you’re expecting to read. You’re dealing with streaming APIs – what determines how much you really want to read before calling setCode? Is it a whole line? Is it some fixed number of characters? Is it characters before a particular delimiter?

    EDIT: Now we know a bit more, I suggest you write a helper method like this:

    // Assuming you now have a field of type Reader, called reader
    private String readEntry() throws IOException {
        char[] buffer = new char[10];
        int index = 0;
        while (index < buffer.length) {
            int charsRead = reader.read(buffer, index, buffer.length - index);
            if (charsRead == -1) {
                throw new IOException("Couldn't read entry - end of data");
            }
            index += charsRead;
        }
        return new String(buffer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.