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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:24:19+00:00 2026-06-13T20:24:19+00:00

I try to read a binary protocol from my serial port in Java. I

  • 0

I try to read a binary protocol from my serial port in Java. I use the RXTX library to read from the serial port.

The protocol is the SiRF binary protocol for GPS receivers. The structure is:

  • first two bytes are 0xA0 and 0xA2, which define the beginning of a data frame
  • next two bytes are the payload length n
  • next n bytes are the payload
  • last two bytes are 0xB0 and 0xB3, which define the end of a data frame

Although this structure is quite simple I am not able to read the data correctly. I tried two different ways:

  1. I wrote a thread, which reads from the InputStream and compares the input with 0xA0. If this matches, then the thread reads the next byte and compares to 0xA2. If this matches again, it reads the next two bytes and computes the payload length. After that, it reads the payload with length n bytes. At last, it reads the last two bytes and compares them to 0xB0 and 0xB3. When everything matches, it creates a hex string from the input and saves it to a queue. Here is my code:

    try {
        byte[] size = new byte[2];
        byte[] checkSum = new byte[2];
        byte[] packet;
        int bytesRead = -1;
        while (in.available() > 0) {
            if (getInput() == 0xA0) {
                if (getInput() == 0xA2) {
                    System.out.print("160,162,");
                    bytesRead = in.read(size);
                    int payLoadLength = (size[0] << 8) | (size[1] & 0xFF);
                    byte[] payload = new byte[payLoadLength];
                    bytesRead = in.read(payload);
                    bytesRead = in.read(checkSum);
                    if (getInput() == 0xB0) {
                        if (getInput() == 0xB3) {
                    System.out.println("out");
                    packet = new byte[2 + 2 + payLoadLength + 2 + 2];
                    packet[0] = (byte) START_1;
                    packet[1] = (byte) START_2;
                    packet[2] = size[0];
                    packet[3] = size[1];
                    for (int i = 0; i < payload.length; i++) {
                        packet[i + 4] = payload[i];
                    }
                    packet[packet.length - 4] = checkSum[0];
                    packet[packet.length - 3] = checkSum[1];
                    packet[packet.length - 2] = (byte) END_1;
                    packet[packet.length - 1] = (byte) END_2;
                    StringBuffer hexString = new StringBuffer();
                    int[] output = new int[packet.length];
                    for (int i = 0; i < packet.length; i++) {
                        output[i] = 0xFF & packet[i];
                        hexString.append(Integer.toHexString(output[i]));
                    }
                    TransportMessage tm = new TransportMessage(hexString.toString());
                    boolean b = queue.offer(tm);
                    System.out.println(hexString.toString().toUpperCase());
                    if(!b)
                        System.err.println("TransportMessageQueue voll!");
                }
            } else {
                System.out.println();
            }
        }
    } 
        }
    } catch (IOException e) {
        System.out.println(e);
    }
    

    getInput() just reads a byte from the InputStream.

  2. I used the SerialPortEventListener from the RXTX library. Everytime something happens on the serial port, a SerialPortEvent is fired and I read the data in a similar way as in the first method.

For debugging reasons I added several System.out.println() calls to show what exactly is read by the threads and it seems that I am missing nearly the half of all data frames sended via the InputStream.

My question now is: What is the best way to read such a protocol from an InputStream? In my opinion I have to get my reading efforts synchronized with the data from the serial port, but I have no idea how to do that in Java.

  • 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-13T20:24:21+00:00Added an answer on June 13, 2026 at 8:24 pm

    If you are using RXTX you can set a listener which will notify you asyncronously when data is available:

    serial.notifyOnDataAvailable(true);
    serial.addEventListener(this);
    
    public void serialEvent(final SerialPortEvent arg0) {
        try {
            if (arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                //read from the input stream;
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    

    In general, when you read from the input stream you should abstract the concept of “packet of data” and not use the “available” function – just read enough from the packet to be able to determine its length, and then just use the “read(byte[])” to read a full packet into a buffer. (on a more advanced note, the “read” can read fewer bytes than needed so it should be used in a loop).

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

Sidebar

Related Questions

In java I need to read a binary file from a site and write
I try to write and read object of class into and from binary file
i try to read Data from an Oracle-Database. The problem is that in some
I try to read out all existing calendars. I tried the example from here:
I see that when I try to read the value from a textarea field
I get a warning in MSVC++ when I try to read an integer from
I have to read image binary from database and save this image binary as
I am trying to read a binary file into an array of structure struct
I'm quite new to java programming, but I'll try to use the correct terms
I am trying to read binary from database and write as a file in

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.