Shop OBEX P1 Docs P2 Docs Learn Events
newbie question :Data conversion — Parallax Forums

newbie question :Data conversion

RacoonRacoon Posts: 15
edited 2009-07-23 13:30 in General Discussion
Hi all,

I need your help again.

I should get data like FF FF 02 0C 0D 05 01. This consider as one package of data. however after I connect my sensor to the javelin stamp, I can only get the very funny character like     ?  ?.

Should I do any data conversion that can convert the hex string to binary number?

Can any one help me about this, thank you very much.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The truth in physics is changed with time.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-21 12:16
    Do you expect
    FF FF 02 0C 0D 05 01
    as characters (eg. 'F','F','F','F','0'.....)
    or as bytevalues (0xFF,0xFF,0x02,0x0C....)

    The bytevalues cannot be simply sent to the javelin IDE message window.
    They need to be converted to printable ascii characters.
    The easiest way is to use Format.

    while (true) {
    · int c = uartRx.receiveByte() & 0xFF;
    ··Format.printf("%02x ",c); //print bytevalues as hexcodes
    }

    regards peter

    ··
  • RacoonRacoon Posts: 15
    edited 2009-07-22 03:00
    Thank you peter.

    Actually I expect the bytevalues.

    After I put your program inside, the IDE indicated that "Format" is either a misplaced package name or non-existing entity. I think the problem is that I only import stamp.core.*; which package do I need to import?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The truth in physics is changed with time.
  • RacoonRacoon Posts: 15
    edited 2009-07-22 04:03
    Thank you very much peter,

    After I downloaded the utility library, the whole program works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The truth in physics is changed with time.
  • RacoonRacoon Posts: 15
    edited 2009-07-22 05:30
    After I get the 8 byte data like FF FF 02 0C 0D 05 01 02, how can I extract the data from any byte. for example I want to extract the data from 3rd byte and 4th byte. the first byte data will be FF.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The truth in physics is changed with time.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-22 06:27
    Assuming your block of data always starts with 0xFF,0xFF and has length 8

    int c;
    char[noparse]/noparse buf = new char[noparse][[/noparse]6];

    //wait until 0xFF
    while (true) {
    ··c = uartRx.receiveByte();
    · if (c == 0xFF) break;
    }
    //wait·while 0xFF
    while (true) {
    ··c = uartRx.receiveByte();
    · if (c != 0xFF) break;
    }
    buf[noparse][[/noparse]0] = (char)c;
    //at this point collect your remaining 5byte data
    for (int i=1; i<6; i++) {
    · bufi] = (char)uartRx.receiveByte();
    }
    //assembly 2 bytes into int (LSB first)
    int value = (buf[noparse][[/noparse]0] & 0xFF) | (buf[noparse][[/noparse]1]<<8);


    regards peter
  • RacoonRacoon Posts: 15
    edited 2009-07-22 09:26
    Thank you peter.

    Currently I modified my code like below. Like if the data is FF FF 01 C8 D2 05 07 A4, I will be able to get a final data like 05D2 together, is my below code is a correct method to do it?

    One more question will be how can I covert the 05D2 into decimal value and divide them by 100.

    Thank you



    import stamp.core.*;
    import stamp.util.text.*;

    public class gyro {

    final static int LED = CPU.pin6;

    final static int TX = CPU.pin3;

    final static int RX = CPU.pin4;
    static Uart txUart = new Uart( Uart.dirTransmit, TX, Uart.dontInvert, Uart.speed38400,Uart.stop1 );
    static Uart rxUart = new Uart( Uart.dirReceive, RX, Uart.dontInvert, Uart.speed38400,Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(128);
    static int c,value,value1,angle,data;
    static char[noparse]/noparse buf = new char[noparse][[/noparse]8];



    static void bufferMessage(){

    if(rxUart.byteAvailable()){
    c = rxUart.receiveByte()& 0xFF;
    buffer.append(c);
    }
    }

    static void counter(){
    buffer.clear();
    bufferMessage();

    }

    static void convert(){
    for (int i=0; i<8; i++) {
    counter();
    buf = (char)c;
    if (i==5){
    value=c;
    }
    else if(i==6) {
    value1=c;
    angle = (value) | (value1)<<8;

    }

    }

    public static void main(){



    convert();


    Format.printf("%02x ",angle);


    }

    }

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The truth in physics is changed with time.

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-22 17:07
    The received bytes are bytevalues, so an assembly is just an int
    that you can divide by 100.
    Just use %d to display them as decimal.

    angle = (value) | (value1)<<8; //your assembled value

    Format.printf("angle = %d",angle/100); //integer part
    Format.printf(".%02d\n",angle%100); //fractional part .00 to .99

    regards peter
  • RacoonRacoon Posts: 15
    edited 2009-07-23 01:23
    Thank you very much peter.

    Btw, have you ever applied kalman filter in any program to minimize the dynamic error?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The truth in physics is changed with time.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-23 13:30
    No.

    regards peter
Sign In or Register to comment.