2

I'm trying to get serial data and convert it to Float. The data is passed from MCU to FTDI chip and to my android device. As i get the values of 0xbf700000(-0.9375),0x3dbc01a3(0.0918),0x3e9f9724(0.3117) my java code change it to float but not as float should be, it takes each byte and change it, not 4 bytes as float should be.

the code :

package com.application.i;



import jp.ksksue.driver.serial.FTDriver;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;





public class FTDI extends Activity{

    // [FTDriver] Object
    FTDriver mSerial;

    // [FTDriver] Permission String
    private static final String ACTION_USB_PERMISSION =
            "jp.ksksue.tutorial.USB_PERMISSION";

    Button btnRead;
    TextView Monitor;
    TextView indicator;
    StringBuffer mText = new StringBuffer();
    StringBuffer Data = new StringBuffer();
    String places=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ftdisub);
    btnRead = (Button) findViewById(R.id.btnRead);
    Monitor = (TextView)findViewById(R.id.onitor);
    indicator = (TextView)findViewById(R.id.indicator);
    // [FTDriver] Create Instance
    mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));


    // [FTDriver] setPermissionIntent() before begin()
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
            ACTION_USB_PERMISSION), 0);
    mSerial.setPermissionIntent(permissionIntent);
    // [FTDriver] Open USB Serial
    if(mSerial.begin(FTDriver.BAUD115200)) {

        btnRead.setEnabled(true);


        Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
    }


    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.ftdisub, menu);
//        return true;
//   }
//   

    @Override
    public void onDestroy() {
        super.onDestroy();

        // [FTDriver] Close USB Serial
        mSerial.end();
    }

    public void onReadClick(View view) {

        int i,len;

        // [FTDriver] Create Read Buffer
        byte[] rbuf = new byte[12]; // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte

        // [FTDriver] Read from USB Serial
        len = mSerial.read(rbuf);

        for(i=0; i<len; i++) {

            if ((char) rbuf[i] == 0x23){
                Data.append((int) i+1);
                Data.append((char) 0x23);
            }
            mText.append((float)rbuf[i]);



        }

        mText.append((char) 0x2c);
        indicator.setText(Data);
        Monitor.setText(mText);

    }






}

the output :

enter image description here

On C coding I will use a float pointer and this pointer is 4 byte long, that will make a successful Hex to float. My question is how do i use an equivalent float pointer like C on Java?

Digol
  • 390
  • 1
  • 15

1 Answers1

2

There is no such thing in Java as float pointer. That what you use in the method definition - it tells Java how to treat provided reference - you can consider it as a pointer. Java assumes that at this particular address will be data which Java will interpret as float.

One more point which you should consider when you work with different processors - Endianness - I do not sure what your processors use but you have to be sure and double check is it Big-endian or Little-endian on both sides.

Alex
  • 4,457
  • 2
  • 20
  • 59
  • Alex, can i change byte to string? "bf" as a byte 10111111 to string "bf"? – Digol May 10 '15 at 19:20
  • Sure you can. Here is explanation http://stackoverflow.com/questions/12310017/how-to-convert-a-byte-to-its-binary-string-representation – Alex May 10 '15 at 19:26