0

I have to change ttyS1 port's baudrate every second. So i need to wake-up remote machine on 9600 Bauds and after communicate with it on 19200 Bauds. But there is a time limit between wake-up signal and real data communication. I use Handler&Thread for this trick.

I done it and seems okay with Handler&Thread. I toggled postdelayed every entrance for 1 milliseconds and 500 milliseconds. But it works bad. Sometimes 1 milliseconds task takes almost 10-15 milliseconds.

Also i noticed that when i add "runOnUiThread" with some UI update, result goes worst like 30milliseconds.

Note: I need to send Wake-up signal everytime not just one time.

Any idea?

public void serial_query(){
    try {
        Runnable cookimageupdate = new Runnable(){
            public void run(){
                            try {
                                if (mOutputStream != null) {
                                    mSerialPort.close();

                                    if (mLAP==0) //First LAP is used to HOLTEK Wake-Up.  Second one is real data.
                                            {mLAP=1; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 9600, 0); mBufferbuf = (byte)0x00; mOutputStream.write(mBufferbuf);}
                                    else    {mLAP=0; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 19200, 0); mOutputStream.write(mBuffer);}


                                } else {
                                        return;
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                                return;
                        }
                            try{
                                runOnUiThread(new Runnable() {
                                       public void run() {

                                               //meat_temp.setText(_meatprobe_temp.toString());
                                               if (_pt1000_status==1) {pt_status.setText("   PT1000 open-circuit");}
                                               else if (_pt1000_status==2){pt_status.setText("   PT1000 short-circuit");}
                                               else if (_pt1000_status==0){pt_status.setText("   -");}
                                       }
                               });

                               }
                            catch (Exception e)
                            {
                               e.getLocalizedMessage();
                            }

                         if (mLAP==1) 
                                {handler_serial.postDelayed(this, 1);}
                        else    {handler_serial.postDelayed(this, 500);}
            }
        };
        handler_serial.postDelayed(cookimageupdate, 50);    //start with 50mSec. delay
    }
    catch(Exception e){
        e.printStackTrace();
        return;
    }
};
babur
  • 11
  • 2

1 Answers1

0

1 millisecond is too short a time delay to post a delayed runnable. Most handlers take more time than that to process your message.

For such low delays you would be better off using

Thread.sleep().
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • I've also tried this. And many things more. But same result. I think Android wants to attach a specific time interval for any task. So my serial communication takes variable time interval. Or any other UI process can affect badly. I must do it so i have no chance to change other MCU's source code. It is another product. Thanks – babur Aug 17 '12 at 07:57