C
C
Chvalov2015-08-29 17:12:06
Java
Chvalov, 2015-08-29 17:12:06

How to write a loop with a different pause time for each command - Android?

There is a button, when pressed, only one command is sent:

bytesToSend = addCRC(new byte[]{0x1, 0x3, 0x0, 0x0 ,0x0, 0x0}); // Команда статус
mPhysicaloid.write(bytesToSend, bytesToSend.length);
I can’t figure out how to write a loop that will send all these commands:
addCRC(new byte[]{1, 0x5, 11, 5 ,0, 0});    //10 сек
addCRC(new byte[]{1, 0x5, 11, 1 ,0, 0});    //0 сек
addCRC(new byte[]{1, 0x5, 0, 5 ,0, 0});      //30 сек
addCRC(new byte[]{1, 0x5, 1, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 2, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 3, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 4, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 5, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 6, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 7, 5 ,0, 0});      //5 сек
addCRC(new byte[]{1, 0x5, 8, 5 ,0, 0});      //5 сек

After each command, there should be a pause (It is indicated in the comments), the pause value for each command will be stored in the int
variable UPD: I'm completely confused with these bytes and cycles:
It should turn out like this:
public byte[] MyListComands = new byte[11];

    public void onClickWrite(View v) { 
        new Thread() {
            @Override
            public void run() {
                int TimeOut = 0;
                try {
                    for (int i = 0; i <= 11;  i++, Thread.sleep(TimeOut)) {
                        MyListComands[i] = addCRC(new byte[]{1, 0x5, 1, 5 ,0, 0});
                        // Тут как я догоняю должно быть то - что в зависимости от значение "i"
                        // будет присваивать новое значение таймеру TimeOut
                        mPhysicaloid.write(MyListComands[i], MyListComands[i].length);
                    }
                } catch (InterruptedException ex) {
                // тут хз что ;(
                }
            }

        }.start();
    }
But this one will not work, but how is it possible to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bolshakov, 2015-08-29
@Chvalov

How is that an option? There may be errors in the code - I'm writing from a tablet.

byte[] mls = new byte[11];
mls[0] = addCRC(new byte[]{1, 0x5, 11, 5 ,0, 0});    //10 сек
mls[1] = addCRC(new byte[]{1, 0x5, 11, 1 ,0, 0});    //0 сек
mls[2] = addCRC(new byte[]{1, 0x5, 0, 5 ,0, 0});      //30 сек
mls[3] = addCRC(new byte[]{1, 0x5, 1, 5 ,0, 0});      //5 сек
mls[4] = addCRC(new byte[]{1, 0x5, 2, 5 ,0, 0});      //5 сек
mls[5] = addCRC(new byte[]{1, 0x5, 3, 5 ,0, 0});      //5 сек
mls[6] = addCRC(new byte[]{1, 0x5, 4, 5 ,0, 0});      //5 сек
mls[7] = addCRC(new byte[]{1, 0x5, 5, 5 ,0, 0});      //5 сек
mls[8] = addCRC(new byte[]{1, 0x5, 6, 5 ,0, 0});      //5 сек
mls[9] = addCRC(new byte[]{1, 0x5, 7, 5 ,0, 0});      //5 сек
mls[10] = addCRC(new byte[]{1, 0x5, 8, 5 ,0, 0});      //5 сек

public int[] delays = new int[11]{10,0,30,5,5,5,5,5,5,5,5};
int shift;
public void onClickWrite(View v) { 
    int len = mls.length;
    for (int i = 0; i < len;  i++) {
        shift += delays[i];
        new Handler(Looper.getMainLooper()).postDelayed( new Runnable() {
            public void run(){
                byte b = mls[i];
                mPhysicaloid.write(b, b.length);
            }
        }, shift);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question