Android - IOIO板,简单的UART代码问题

发布于 2024-12-12 03:10:18 字数 4266 浏览 0 评论 0原文

我使用的是sparkfun提供的IOIO板。在尝试测试 UART 时,我连接了 IOIO 板的 RX 和 TX,并希望从手机发送一个字节并在手机上接收相同的字节并在文本框中显示。当我运行以下代码时,用户界面没有任何变化。我认为我错过了一件基本的事情。有什么建议/想法吗?

package ioio.examples.hello;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import ioio.examples.hello.R;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIOFactory;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.api.exception.IncompatibilityException;
import ioio.lib.util.AbstractIOIOActivity;
import android.os.Bundle;
import android.widget.ToggleButton;
import android.widget.TextView;
import ioio.lib.api.Uart;

/**
 * This is the main activity of the HelloIOIO example application.
 * 
 * It displays a toggle button on the screen, which enables control of the
 * on-board LED. This example shows a very simple usage of the IOIO, by using
 * the {@link AbstractIOIOActivity} class. For a more advanced use case, see the
 * HelloIOIOPower example.
 */

public class MainActivity extends AbstractIOIOActivity {

private ToggleButton togglebutton;
private TextView textView;

/**
 * Called when the activity is first created. Here we normally initialize
 * our GUI.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.editText1);
    togglebutton = (ToggleButton) findViewById(R.id.button);
}

/**
 * This is the thread on which all the IOIO activity happens. It will be run
 * every time the application is resumed and aborted when it is paused. The
 * method setup() will be called right after a connection with the IOIO has
 * been established (which might happen several times!). Then, loop() will
 * be called repetitively until the IOIO gets disconnected.
 */
class IOIOThread extends AbstractIOIOActivity.IOIOThread {
    /** The on-board LED. */

    private Uart uart;
    private InputStream in;
    private OutputStream out;
    private byte receivedData[] = new byte[10];
    private int offset = 0;
    private Byte b;
    protected IOIO ioio_;

    /**
     * Called every time a connection with IOIO has been established.
     * Typically used to open pins.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
     */
    @Override
    protected void setup() throws ConnectionLostException {
        // led_ = ioio_.openDigitalOutput(0, true);
        ioio_ = IOIOFactory.create();
        try {
            ioio_.waitForConnect();
        } catch (IncompatibilityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uart = ioio_.openUart(3, 4, 115200, Uart.Parity.NONE,
                Uart.StopBits.ONE);
        in = uart.getInputStream();
        out = uart.getOutputStream();
    }

    /**
     * Called repetitively while the IOIO is connected.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
     */
    @Override
    protected void loop() throws ConnectionLostException {

        offset = 0;
        while (togglebutton.isChecked()) {
        try {
                out.write(65);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // Ignore
                }
            } catch (IOException e) {
                // TODO ???
            }
            try {
                in.read(receivedData, 0, 1);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // Ignore
                }
            } catch (IOException e) {
                // TODO ???
            }

            textView.setText(Byte.toString(receivedData[0]));
        }
    }

}

/**
 * A method to create our IOIO thread.
 * 
 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
 */
@Override
protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
    return new IOIOThread();
}

}

I am using the IOIO board given by sparkfun. While trying to test UART , I tied the RX and TX of the IOIO board and wanted to send one byte from the phone and receive same byte on the Phone ans show it in a textbox. When I run the following code , nothing changes in the UI. I think I am missing a fundamental thing. Any suggestions / ideas ?

package ioio.examples.hello;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import ioio.examples.hello.R;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIOFactory;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.api.exception.IncompatibilityException;
import ioio.lib.util.AbstractIOIOActivity;
import android.os.Bundle;
import android.widget.ToggleButton;
import android.widget.TextView;
import ioio.lib.api.Uart;

/**
 * This is the main activity of the HelloIOIO example application.
 * 
 * It displays a toggle button on the screen, which enables control of the
 * on-board LED. This example shows a very simple usage of the IOIO, by using
 * the {@link AbstractIOIOActivity} class. For a more advanced use case, see the
 * HelloIOIOPower example.
 */

public class MainActivity extends AbstractIOIOActivity {

private ToggleButton togglebutton;
private TextView textView;

/**
 * Called when the activity is first created. Here we normally initialize
 * our GUI.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.editText1);
    togglebutton = (ToggleButton) findViewById(R.id.button);
}

/**
 * This is the thread on which all the IOIO activity happens. It will be run
 * every time the application is resumed and aborted when it is paused. The
 * method setup() will be called right after a connection with the IOIO has
 * been established (which might happen several times!). Then, loop() will
 * be called repetitively until the IOIO gets disconnected.
 */
class IOIOThread extends AbstractIOIOActivity.IOIOThread {
    /** The on-board LED. */

    private Uart uart;
    private InputStream in;
    private OutputStream out;
    private byte receivedData[] = new byte[10];
    private int offset = 0;
    private Byte b;
    protected IOIO ioio_;

    /**
     * Called every time a connection with IOIO has been established.
     * Typically used to open pins.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
     */
    @Override
    protected void setup() throws ConnectionLostException {
        // led_ = ioio_.openDigitalOutput(0, true);
        ioio_ = IOIOFactory.create();
        try {
            ioio_.waitForConnect();
        } catch (IncompatibilityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uart = ioio_.openUart(3, 4, 115200, Uart.Parity.NONE,
                Uart.StopBits.ONE);
        in = uart.getInputStream();
        out = uart.getOutputStream();
    }

    /**
     * Called repetitively while the IOIO is connected.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
     */
    @Override
    protected void loop() throws ConnectionLostException {

        offset = 0;
        while (togglebutton.isChecked()) {
        try {
                out.write(65);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // Ignore
                }
            } catch (IOException e) {
                // TODO ???
            }
            try {
                in.read(receivedData, 0, 1);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // Ignore
                }
            } catch (IOException e) {
                // TODO ???
            }

            textView.setText(Byte.toString(receivedData[0]));
        }
    }

}

/**
 * A method to create our IOIO thread.
 * 
 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
 */
@Override
protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
    return new IOIOThread();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-12-19 03:10:18

似乎传递给 UI 的任何内容都必须发生在单独的线程中。你的情况你必须添加这样的东西:

        private void changeText(String displayText){
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                textView.setText(displayText);

            }   
        });
    }

It seems that anything passed to the UI must occur in a separate thread. Your case you'd have to add something like this:

        private void changeText(String displayText){
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                textView.setText(displayText);

            }   
        });
    }
表情可笑 2024-12-19 03:10:18

您遇到的一个明显错误是:

ioio_ = IOIOFactory.create();
try {
    ioio_.waitForConnect();
} catch (IncompatibilityException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}

完全没有必要并且可能存在问题。 AbstractIOIOActivity 会为您处理所有这些事情。查看一些示例(例如 HelloIOIO 或 IOIOSimpleApp),了解您的应用程序通常应该是什么样子。

顺便说一句,此类问题的正确位置是 Google 群组中的 ioio-users 列表。

One obvious error you have is that:

ioio_ = IOIOFactory.create();
try {
    ioio_.waitForConnect();
} catch (IncompatibilityException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}

is completely unnecessary and possibly problematic. AbstractIOIOActivity takes care of all that for you. Have a look at some of the examples (e.g. HelloIOIO or IOIOSimpleApp) to get an idea regarding what your app should typically look like.

By the way, the proper place for such questions is the ioio-users list on Google groups.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文