0

Firstly, pardon me due to lack of info as I am very new to programming. Below are my codes for opening Physicaloid to connect my android device to Arduino through serial communication. Right now I am receiving analog signals coming from my arduino and appearing on tVread from the array "buf". However I failed and stucked trying to plot it the array "buf" using Android Plot. Please kindly advice. Thank you!

if (mPhysicaloid.open()) {
        setEnabledUi(true);

        if (cbAutoscroll.isChecked()) {
            tvRead.setMovementMethod(new ScrollingMovementMethod());
        }
        mPhysicaloid.addReadListener(new ReadLisener() {
            @Override
            public void onRead(int size) {
                byte[] buf = new byte[size];
                Number[] numarray = new Number[size];

                mPhysicaloid.read(buf,size);
                //convert buf to int array
                for(int i=0;i<size;i++) {
                    numarray[i]= buf[i];
                }
                tvAppend(tvRead, Html.fromHtml("<font color=blue>" + new String (buf) + "</font>"));
                XYSeries series1 = new SimpleXYSeries(
                        Arrays.asList(numarray),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
                LineAndPointFormatter series1Format = new LineAndPointFormatter(
                        Color.rgb(0, 200, 0),                   // line color
                        Color.rgb(0, 100, 0),                   // point color
                        null,                                   // fill color (none)
                        new PointLabelFormatter(Color.WHITE));

                mySimpleXYPlot.addSeries(series1, series1Format);

            }


        });
    } else {
        Toast.makeText(this, "Cannot open", Toast.LENGTH_LONG).show();
    }
}

1 Answers1

0

It's not clear from the code snippet at which point in the Activity lifecycle this gets called but it seems likely that it happens well after onCreate via a background thread.

You'll likely need to add a call to plot.redraw() at the end of your onRead() implementation. Also, depending on how many times this method gets called, what you currently have will result in adding an additional series to the plot on each call, which you probably do not want.

Instead you probably want create your XYSeries outside of the callback and simply update it from onRead().

Nick
  • 8,181
  • 4
  • 38
  • 63
  • Thank you so much Nick! Your recommendation works! The plot populates when I add a mySimpleXYPlot.redraw() at the end of my onRead(). However I am facing the problem which you had anticipated in which its recreating numerous series to the plot and I had failed trying to solve the problem. Is it possible for you to enter some example codes for me to try? Thank you very much! – Liang Weikang Oct 21 '16 at 03:31
  • Do you think you can help if I provide the .java file? – Liang Weikang Oct 27 '16 at 07:58