Problem Reading Data using FTDI D2xx library

837 views Asked by At

I am trying for MODBUS RTU communication using FTDI usb to serial driver / library d2xx. I can see the message being sent from my android app on my pc and my desktop pc does sends me the message back. I am successfully getting required amount of bytes but i am receiving all "0" from FT_Device.read() method.

Any help would be of greatly appreciated.

I have written android code...

package com.example.ftdilib;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdi.j2xx.D2xxManager;
import com.ftdi.j2xx.FT_Device;

import java.sql.Driver;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private TextView tv;

    private Context mMe;
    private D2xxManager mManager;
    private FT_Device mDevice;
    private D2xxManager.DriverParameters mDriverParameters;

    private Handler mHandler = new Handler();
    private Runnable mRunner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.btn);
        tv = findViewById(R.id.tv);

        mMe = getApplicationContext();

        try {
            mManager = D2xxManager.getInstance(mMe);
        } catch (D2xxManager.D2xxException e) {
            Toast.makeText(mMe, e.getMessage(), Toast.LENGTH_LONG);
        }

        initButton();
    }

    private void initButton() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                if(mManager != null)
                {
                    int devCount = mManager.createDeviceInfoList(mMe);
                    if(devCount > 0)
                    {
                        mDriverParameters = new D2xxManager.DriverParameters();
                        mDriverParameters.setBufferNumber(2);
                        mDriverParameters.setMaxTransferSize(1024);
                        mDriverParameters.setReadTimeout(250);

                        mDevice = mManager.openByIndex(mMe, 0, mDriverParameters);

                        if(mDevice != null)
                        {
                            if(mDevice.isOpen())
                            {
                                mDevice.setBaudRate(9600);
                                mDevice.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8, D2xxManager.FT_STOP_BITS_1, D2xxManager.FT_PARITY_NONE);
                                mDevice.restartInTask();

                                mRunner = new Runnable() {
                                    @Override
                                    public void run() {
                                        getData();
                                        mHandler.postDelayed(mRunner, 50);
                                    }
                                };

                                mHandler.post(mRunner);
                            }
                        }
                    }
                    else
                    {
                        Toast.makeText(mMe, "NO DEVICE FOUND.", Toast.LENGTH_LONG);
                    }
                }
            }
        });
    }

    private void getData()
    {
        byte[] _req =  getHoldingRegisters((byte)1,(int)0, (int)4);
    }

    private byte[] getHoldingRegisters(byte slaveid, int startingaddress, int quantity)
    {
        byte[] _msg = new byte[8];
        _msg[0] = slaveid;
        _msg[1] = 3;
        _msg[2] = (byte) (startingaddress >> 8);
        _msg[3] = (byte) (startingaddress & 0xFF);
        _msg[4] = (byte) (quantity >> 8);
        _msg[5] = (byte) (quantity & 0xFF);
        int crc = getCRC(_msg);

        _msg[6] = (byte) (crc & 0xFF);
        _msg[7] = (byte) (crc >> 8);

        mDevice.write(_msg, _msg.length);

        byte[] _req = new byte[5 + (quantity*2)];

        int available = mDevice.getQueueStatus();

        setText(tv, String.valueOf(available));

        mDevice.read(_req, _req.length, 250);

        return null;
    }

    private int getCRC(byte[] input) {
        int iPos = 0; // loop position in input buffer
        int crc = 0xFFFF;
        while (iPos < input.length - 2) {
            crc ^= (input[iPos] & 0xFF);
            iPos++;
            for (int j = 0; j < 8; j++) {
                boolean bitOne = ((crc & 0x1) == 0x1);
                crc >>>= 1;
                if (bitOne) {
                    crc ^= 0x0000A001;
                }
            }
        }

        return crc;
    }

    private void setText(final TextView textView, final String value)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(value);
            }
        });
    }
}
1

There are 1 answers

1
Nimit Vachhani On BEST ANSWER

Actually i was compilling the project using Android SDK 28 i.e. for Android 9 and i was having Android 6 on my mobile and table. Later i installed it in Android 9 on my friend's mobile and it worked like charm..