GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2005, 08:01
Moooey Moooey is offline
New Member
 
Join Date: Mar 2005
Posts: 2
Moooey is on a distinguished road

Need Help reading HEX data from a sensor


Hello, I need some help writing this code for this little project I have. I am working with a DMU accelerometer sensor and I am reading data from a serial port. The sensor sends data in a 24 byte packet. The program that I wrote is in C and currently I am able to open the serial port and read the data in HEX and display to the screen. I am currently reading the data one byte at a time.

I am storing the data into a INT array. The problem I am having is that byte 0 and byte 1 is the header, the next two bytes represent the data value that I need to parse and so forth for 24 bytes. If I read 1 byte at a time how do I combine the two bytes into the correct data that I need?

I also tried reading two bytes at a time and when I do that the unit sends the MSB first and then the LSB. For example, the unit will always send the header AA55 as the first two bytes. When I store the data in the array it is storing as 55AA.

So my dilema is if I read one byte at a time how do I combine the first two bytes into the data value that I need or if I read two bytes at a time how do I reverse the bits in the correct order? Thanks in advance....
  #2  
Old 09-Mar-2005, 12:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Moooey
So my dilema is if I read one byte at a time how do I combine the first two bytes into the data value that I need or if I read two bytes at a time how do I reverse the bits in the correct order? Thanks in advance....


Just combine by bit shifting, two bytes at a time to give the int. In this program, the array input_bytes[] represents the bytes as they are received (you can actually store the bytes in an array of char, or you can simply process pairs of bytes as they are collected). I did it for eight bytes, you can do it for as many as you have.

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  /* simulated received bytes: one at a time */
  unsigned char input_bytes[8] = {1, 2, 3, 4, 5, 6, 7, 8};

  int saved_ints[4];

  int i, j;

  j = 0;
  for (i = 0; i < 8; i+= 2) {

    printf("Bytes %d and %d: 0x%02x, 0x%02x\n", 
            i, i+1, input_bytes[i], input_bytes[i+1]);

    saved_ints[j] = (input_bytes[i]<< 8) | (input_bytes[i+1]);

    j++;
  }

  printf("\n\n");

  printf("Here are the saved integers:\n");

  for (i = 0; i < 4; i++) {
    printf("%d: 0x%04x(hex), %d (decimal)\n", i, saved_ints[i], saved_ints[i]);
  }
  return 0;
}

Regards,

Dave
  #3  
Old 09-Mar-2005, 12:52
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
You can store data in a Single byte array and then just do some bit shifting operations (like dave has already explained in his post). Just to simplify.

CPP / C++ / C Code:

unsigned short int header;    /*16-bit (2 bytes) header*/
unsigned char data[24];      /*24 bytes array*/
int i=0;

/* Do some I/O, get data in data array*/

/*Extract Header*/
header = (0xFFFF & data[i++]) << 8;   /*data[0] in MSB*/
header = (0xFFFF & data[i++]);          /*data[1] in LSB*/

/*Process remaining data*/
........

  #4  
Old 09-Mar-2005, 13:18
Moooey Moooey is offline
New Member
 
Join Date: Mar 2005
Posts: 2
Moooey is on a distinguished road

Thanks


Quote:
Originally Posted by davekw7x
Just combine by bit shifting, two bytes at a time to give the int. In this program, the array input_bytes[] represents the bytes as they are received (you can actually store the bytes in an array of char, or you can simply process pairs of bytes as they are collected). I did it for eight bytes, you can do it for as many as you have.

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  /* simulated received bytes: one at a time */
  unsigned char input_bytes[8] = {1, 2, 3, 4, 5, 6, 7, 8};

  int saved_ints[4];

  int i, j;

  j = 0;
  for (i = 0; i < 8; i+= 2) {

    printf("Bytes %d and %d: 0x%02x, 0x%02x\n", 
            i, i+1, input_bytes[i], input_bytes[i+1]);

    saved_ints[j] = (input_bytes[i]<< 8) | (input_bytes[i+1]);

    j++;
  }

  printf("\n\n");

  printf("Here are the saved integers:\n");

  for (i = 0; i < 4; i++) {
    printf("%d: 0x%04x(hex), %d (decimal)\n", i, saved_ints[i], saved_ints[i]);
  }
  return 0;
}

Regards,

Dave


Thanks Dave I will try it out. I noticed that you used unsigned char, I forgot to mention that the data packet is in 2's complment format. I guess my first step is to have everything parsed correctly and worry about the sign later.
  #5  
Old 09-Mar-2005, 13:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Moooey
Thanks Dave I will try it out. I noticed that you used unsigned char, I forgot to mention that the data packet is in 2's complment format. I guess my first step is to have everything parsed correctly and worry about the sign later.


Right: First of all, get the bytes into proper relationship with each other.

Bytes are bytes; unsigned --- signed --- whatever. Notice that I performed logic, not arithmetic.

Left shift operations and logical operations are the same, regardless of whether they are signed or unsigned.

Now after you have the two bytes together, if the ints in your system are two bytes long, you are through.

If the ints are longer than two bytes, you have to do only a little more work to get the sign extended from the most-significant bit of the first byte all the way into the upper bits. (Left to the motivated student as an exercise.)

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Mrs stacy12 C Programming Language 14 05-Feb-2005 18:02
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
reading a char* into struct data spike666 C Programming Language 7 19-Apr-2004 12:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.