0

我正在开发一个应用程序,用于通过 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();

    }
4

2 回答 2

0

您描述的行为似乎是这篇文章中描述的串行问题的自动重置:https ://stackoverflow.com/a/10921900/1440715

当 Arduino 接收到来自 Arduino IDE 以外的大多数设备的串行通信时,它会自动重置。这就是为什么您可以从 IDE 发送但不能从其他任何东西发送(无需特别注意)。

我有一个 Uno,并在 Reset 和 Ground 之间放置了一个电容器。这是一个页面,其中包含有关该主题的一些很好的信息。
祝你好运。 http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

于 2012-08-14T14:20:12.300 回答
0

两种简单的解决方案,保证对仍在寻找的任何人都有效。在arduino代码中: -

  1. 将延迟增加到 50 到 100 毫秒。

  2. Serial.begin(9600)在in之后添加这个setup()

    Serial.setTimeout(50);
    

第二步是最重要的。只有在我添加了上面的代码之后它才对我有用。当我遇到完全相同的问题时,在我看过的许多其他论坛中并没有经常提到这一点。

于 2017-06-03T04:02:25.663 回答