Reputation: 27
Does anyone know what the issue would be with my code below?
I'm trying to create an Android application that can access an external sensor and display the data that is being read from the device. I have managed to get back an array of bytes, but I'm not sure how this translates to the integer I was hoping to read.
I decided to start by creating a java application on Windows, to try and get a better understanding of the problem, before I create an Android application.
I feel like the project is set up correctly, I've imported the ftdichip.win32 and ftdichip.ftd2xx libraries, and I think that I have managed to find the device.
However, I am unable to read the values that I hope the sensor is collecting. I am not sure if it's something to do with unsigned bytes?
I have included my code below and any help that can be offered would be greatly appreciated.
import jd2xx.JD2XX;
Above is the only necessary import (to my understanding?)
JD2XX jd = new JD2XX();
//list by Serial Number
Object[] devs = jd.listDevicesBySerialNumber();
for (int i = 0; i < devs.length; ++i) {
System.out.println(devs[i]);
}
//List by Description
devs = jd.listDevicesByDescription();
for (int i = 0; i < devs.length; ++i) {
System.out.println(devs[i]);
}
//List by location
devs = jd.listDevicesByLocation();
for (int i = 0; i < devs.length; ++i) {
System.out.println(
Integer.toHexString((Integer) devs[i]));
}
//open device
jd.open(0);
//configure the device
jd.setBaudRate(38400);
jd.setDataCharacteristics(
8, JD2XX.STOP_BITS_1, JD2XX.PARITY_NONE);
jd.setTimeouts(100, 100); //Set the time in ms between reads and writes (i think)
jd.setFlowControl(JD2XX.FLOW_NONE, 0, 0);
jd.setTimeouts(1000, 1000);
while (true) {
//try this
String msg = "So i can send it a message";
int ret = jd.write(msg.getBytes())
System.out.println(ret + "bytes sent.");
//read
int test = jd.read()
System.out.println("This is what is returned: " + test);
}
If anyone could explain the reason why I am not getting any values from the device you would be an absolute life saver.
Thank you very much.
Upvotes: 0
Views: 1298
Reputation: 31
You should check the queue first:
int status = ftDev.getQueueStatus();
byte[] data = new byte[(int) status];
long lngBytesReturned = ftDev.read(data,data.length);
I used the android d2xx library with the D2xxManager from the FTDI website.
Upvotes: 2