Serial data mixed variables to array

edited June 2016 in How To...

Hi.

I´m reading serial data. Package is 81 bytes long. I´m saving it to and array and it works as such.

Problem is that the stream consists data of 16-bit integers and 8-bit chars mixed. So the data gets mixed in the array.

Data stream:

byte 0 = (int) first 8-bits information 1 -> goes to array[0]

byte 1 = second 8-bits information 1 -> goes to array[0]

byte 2 = (char) 8-bits information 2 -> goes to array[1]

byte 3 = (int) first 8-bits information 3 -> half goes to array [1] and rest array[2]

byte 4 = second 8-bits information 3 -> half goes to array [2] and rest array[3]

byte 5 = (char) 8-bits information 4 -> goes to array [3]


So when I read array[1] it would have combined values of two information.

I can´t do nothing about the send protocol.

So what is the best way to get the information so it don´t get mixed?

Tagged:

Answers

  • Answer ✓

    Bit shifting!

    Example

    Array[0]=8 bits

    Last 4 bits

    Char chr0last = array[0] & 0x0f;

    First 4 bits

    Char chr0first = (array[0] &  0xf0)  >>4;
    

    So now array[0]= (chr0first <<4) + chr0last;

    This illustrates the concept of extracting group of bits. I used hex as it is customary. Last line I show how to merge group of bits to get new values (in this case I am restoring the. Orginal valuel)

    I hope this helps,

    Kf

  • Thanks kfrajer. Bit shifting works! Did a simpler version to make sense of it.

    int val = 46538; //46538=1011010111001010
    int a; //first 8 bits=10110101=181 
    int b; //last 8 bits=11001010=202
    
    a = val >> 8; //= 000000010110101
    println("Should get 181 ->"+a);
    
    
    // & (bitwise AND)      
    //Val=  1011010111001010
    //0xff= 0000000011111111
    //    = 0000000011001010
    b = val & 0xff;                   
    println("Should get 202 ->"+b);  
    
  • edited June 2016
    // forum.Processing.org/two/discussion/16973/serial-data-mixed-variables-to-array
    // GoToLoop (2016-Jun-03)
    
    static final byte[] DATAGRAM = { 1, 2, 2, 1, 1, 1, 2 };
    static final int SUM;
    
    byte[] received = { 50, -120, 70, 43, -1, 95, -31, 100, -64, 1 };
    final short[] converted = new short[DATAGRAM.length];
    
    static {
      int sum = 0;
      for (final byte b : DATAGRAM) {
      assert b == 1 | b == 2 :
        b + " is invalid # of bytes!";
    
        sum += b;
      }
      SUM = sum;
    }
    
    void setup() {
      println(received.length, converted.length, SUM, ENTER);
    
    assert SUM == received.length :
      SUM + " isn't " + received.length;
    
      byteToShortArr(received, converted, DATAGRAM);
    
      int idx = 0;
      for (final short sh : converted)  println(idx++, sh);
    
      exit();
    }
    
    static final short[] byteToShortArr(byte[] src, short[] tgt, byte... dgr) {
      if (src == null || dgr == null)  return tgt;
      if (tgt == null || tgt.length < dgr.length)  tgt = new short[dgr.length];
    
      int srcIdx = 0, tgtIdx = 0;
    
      for (final byte d : dgr) {
        final short s = src[srcIdx++];
        tgt[tgtIdx++] = (short) (d == 1? s & 0xFF : s<<010 | src[srcIdx++]);
      }
    
      return tgt;
    }
    
  • Thanks GoToLoop but your code is bit out of my talent range yet. But I will get back to it later.

Sign In or Register to comment.