访问串口(为/ dev / tty的*)没有投票传入数据串口、数据、dev、tty

2023-09-06 15:11:59 作者:丶男生的痛只有自己懂。

我用姜饼。我已经跟本地串口(RS-232原生UART,USB不dungle)。这是通过java.io和一小片原生$ C $的c C语言编写的用于打开和关闭该流并配置波特率进行。它的工作原理,但我首先想到的是,它是没有效率的,因为我需要设置一个线程,不断查询的InputStream检查是否有从串口来个字节。什么是浪费的方式来使用CPU和电池。

I use Gingerbread. I already talk to a native serial port (RS-232 native UART, not USB dungle). This is done through java.io and a small piece of native code written in C to open and close the stream and configure the baudrate. It works but my first thought was that it is not efficient because I need to setup a thread that constantly poll the inputstream to check if there are bytes coming from the serial port. What a wasteful way to use CPU and batteries.

    private InputStream mInputStream;
private ReadThread mReadThread;

private class ReadThread extends Thread {

    @Override
    public void run() {
        super.run();
        while(!isInterrupted()) {
            int size;
            try {
                byte[] buffer = new byte[64];
                if (mInputStream == null) return;
                size = mInputStream.read(buffer);
                if (size > 0) {
                    onDataReceived(buffer, size);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    }
}

我认为我需要做的是有同样的信号,作为android系统中的其他事件,我可以设置一些听者,让我被告知一些字节来的,醒来我的线程会取输入数据的服务。我没有发现这样的API的任何东西。

What I Thought I needed to do is to have the same kind of signal as for other events in android where I can setup some kind of listener that would allow me to be informed of some bytes coming and wake up my thread that will take care of the incoming data. I did not find anything like that in the APIs.

不过,寻找和研究多,终于,我认为这是不浪费,但我想从别人谁知道线程在Android中是如何工作的意见。让我们来看看codeS线。

But, after looking and researching more, finally, I think this is not wasteful but I'd like an opinion from someone who knows about how threads work in Android. Let's look at codes lines.

size = mInputStream.read(buffer);

实际上是一个阻塞读。我看着怎么装置被打开,它在C通过NDK / JNI接口完成的,这是没有标志NOBLOCK / NODELAY打开。为此,它不应该不断地旋转在while循环,但只是被阻塞的mInputStream.read和内核应该只是切换到另一个线程有一些数据可供读取到。在这种情况下,这code是完全罚款。这是假设真的吗?

is actually a blocking read. I looked how the device was opened and it is done in C through a NDK/JNI interface and it is open without the flag "NOBLOCK/NODELAY". Therefor, it should not spin constantly in the while loop but just get blocked on the mInputStream.read and the Kernel should just switch to another thread until there is some data available to read. In such case, this code would be totally fine. Is this assumption true?

我想知道,因为我的嵌入式系统的主要目的是为了显示大部分时间视频,我不希望我的管理幕后吸行动多少资源并给予一定的打嗝的视频播放。

I'd like to know that because my embedded system main purpose is to display videos most of the time and I don't want what I manage behind the scene to suck-up to much resources and give some hiccups in the video playback.

推荐答案

编写应用程序,并经过测试,我可以说,在这个问题做出最后的假设是真的。如果该设备是无标记NOBLOCK / NODELAY公开,该线程将在mInputStream.read()被暂停,直到有可用的数据。没有浪费轮询。

After writing the application, and testing, I can say that the last assumption made in the question is actually true. If the device is open without the flag "NOBLOCK/NODELAY", the thread will be suspended on the mInputStream.read() until there is data available. No wasteful polling.