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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:56+00:00 2026-06-04T02:03:56+00:00

First, I’m a student still. So I am not very experienced. I’m working with

  • 0

First, I’m a student still. So I am not very experienced.

I’m working with a piece of bluetooth hardware and I am using its protocol to send it commands. The protocol requires packets to be sent with LSB first for each packet field.

I was getting error packets back to me indicating my CRC values were wrong so I did some investigating. I found the problem, but I became confused in the process.

Here is Some GDB output and other information elucidating my confusion.

I’m sending a packet that should look like this:

|Start Flag| Packet Num | Command | Payload |    CRC    | End Flag|
    0xfc        0x1       0x0 0x8   0x0 0x5   0x59 0x42    0xfd

Here is some GDB output:

print /x reqId_ep
$1 = {start_flag = 0xfc, data = {packet_num = 0x1, command = {0x0, 0x8}, payload = {
  0x0, 0x5}}, crc = 0x5942, end_flag = 0xfd}

reqId_ep is the variable name of the packet I’m sending. It looks all good there, but I am receiving the CRC error codes from it so something must be wrong.

Here I examine 9 bytes in hex starting from the address of my packet to send:

x/9bx 0x7fffffffdee0
0xfc    0x01    0x00    0x08    0x00    0x05    0x42    0x59    0xfd

And here the problem becomes apparent. The CRC is not LSB first. (0x42 0x59)

To fix my problem I removed the htons() that I set my CRC value equal with.

And here is the same output above without htons():

p/x reqId_ep

$1 = {start_flag = 0xfc, data = {packet_num = 0x1, command = {0x0, 0x8}, payload = {
  0x0, 0x5}}, crc = 0x4259, end_flag = 0xfd}

Here the CRC value is not LSB.

But then:

x/9bx 0x7fffffffdee0
0xfc    0x01    0x00    0x08    0x00    0x05    0x59    0x42    0xfd

Here the CRC value is LSB first.

So apparently the storing of C is LSB first? Can someone please cast a light of knowledge upon me for this situation? Thank you kindly.

  • 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-04T02:03:57+00:00Added an answer on June 4, 2026 at 2:03 am

    This has to do with Endianness in computing:
    http://en.wikipedia.org/wiki/Endianness#Endianness_and_operating_systems_on_architectures

    For example, the value 4660 (base-ten) is 0x1234 in hex. On a Big Endian system, it would be stored in memory as 1234 while on a Little Endian system it would be stored as 3412

    If you want to avoid this sort of issue in the future, it might just be easiest to create a large array or struct of unsigned char, and store individual values in it.

    eg:

    |Start Flag| Packet Num | Command | Payload |    CRC    | End Flag|
        0xfc        0x1       0x0 0x8   0x0 0x5   0x59 0x42    0xfd
    
    typedef struct packet {
      unsigned char startFlag;
      unsigned char packetNum;
      unsigned char commandMSB;
      unsigned char commandLSB;
      unsigned char payloadMSB;
      unsigned char payloadLSB;
      unsigned char crcMSB;
      unsigned char crcLSB;
      unsigned char endFlag;
    } packet_t;
    

    You could then create a function that you compile differently based on the type of system you are building for using preprocessor macros.

    eg:

    /* Uncomment the line below if you are using a little endian system;
    /* otherwise, leave it commented
     */
    //#define LITTLE_ENDIAN_SYSTEM
    
    // Function protocol
    void writeCommand(int cmd);
    
    
    //Function definition
    void writeCommand(int cmd, packet_t* pkt)
    {
      if(!pkt)
      {
         printf("Error, invalid pointer!");
         return;
      }
    
      #if LITTLE_ENDIAN_SYSTEM
        pkt->commandMSB = (cmd && 0xFF00) >> 8;
        pkt->commandLSB = (cmd && 0x00FF);
      # else  //  Big Endian system
        pkt->commandMSB = (cmd && 0x00FF);
        pkt->commandLSB = (cmd && 0xFF00) >> 8;
      #endif
    
      // Done
    }
    
    int main void() 
    {
      packet_t myPacket = {0};  //Initialize so it is zeroed out
      writeCommand(0x1234,&myPacket);
    
    
      return 0;
    }
    

    One final note: avoid sending structs as a stream of data, send it’s individual elements one-at-a-time instead! ie: don’t assume that the struct is stored internally in this case like a giant array of unsigned characters. There are things that the compiler and system put in place like packing and allignment, and the struct could actually be larger than 9 x sizeof(unsigned char).

    Good luck!

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

Sidebar

Related Questions

First, please tell me if I'm not allowed to ask about this protocol here...
First of all, I'm using KohanaPHP Framework. I've impletemented SWFUpload successfully, working quite nice.
First of all, I'm not sure if the title accurately describes what I'm referring
First some background: I'm working on an application and I'm trying to follow MVVM
First off, let me clarify the platforms we are using. We have an ASP.NET
first of all sorry for my bad english. I got a working PagerAdapter with
First the apologies, i'm not sure if my question title even accuratly explains what
We're building an app, our first using Rails 3, and we're having to build
first of all, I'm not good with jQuery and coding. I'm trying to achieve
First, let's get the security considerations out of the way. I'm using simple authentication

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.