serialVisualization/serialtest_receiver_processing/serialtest_receiver_process...

52 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

import processing.serial.*;
Serial serialport;
void setup() {
printArray(Serial.list());
serialport = new Serial(this, Serial.list()[2], 9600);
println("start");
}
void draw() {
byte[] inBuffer = new byte[9];
while(serialport.available()>0) {
inBuffer = serialport.readBytes();
serialport.readBytes(inBuffer);
if (inBuffer!= null) {
int counter= inBuffer[0];
int value1= extract_uint16_t(inBuffer,1);
int value2= extract_int16_t(inBuffer,3);
float floatvalue = extract_float(inBuffer,5);
println("counter="+counter);
println("value1="+value1);
println("value2="+value2);
println("floatvalue="+floatvalue);
}
}
}
public int extract_uint16_t(byte array[], int startbyte) {
return ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0xff)<<8;
}
public int extract_int16_t(byte array[], int startbyte) {
if ( ((int)array[startbyte+1] & 0x80) == 0x00 ) { //2's complement, not negative
return ( ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0x7f)<<8 );
}else{ //value is negative
return -32768 + ( ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0x7f)<<8 );
}
}
public float extract_float(byte array[], int startbyte) {
int MASK = 0xff;
int bits = 0;
int i = startbyte+3; //+3 because goes backwards and has 4 bytes
for (int shifter = 3; shifter >= 0; shifter--) {
bits |= ((int) array[i] & MASK) << (shifter * 8);
i--;
}
return Float.intBitsToFloat(bits);
}