gpt4 book ai didi

java - IOIO UART 读回问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:07 25 4
gpt4 key购买 nike

我正在扩展 BaseIOIOLooper 以打开 UART 设备并发送消息。我正在使用回读进行测试,其中我通过一条线路发送一个数据包,并在另一条线路上接收该数据包并将其打印出来。因为我不希望 InputStream.read() 方法阻塞,所以我在不同的线程中处理数据包形成和输入。我已将问题范围缩小到 InputStream.read() 方法,该方法返回 -1 (没有读取字节,但也不异常(exception))。这是 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 都读取振荡电压,尽管幅度非常高,这是值得关注的。要点是无法从 InputHandler 类中的 InputStream 中读取任何内容。有什么想法吗?

最佳答案

read() 返回的

-1 仅应在 UART 关闭时发生。在 Uart 对象上显式调用 close() 或在 IOIO 对象上调用 softReset() 可能会导致关闭。

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

您在示波器上看到的读数很可疑:“非常高的幅度”有多高?您应该只能在这些引脚上看到 0V 或 3.3V,或者在引脚由于某种原因未打开(或关闭)的情况下处于 float 状态。

关于java - IOIO UART 读回问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com