我正在尝试使用FTDI USB到串行驱动程序/库d2xx进行MODBUS RTU通信。我可以看到从我的电脑上的安卓应用程序发送的消息,我的台式电脑确实向我发送了消息。我成功获得了所需的字节数,但我从 FT_Device.read(( 方法接收到所有"0"。
任何帮助将不胜感激。
我已经写了安卓代码...
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);
}
});
}
}
实际上,我正在使用Android SDK 28(即Android 9(编译该项目,并且我的手机和桌子上都有Android 6。后来我在我朋友的手机上安装了它,它就像魅力一样工作。