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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:27:23+00:00 2026-05-30T13:27:23+00:00

So I am trying to define a communication protocol for serial communication, I want

  • 0

So I am trying to define a communication protocol for serial communication, I want to be able to send 4 byte numbers to the device, but I’m unsure how to make sure that the device starts to pick it up on the right byte.

For instance if I want to send

0x1234abcd 0xabcd3f56 ...

how do I makes sure that the device doesn’t start reading at the wrong spot and get the first word as:

0xabcdabcd

Is there a clever way of doing this? I thought of using a marker for the start of a message, but what if I want to send the number I choose as data?

  • 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-30T13:27:24+00:00Added an answer on May 30, 2026 at 1:27 pm

    Why not send a start-of-message byte followed by a length-of-data byte if you know how big the data is going to be?

    Alternatively, do as other binary protocols and only send fixed sizes of packages with a fixed header. Say that you will only send 4 bytes, then you know that you’ll have one or more bytes of header before the actual data content.

    Edit: I think you’re misunderstanding me. What I mean is that the client is supposed to always regard bytes as either header or data, not based on value but rather based on the position in the stream. Say you’re sending four bytes of data, then one byte would be the header byte.

    +-+-+-+-+-+
    |H|D|D|D|D|
    +-+-+-+-+-+
    

    The client would then be a pretty basic state machine, along the lines of:

    int state = READ_HEADER;
    int nDataBytesRead = 0;
    while (true) {
      byte read = readInput();
      if (state == READ_HEADER) {
        // process the byte as a header byte
        state = READ_DATA;
        nDataBytesRead = 0;
      } else {
        // Process the byte as incoming data
        ++nDataBytesRead;
        if (nDataBytesRead == 4) 
        {
          state = READ_HEADER;
        }
      }
    } 
    

    The thing about this setup is that what determines if the byte is a header byte is not the actual content of a byte, but rather the position in the stream. If you want to have a variable number of data bytes, add another byte to the header to indicate the number of data bytes following it. This way, it will not matter if you are sending the same value as the header in the data stream since your client will never interpret it as anything but data.

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

Sidebar

Related Questions

I'm trying to define an infix operator but I keep getting errors. I'm using
I'm trying to communicate to a device using RS-485 through the serial port. Everything
I'm trying to define a custom formatter for my class, but I can't find
I was trying to: #define CommonAppData {commonappdata} but it yields: Compiler Error [ISPP] Expression
I'm trying to define the inheritance-check predicate is_a/2 in Prolog, but so far all
I am trying to define a scope in my Account model but it is
Im trying to define an array of arrays as a Constant in one of
Im trying to define different ld and c flags for different arch. im using
I'm trying to create a two-way communication between parent and child processes using 2
I am trying to define two conditions for a WHERE clause: WHERE Date =

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.