获得无限循环?

时间:2013-01-18 16:27:36

标签: java android loops serial-port infinite-loop

我使用android中的库连接到终端模拟器,它连接到串行设备(交换机)并显示发送/接收的数据。我使用另一个库通过串行发送数据。我通过终端下方的文本框通过连接发送数据,或者通过键入终端本身并在两种情况下按键盘输入。我也可以通过按下按钮来发送命令。

在我的活动中,我有一个名为sendOverSerial的方法,它只调用一个库方法,通过usb将数据发送到通过串行连接的设备。

public static void sendOverSerial(byte[] data) {

        if(mSelectedAdapter !=null && data !=null){
        mSelectedAdapter.sendData(data);

      }}

此活动打开一个类的实例,该类用于将数据写入终端屏幕。通常,当我想通过串行发送数据时,通过editTextbuttons i调用活动中的sendOverSerial方法。但是当我将字符写入终端本身时,它们会在这个新的实例write方法中被选中。所以我必须从该实例调用sendOverSerial方法。我的问题是如果我把它叫做下面的“TEST”,那么TEST会在无限循环中写入终端,它只是继续写它。知道为什么吗?如果我按照我的评论进一步向下发送它只是按预期发送一次。

 public void write(byte[] bytes, int offset, int count) {

            int numCRs = 0;
            for (int i = offset; i < offset + count; ++i) {
                if (bytes[i] == '\r') {
                    ++numCRs;
                }
            }

            if (numCRs == 0) {
                // No CRs -- just send data as-is

                //infinite loop if I send from here
                GraphicsTerminalActivity.sendOverSerial("TEST".getBytes());

                super.write(bytes, offset, count);

                if (isRunning()) {
                   doLocalEcho(bytes);
                }
                return;
            }

            Log.d(TAG, "CRs=== " + numCRs);
            // Convert CRs into CRLFs
            byte[] translated = new byte[count + numCRs];
            int j = 0;
            for (int i = offset; i < offset + count; ++i) {
                if (bytes[i] == '\r') {
                    translated[j++] = '\r';
                    translated[j++] = '\n';
                } else {
                    translated[j++] = bytes[i];
                }
            }
           //fine if I send from here, sends once
            GraphicsTerminalActivity.sendOverSerial("SECOND TEST".getBytes());
           super.write(translated, 0, translated.length);

            // If server echo is off, echo the entered characters locally
            if (isRunning()) {
                doLocalEcho(translated);
            }                            
        }

来自图书馆的超级写作:

 public void write(byte[] data, int offset, int count) {
        try {
            mWriteQueue.write(data, offset, count);
        } catch (InterruptedException e) {
        }
        notifyNewOutput();
    }

然后在库中的另一个类中调用write,即bytequeue类

public void write(byte[] buffer, int offset, int length)
    throws InterruptedException {
        if (length + offset > buffer.length) {
            throw
                new IllegalArgumentException("length + offset > buffer.length");
        }
        if (length < 0) {
            throw
            new IllegalArgumentException("length < 0");

        }
        if (length == 0) {
            return;
        }
        synchronized(this) {
            int bufferLength = mBuffer.length;
            boolean wasEmpty = mStoredBytes == 0;
            while (length > 0) {
                while(bufferLength == mStoredBytes) {
                    wait();
                }
                int tail = mHead + mStoredBytes;
                int oneRun;
                if (tail >= bufferLength) {
                    tail = tail - bufferLength;
                    oneRun = mHead - tail;
                } else {
                    oneRun = bufferLength - tail;
                }
                int bytesToCopy = Math.min(oneRun, length);
                System.arraycopy(buffer, offset, mBuffer, tail, bytesToCopy);
                offset += bytesToCopy;
                mStoredBytes += bytesToCopy;
                length -= bytesToCopy;
            }
            if (wasEmpty) {
                notify();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

当数据被发送时,一个方法被自动调用,这个方法也有一个写入它的调用,et viola,无限循环。

相关问题