Hi,
I'm trying to get an image from ucamII using a ioio UART connection.
here is the doc of the ucamII:
http://www.robotshop.com/media/files/pdf/datasheet-ucam-ii.pdf
What I'm tying todo is Presented in section 7.4 page 16/22 of the pdf
but I want to Get a JPEG picture with 640*480
In the UARTController.java you have the code that i use like presented here
after:
The problem is that I always have the message:
UART Read PICTACK OK
UART Read TimeoutException
UART Read Get Image Size FAILED
I do not know if i should read differently:
for exemple wait some bytes to start the read like I do in waitReference.
or should i set the baudrate and not use the automatic detection.
If you have a proposition I will try it.
THANKS IN Advance
==============================================
// this is just For Logging
class CamController extends UARTController {
@Override
public void log(String msg) {
final String theMsg = msg;
runOnUiThread(new Runnable() {
@Override
public void run() {
textLogger_.append(theMsg);
scrollTextLogger_.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
// at setup
camIoioRxPIN30 = 30;
camIoioTxPIN29 = 29;
Integer baud = 115200; // 115200; // after that try: ucamII
autodetect 921600 or set 3686400;
CamController camCom = new CamController();
camCom.connect(ioio_, camIoioRxPIN30, camIoioTxPIN29, baud);
boolean syncOK = camCom.camSYNC();
if (syncOK) {
boolean initOK = camCom.camINITJPEG();
if (initOK) {
boolean setPKGSizeOK = camCom.camSetPKGSize();
if (setPKGSizeOK) {
final byte[] data = camCom.camGetFrame();
if (data.length != 0) {
this.logOnUI("GET FRAME OK\n");
runOnUiThread(new Runnable() {
@Override
public void run() {
// VideoView
//Bitmap bitmap =
BitmapFactory.decodeByteArray(data, 0, dataSize);
//videoViewCam_.setImageBitmap(bitmap);
//textLogger_.append("bitmap.getByteCount()
=> " + bitmap.getByteCount() + "\n");
//textLogger_.append("bitmap.getHeight()
=> " + bitmap.getHeight() + "\n");
//textLogger_.append("bitmap.getWidth()
=> " + bitmap.getWidth() + "\n");
textLogger_.append("data.length => " +
data.length + "\n");
}
});
}
}
}
}
--
You received this message because you are subscribed to the Google Groups
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.
package ioioquickstart.android;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import ioio.lib.api.IOIO;
import ioio.lib.api.Uart;
import ioio.lib.api.exception.ConnectionLostException;
/**
* Created by User on 21/03/2015.
*/
public abstract class UARTController {
public int readTimeout = 100;
// SYNC = AA 0D 00 00 00 00;
public byte[] _bufferSYNC = new byte[6];
// ACK = AA 0E 0D xx 00 00
public byte[] _bufferACK = new byte[6];
// -> INITIAL JPEG, VGA = AA 01 00 07 yy 07 // in example yy = 07
public byte[] _bufferINIT = new byte[6];
// <- ACK = AA 0E 01 xx 00 00
public byte[] _bufferINITACK = new byte[6];
// -> SET PACKAGE SIZE 512 Bytes = AA 06 08 00 02 00
public byte[] _bufferPKGSize = new byte[6];
// <- ACK = AA 0E 06 xx 00 00
public byte[] _bufferPKGSizeACK = new byte[6];
// -> GET PICTURE JPEG picture = AA 04 05 00 00 00
public byte[] _bufferPICT = new byte[6];
// <- ACK = AA 0E 04 xx 00 00
public byte[] _bufferPICTACK = new byte[6];
// IOIO
public IOIO _ioio;
// Baud
public Integer _baud;
// ioioRxPIN
public Integer _ioioRxPIN;
// ioioTxPIN
public Integer _ioioTxPIN;
// uart
public Uart _uart;
// uart
public InputStream _uartIn;
// uart
public OutputStream _uartOut;
private class UARTMessage {
//
public byte[] buffer;
/**
*
* @param initBuffer
*/
public UARTMessage(byte[] initBuffer) {
buffer = initBuffer;
}
}
/**
* UARTRead
*/
private class UARTRead extends AsyncTask<InputStream, Void, Boolean> {
public UARTMessage _inBuffer;
public int _offset;
public int _nbOfBytesToRead = 6;
public int _nbOfBytesRead = 0;
public int _nbOfTriesBeforeRead = 0;
public boolean _iOExceptionRaised = false;
public boolean _InterruptedExceptionRaised = false;
public UARTRead(UARTMessage inBuffer, int offset, int nbOfBytesToRead) {
super();
_inBuffer = inBuffer;
_offset = offset;
_nbOfBytesToRead = nbOfBytesToRead;
}
/**
* The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute()
* @param uartCamIn
* @return
*/
protected Boolean doInBackground(InputStream... uartCamIn) {
try {
_nbOfBytesRead = uartCamIn[0].read(_inBuffer.buffer, _offset, _nbOfBytesToRead); // blocking
} catch (final IOException ex) {
_iOExceptionRaised = true;
return false;
}
return true;
}
}
/**
* UARTWaitReference
*/
private class UARTWaitReference extends AsyncTask<InputStream, Void, Boolean> {
public byte[] _reference;
private int _ignoredByteIndex;
public boolean _iOExceptionRaised = false;
public UARTWaitReference(byte[] reference, int ignoredByteIndex) {
super();
_reference = reference;
_ignoredByteIndex = ignoredByteIndex;
}
/**
* The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute()
* @param uartCamIn
* @return
*/
protected Boolean doInBackground(InputStream... uartCamIn) {
try {
int referenceIndex = 0;
int readingIndex = 0;
byte[] inByte = new byte[1];
while (uartCamIn[0].available() != 0) {
int nbOfBytesRead = uartCamIn[0].read(inByte, 0, 1); // blocking
if (referenceIndex != _ignoredByteIndex) {
if (inByte[0] == _reference[referenceIndex]) {
referenceIndex++;
if (referenceIndex == 6) {
return true;
}
}
} else {
referenceIndex++;
}
}
} catch (final IOException ex) {
_iOExceptionRaised = true;
return false;
}
return true;
}
}
/**
* Constructor
*/
public UARTController() {
initBuffers();
}
/**
* @param ioio
* @param ioioRxPIN
* @param ioioTxPIN
* @param baud
* @throws ConnectionLostException
*/
public void connect(IOIO ioio, Integer ioioRxPIN, Integer ioioTxPIN, Integer baud) throws ConnectionLostException {
_ioio = ioio;
_ioioRxPIN = ioioRxPIN;
_ioioTxPIN = ioioTxPIN;
_baud = baud; // after that try: ucamII autodetect 921600 or set 3686400;
_uart = _ioio.openUart(_ioioRxPIN, _ioioTxPIN, _baud, Uart.Parity.NONE, Uart.StopBits.ONE);
_uartIn = _uart.getInputStream();
//_uartIn.available()
_uartOut = _uart.getOutputStream();
}
/**
*
*/
private void initBuffers() {
_bufferSYNC[0] = (byte) 0xAA;
_bufferSYNC[1] = (byte) 0x0D;
_bufferSYNC[2] = (byte) 0x00;
_bufferSYNC[3] = (byte) 0x00;
_bufferSYNC[4] = (byte) 0x00;
_bufferSYNC[5] = (byte) 0x00;
_bufferACK[0] = (byte) 0xAA;
_bufferACK[1] = (byte) 0x0E;
_bufferACK[2] = (byte) 0x0D;
_bufferACK[3] = (byte) 0x00;
_bufferACK[4] = (byte) 0x00;
_bufferACK[5] = (byte) 0x00;
_bufferINIT[0] = (byte) 0xAA;
_bufferINIT[1] = (byte) 0x01;
_bufferINIT[2] = (byte) 0x00;
_bufferINIT[3] = (byte) 0x07;//0x07;//0x06;
_bufferINIT[4] = (byte) 0x07;//0x07;//0x09;
_bufferINIT[5] = (byte) 0x07;//0x07;//0x00;
_bufferINITACK[0] = (byte) 0xAA;
_bufferINITACK[1] = (byte) 0x0E;
_bufferINITACK[2] = (byte) 0x01;
_bufferINITACK[3] = (byte) 0x00;
_bufferINITACK[4] = (byte) 0x00;
_bufferINITACK[5] = (byte) 0x00;
_bufferPKGSize[0] = (byte) 0xAA;
_bufferPKGSize[1] = (byte) 0x06;
_bufferPKGSize[2] = (byte) 0x08;
_bufferPKGSize[3] = (byte) 0x00;
_bufferPKGSize[4] = (byte) 0x02;
_bufferPKGSize[5] = (byte) 0x00;
_bufferPKGSizeACK[0] = (byte) 0xAA;
_bufferPKGSizeACK[1] = (byte) 0x0E;
_bufferPKGSizeACK[2] = (byte) 0x06;
_bufferPKGSizeACK[3] = (byte) 0x00;
_bufferPKGSizeACK[4] = (byte) 0x00;
_bufferPKGSizeACK[5] = (byte) 0x00;
_bufferPICT[0] = (byte) 0xAA;
_bufferPICT[1] = (byte) 0x04;
_bufferPICT[2] = (byte) 0x05;
_bufferPICT[3] = (byte) 0x00;
_bufferPICT[4] = (byte) 0x00;
_bufferPICT[5] = (byte) 0x00;
_bufferPICTACK[0] = (byte) 0xAA;
_bufferPICTACK[1] = (byte) 0x0E;
_bufferPICTACK[2] = (byte) 0x04;
_bufferPICTACK[3] = (byte) 0x00;
_bufferPICTACK[4] = (byte) 0x00;
_bufferPICTACK[5] = (byte) 0x00;
}
/**
* Read and compare to reference buffer
* @param reference
* @param timeout
* @param notComparedByteIndex
* @return true if read ok and equal to reference
*/
public boolean waitReference(byte[] reference, int timeout, int notComparedByteIndex) {
boolean output = false;
UARTWaitReference uartWaitReference = new UARTWaitReference(reference, notComparedByteIndex);
uartWaitReference.execute(_uartIn);
try {
output = uartWaitReference.get(timeout, TimeUnit.MILLISECONDS);
} catch (CancellationException e) {
this.log("UART WaitReference CancellationException\n");
} catch (InterruptedException e) {
this.log("UART WaitReference InterruptedException\n");
} catch (ExecutionException e) {
this.log("UART WaitReference ExecutionException\n");
} catch (TimeoutException e) {
this.log("UART WaitReference TimeoutException\n");
}
return output;
}
/**
*
* @return
*/
public boolean camSYNC() {
int tries = 0;
boolean ackOK = false;
try {
do {
// -> SYNC = AA 0D 00 00 00 00;
_uartOut.write(_bufferSYNC);
// <- ACK = AA 0E 0D xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferACK, readTimeout, 3);
tries++;
} while (tries < 100 && !ackOK);
this.log("UART SYNC NUMBER OF TRIES:" + tries + "\n");
if (!ackOK) {
this.log("UART Read ACK FAILED\n");
return false;
} else {
this.log("UART Read ACK OK\n");
}
// <- SYNC = AA 0D 00 00 00 00;
//Thread.sleep(10);
boolean syncOK = false;
syncOK = waitReference(_bufferSYNC, readTimeout, 6); // 500 because we need to wait a bit longer
if (!syncOK) {
this.log("UART Read SYNC FAILED\n");
return false;
} else {
this.log("UART Read SYNC OK\n");
}
// -> ACK = AA 0E 0D xx 00 00
_uartOut.write(_bufferACK);
} catch (final IOException ex) {
this.log("UART WRITE IOException \n" + ex.toString() + "\n");
return false;
}/* catch (InterruptedException ex2) {
this.log("UART WRITE Thread InterruptedException \n" + ex2.toString() + "\n");
return false;
}*/
//
this.log("UART ACK SENT\n");
return true;
}
/**
*
* @return
*/
public boolean camINITJPEG() {
try {
int i = 0;
boolean ackOK = false;
/*
// -> SYNC = AA 0D 00 00 00 00;
_uartOut.write(_bufferSYNC);
// <- ACK = AA 0E 0D xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferACK, readTimeout, 3);
do {
*/
// -> INITIAL JPEG, VGA = AA 01 00 07 yy 07 // in example yy = 07
_uartOut.write(_bufferINIT);
// <- ACK = AA 0E 01 xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferINITACK, readTimeout, 3);
i++;
//} while(!ackOK && i < 100);
if (!ackOK) {
this.log("UART Read INITACK FAILED\n");
return false;
} else {
this.log("UART Read INITACK OK\n");
}
} catch (final IOException ex) {
this.log("UART WRITE IOException \n" + ex.toString() + "\n");
return false;
}/* catch (InterruptedException ex2) {
this.log("UART WRITE Thread InterruptedException \n" + ex2.toString() + "\n");
return false;
}*/
return true;
}
/**
*
* @return
*/
public boolean camSetPKGSize() {
try {
int i = 0;
boolean ackOK = false;
/*
// -> SYNC = AA 0D 00 00 00 00;
_uartOut.write(_bufferSYNC);
// <- ACK = AA 0E 0D xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferACK, readTimeout, 3);
do {
*/
// -> SET PACKAGE SIZE 512 Bytes = AA 06 08 00 02 00
_uartOut.write(_bufferPKGSize);
// <- ACK = AA 0E 06 xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferPKGSizeACK, readTimeout, 3);
i++;
//} while(!ackOK && i < 100);
if (!ackOK) {
this.log("UART Read PKGSizeACK FAILED\n");
return false;
} else {
this.log("UART Read PKGSizeACK OK\n");
}
} catch (final IOException ex) {
this.log("UART WRITE IOException \n" + ex.toString() + "\n");
return false;
}/* catch (InterruptedException ex2) {
this.log("UART WRITE Thread InterruptedException \n" + ex2.toString() + "\n");
return false;
}*/
return true;
}
/**
*
* @return
*/
public byte[] camGetFrame() {
byte[] outEmptyBytes = new byte[0];
int imageSize = 0;
try {
boolean ackOK = false;
/*
// -> SYNC = AA 0D 00 00 00 00;
_uartOut.write(_bufferSYNC);
// <- ACK = AA 0E 0D xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferACK, readTimeout, 3);
*/
//
// -> GET PICTURE JPEG picture = AA 04 05 00 00 00
_uartOut.write(_bufferPICT);
// <- ACK = AA 0E 04 xx 00 00
//Thread.sleep(10);
ackOK = waitReference(_bufferPICTACK, readTimeout, 3);
if (!ackOK) {
this.log("UART Read PICTACK FAILED\n");
return outEmptyBytes;
} else {
this.log("UART Read PICTACK OK\n");
}
//Thread.sleep(10);
// <- DATA JPEG picture = AA 0A 05 b0 b1 b2 // b2b1b0 = imageSize
boolean getImageSizeOK = false;
byte[] buffer6 = {0,0,0,0,0,0};
UARTMessage inBuffer = new UARTMessage(buffer6);
getImageSizeOK = readIn(inBuffer, 0, 6, readTimeout);
if (!getImageSizeOK) {
this.log("UART Read Get Image Size FAILED\n");
return outEmptyBytes;
} else {
this.log("UART Read Get Image Size OK\n");
}
// compute imageSize, nbOfPackages and lastPackageSize
for(int i=5; i>=3; i--) {
this.log("UART " + String.format("%02X ", inBuffer.buffer[i]) + " ");
this.log("UART " + String.format("%02X ", buffer6[i]) + " ");
imageSize = (imageSize << 8) | (inBuffer.buffer[i] & 0xFF);
}
this.log("\n");
this.log("UART Read Get Image Size = " + imageSize + "\n");
if (getImageSizeOK) return outEmptyBytes;
long nbOfPackages = (long)Math.ceil((double)imageSize / (506.0));
long lastPackageSize = imageSize - (nbOfPackages-1) * 506;
//
byte[] bufferACK = {(byte) 0xAA, (byte) 0x0E, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
byte[] bufferData = new byte[imageSize];
UARTMessage inBufferData = new UARTMessage(bufferData);
byte[] bufferID = {(byte) 0x00,(byte) 0x00};
UARTMessage inBufferID = new UARTMessage(bufferID);
byte[] bufferPKGSize = {(byte) 0x00,(byte) 0x00};
UARTMessage inBufferPKGSize = new UARTMessage(bufferPKGSize);
byte[] bufferVerifCode = {(byte) 0x00,(byte) 0x00};
UARTMessage inBufferVerifCode = new UARTMessage(bufferVerifCode);
boolean readOK = false;
int dataOffset = 0;
for (int i=0; i < (nbOfPackages-1); i++) {
// -> ACK = AA 0E 00 00 b0 b1 // b1b0 = idid start with 00 00
_uartOut.write(bufferACK);
// <- Image Data Package 512 bytes [idid dsds 512-6bytes vcvc] [package id, data size, data, verif code]
//Thread.sleep(10);
readOK = readIn(inBufferID, 0, 2, readTimeout); // idid
if (!readOK) {
this.log("UART Read Package " + i + " ID FAILED\n");
return outEmptyBytes;
}
readOK = readIn(inBufferPKGSize, 0, 2, readTimeout); // dsds
if (!readOK) {
this.log("UART Read Package " + i + " Data size FAILED\n");
return outEmptyBytes;
}
readOK = readIn(inBufferData, dataOffset, 506, readTimeout); // 512-6bytes
if (!readOK) {
this.log("UART Read Package " + i + " Data FAILED\n");
return outEmptyBytes;
}
dataOffset += 506;
readOK = readIn(inBufferVerifCode, 0, 2, readTimeout); // vcvc
if (!readOK) {
this.log("UART Read Package " + i + " Verification code FAILED\n");
return outEmptyBytes;
}
// prepare NEXT ACK
bufferACK[4] = bufferID[0];
bufferACK[5] = bufferID[1];
}
// -> ACK = AA 0E 00 00 b0 b1 // b1b0 = idid
_uartOut.write(bufferACK);
// <- Image Data Package 512 bytes [idid dsds rest-6bytes vcvc] [package id, data size, data, verif code]
//Thread.sleep(10);
readOK = readIn(inBufferID, 0, 2, readTimeout); // idid
if (!readOK) {
this.log("UART Read Package Last ID FAILED\n");
return outEmptyBytes;
}
readOK = readIn(inBufferPKGSize, 0, 2, readTimeout); // dsds
if (!readOK) {
this.log("UART Read Package Last Data size FAILED\n");
return outEmptyBytes;
}
readOK = readIn(inBufferData, dataOffset, (int)lastPackageSize, readTimeout); // rest-6bytes
if (!readOK) {
this.log("UART Read Package Last Data FAILED\n");
return outEmptyBytes;
}
readOK = readIn(inBufferVerifCode, 0, 2, readTimeout); // vcvc
if (!readOK) {
this.log("UART Read Package Last Verification code FAILED\n");
return outEmptyBytes;
}
// prepare LAST ACK
bufferACK[4] = bufferID[0];
bufferACK[5] = bufferID[1];
// -> ACK = AA 0E 00 00 b0 b1 // b1b0 = idid
_uartOut.write(bufferACK);
// proper output
return bufferData;
} catch (final IOException ex) {
this.log("UART WRITE IOException \n" + ex.toString() + "\n");
}/* catch (InterruptedException ex2) {
this.log("UART WRITE Thread InterruptedException \n" + ex2.toString() + "\n");
}*/
return outEmptyBytes;
}
/**
*
* @param inBuffer
* @param nbOfBytesToRead
* @param timeout
* @return
*/
private boolean readIn(UARTMessage inBuffer, int offset, int nbOfBytesToRead, int timeout) {
boolean output = false;
UARTRead uartRead = new UARTRead(inBuffer, offset, nbOfBytesToRead);
uartRead.execute(_uartIn);
try {
output = uartRead.get(timeout, TimeUnit.MILLISECONDS);
} catch (CancellationException e) {
this.log("UART Read CancellationException\n");
} catch (InterruptedException e) {
this.log("UART Read InterruptedException\n");
} catch (ExecutionException e) {
this.log("UART Read ExecutionException\n");
} catch (TimeoutException e) {
this.log("UART Read TimeoutException\n");
}
return output;
}
/**
*
* @param msg
*/
public abstract void log(String msg);
}