我正在开发一个应用程序,用于通过 Arduino 从 Android 设备读取传感器数据。我能够设置通信并读取该数据。该数据通过 USB 电缆以字节数组的形式来自 Arduino。
从那个字节数组中,我将它读入一个字节并显示在 Android 设备上,当我从 Arduino 接收到所有数据时,我将输出发送到 Arduino。它使LED闪烁。在这次交流之间,我得到了一秒钟的延迟。如何减少沟通中的延迟?
我从 arduino 到 android 接收数据的代码是:
public void run() {
int ret = 0;
//byte[] buffer = new byte[16384];
byte[] buffer = new byte[65536];
int i;
i = 0;
while(i<1){
try {
startSendHandshakeDataExecTime = System.currentTimeMillis();
ret = mInputStream.read(buffer);
for(int a=0;a<noOfFlowSensors;a++){
intListFlowBytes.add((int)buffer[a]);
}
for(int b=0;b<noOfAnalogSensors;b++){
intListAnalogBytes.add((int)buffer[noOfFlowSensors+b]);
}
for(int c=0;c<noOfSwitches;c++){
intListSwitchesBytes.add((int)buffer[noOfFlowSensors+noOfAnalogSensors+c]);
}
/*for (int n = 0; n <noOfFlowSensors + noOfAnalogSensors + noOfSwitches; n++)
{*/
endSendHandshakeDataExecTime = System.currentTimeMillis();
Message m = Message.obtain(mHandlerHandShake);
//m.obj = new TelemetryPacket(composeInt(buffer[i + 1],buffer[i + 2]));
//m.obj = new ValueMsg('f',ret,buffer[0],composeInt(buffer[1],buffer[2]),buffer[3],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
// m.obj = new ValueMsg('f',ret,buffer[0],buffer[1],buffer[2],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
m.obj = new ValueMsgHandShake(""+(endSendHandshakeDataExecTime-startSendHandshakeDataExecTime),ret,intListFlowBytes,intListAnalogBytes,intListSwitchesBytes,1);
mHandlerHandShake.sendMessage(m);
// }
mInputStream.close();
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while (true) { // read data
try {
startTime = System.currentTimeMillis();
startReceiveDataExecTime = System.currentTimeMillis();
ret = mInputStream.read(buffer);
for(int p=0;p<noOfFlowSensors;p++){
intListFlowValues.add((int)buffer[p]);
//intPulseValues = (int)buffer[p];
}
Message mPulse = Message.obtain(mHandlerPulseSensors);
//m.obj = new TelemetryPacket(composeInt(buffer[i + 1],buffer[i + 2]));
//m.obj = new ValueMsg('f',ret,buffer[0],composeInt(buffer[1],buffer[2]),buffer[3],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
// m.obj = new ValueMsg('f',ret,buffer[0],buffer[1],buffer[2],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
mPulse.obj = new ValueMsgPulseSensors("f",ret,intListFlowValues);
mHandlerPulseSensors.sendMessage(mPulse);
for(int a=0;a<2*noOfAnalogSensors;a=a+2){
intListAnalogValues.add((int)(composeInt(buffer[noOfFlowSensors+a],buffer[noOfFlowSensors+a+1])));
// intAnalogValues = (int)buffer[composeInt(buffer[noOfFlowSensors+a],buffer[noOfFlowSensors+a+1])];
}
Message mAnalog = Message.obtain(mHandlerAnalogSensors);
//m.obj = new TelemetryPacket(composeInt(buffer[i + 1],buffer[i + 2]));
//m.obj = new ValueMsg('f',ret,buffer[0],composeInt(buffer[1],buffer[2]),buffer[3],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
// m.obj = new ValueMsg('f',ret,buffer[0],buffer[1],buffer[2],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
mAnalog.obj = new ValueMsgAnalogSensors("f",ret,intListAnalogValues);
mHandlerAnalogSensors.sendMessage(mAnalog);
for(int l=0;l<noOfSwitches;l++){
intListSwitchesValues.add( (int)buffer[noOfFlowSensors+2*noOfAnalogSensors+l]);
//intSwitchValues = (int)buffer[noOfFlowSensors*intListFlowBytes.size()+intListAnalogBytes.size()*noOfAnalogSensors+l];
}
Message mSwitch = Message.obtain(mHandlerSwitchSensors);
//m.obj = new TelemetryPacket(composeInt(buffer[i + 1],buffer[i + 2]));
//m.obj = new ValueMsg('f',ret,buffer[0],composeInt(buffer[1],buffer[2]),buffer[3],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
// m.obj = new ValueMsg('f',ret,buffer[0],buffer[1],buffer[2],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
mSwitch.obj = new ValueMsgSwitchSensors("f",ret,intListSwitchesValues);
mHandlerSwitchSensors.sendMessage(mSwitch);
/* int intEnd=(int)buffer[noOfFlowSensors+noOfAnalogSensors+noOfSwitches+1];
for (int n = 0; n <noOfFlowSensors + noOfAnalogSensors + noOfSwitches; n++)
{
Message m = Message.obtain(mHandler);
//m.obj = new TelemetryPacket(composeInt(buffer[i + 1],buffer[i + 2]));
//m.obj = new ValueMsg('f',ret,buffer[0],composeInt(buffer[1],buffer[2]),buffer[3],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
// m.obj = new ValueMsg('f',ret,buffer[0],buffer[1],buffer[2],composeInt(buffer[4],buffer[5]),buffer[6],composeInt(buffer[7],buffer[8]),buffer[9],composeInt(buffer[10],buffer[11]),buffer[12],buffer[13],buffer[14],buffer[15]);
m.obj = new ValueMsg1('f',ret,intListFlowValues,intListAnalogValues,intListSwitchesValues,intEnd);
mHandler.sendMessage(m);
// }
*/
i += 1;
endReceiveDataExecTime = System.currentTimeMillis();
Message mReceivedDataDelay = Message.obtain(mHandlerReceiveDataDelay);
mReceivedDataDelay.obj = new ValueMsgReceivedDataDelay(""+ (endReceiveDataExecTime-startReceiveDataExecTime));
mHandlerReceiveDataDelay.sendMessage(mReceivedDataDelay);
mInputStream.close();
OutputDataAfterDataReceived();
} catch (IOException e) {
break;
}
}
//sendOutputData();
}