IOIO UART回读问题

时间:2015-12-17 03:07:08

标签: java android multithreading uart ioio

我正在扩展BaseIOIOLooper以打开UART设备并发送消息。我正在使用回读进行测试,我通过线路发送数据包并在另一条线路上接收该数据包并将其打印出来。因为我不想阻止InputStream.read()方法,所以我在另一个线程中处理数据包的形成和输入。我已将问题缩小到InputStream.read()方法,该方法返回-1(没有读取字节,但没有异常)。 这是Looper线程中的样子:

        @Override
    protected void setup() throws ConnectionLostException, InterruptedException {
        log_.write_log_line(log_header_ + "Beginning IOIO setup.");
        // Initialize IOIO UART pins
        // Input at pin 1, output at pin 2
        try {
            inQueue_ = MinMaxPriorityQueue.orderedBy(new ComparePackets())
                    .maximumSize(QUEUESIZE).create();
            outQueue_ = MinMaxPriorityQueue.orderedBy(new ComparePackets())
                    .maximumSize(QUEUESIZE).create();
            ioio_.waitForConnect();
            uart_ = ioio_.openUart(1, 2, 38400, Uart.Parity.NONE, Uart.StopBits.ONE);
            // Start InputHandler. Takes packets from ELKA on inQueue_
            in_= new InputHandler(inQueue_, uart_.getInputStream());
            in_.start();
            // Start OutputHandler. Takes packets from subprocesses on outQueue_
            out_= new OutputHandler(outQueue_);
            out_.start();
            // Get output stream
            os_=uart_.getOutputStream();
            // Set default target state
            setTargetState(State.TRANSFERRING);
            currInPacket_[0]=1; //Initial value to start transferring
            log_.write_log_line(log_header_ + "IOIO setup complete.\n\t" +
                    "Input pin set to 1\n\tOutput pin set to 2\n\tBaud rate set to 38400\n\t" +
                    "Parity set to even\n\tStop bits set to 1");
        } catch (IncompatibilityException e) {
            log_.write_log_line(log_header_+e.toString());
        } catch (ConnectionLostException e) {
            log_.write_log_line(log_header_+e.toString());
        } catch (Exception e) {
            log_.write_log_line(log_header_+"mystery exception: "+e.toString());
        }
    }

在InputHandler线程中:

    @Override
public void run() {
    boolean notRead;
    byte i;
    log_.write_log_line(log_header_+"Beginning InputHandler thread");
    while (!stop) {
        i = 0;
        notRead = true;
        nextInPacket = new byte[BUFFERSIZE];
        readBytes = -1;
        //StringBuilder s=new StringBuilder();
        //TODO re-implement this with signals
        while (i < READATTEMPTS && notRead) {
            try {
                // Make sure to adjust packet size. Done manually here for speed.
                readBytes = is_.read(nextInPacket, 0, BUFFERSIZE);
                /* Debugging
                for (int j=0;j<nextInPacket.length;j++)
                    s.append(Byte.toString(nextInPacket[j]));
                log_.write_log_line(log_header_+s.toString());
                */

                if (readBytes != -1) {
                    notRead = false;
                    nextInPacket= new byte[]{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0};
                    synchronized (q_) {
                        q_.add(nextInPacket);
                    }
                //log_.write_log_line(log_header_ + "Incoming packet contains valid data.");
                } else i++;
            } catch (IOException e) {
                log_.write_log_line(log_header_ + "mystery exception:\n\t" + e.toString());
            }
        }

        if (i>=READATTEMPTS)
            log_.write_log_line(log_header_+"Too many read attempts from input stream.");

        /*
        try {
            sleep(100);
        } catch (InterruptedException e) {
            log_.write_log_line(log_header_+"fuck");
        }
        */
    }
}

在示波器上,引脚1和2都读取振荡电压,虽然幅度非常高,这是一个值得关注的问题。 Point无法从InputHandler类的InputStream中读取。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

只有当UART关闭时才会发生从read()返回的

-1。闭包可能是因为在close()对象上显式调用Uart或在softReset()对象上调用IOIO而发生的。

Android日志可能会为您提供有关正在发生的事情的线索。

你在示波器上看到的读数是可疑的:“非常高的振幅”有多高?您应该只在这些引脚上看到0V或3.3V,或者浮动以防由于某些原因引脚未打开(或关闭)。