newbie question :Data conversion
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.
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
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
··
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.
After I downloaded the utility library, the whole program works.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The truth in physics is changed with time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The truth in physics is changed with time.
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
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.
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
Btw, have you ever applied kalman filter in any program to minimize the dynamic error?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The truth in physics is changed with time.
regards peter