【问题标题】:Android Things 3 Cannot read from rxtx on Raspberry PI 3Android Things 3 无法从 Raspberry PI 3 上的 rxtx 读取
【发布时间】:2026-01-17 23:55:01
【问题描述】:

我有一个配置为继续在 TX 端口上写东西的 Arduino,它通过 Android Things Developer preview 3 连接到我的 Raspberry Pi 3(我确信 RX/TX 电缆配置正确且电压正常)。

我正在尝试使用 Android Things 库的 PeripheralManagerService 读取数据和写入数据。

启动应用时出现错误:

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:44): avc: denied { read write } for name="ttyAMA0" dev="tmpfs" ino=1203 scontext=u:r:peripheralman:s0 tcontext=u:object_r:设备:s0 tclass=chr_file permissive=1

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:45): avc: denied { open } for path="/dev/ttyAMA0" dev="tmpfs" ino=1203 scontext=u:r:peripheralman:s0 tcontext=u: object_r:device:s0 tclass=chr_file permissive=1

04-11 16:26:13.665 163-163/? I/peripheralman: type=1400 audit(0.0:46): avc: denied { ioctl } for path="/dev/ttyAMA0" dev="tmpfs" ino=1203 ioctlcmd=5401 scontext=u:r:peripheralman:s0 tcontext =u:object_r:device:s0 tclass=chr_file permissive=1

我的代码:

public class MainActivity extends Activity {

private final int BUFFER_SIZE = 64;
private UartDevice rxtx;


public void configureUartFrame(UartDevice uart) throws IOException {
    // Configure the UART port
    uart.setBaudrate(9600);
}

public void writeUartData(UartDevice uart, String msg) throws IOException {
    byte[] buffer = msg.getBytes();
    int count = uart.write(buffer, buffer.length);
    Log.d(TAG, "Wrote " + count + " bytes to peripheral");
}

public void readUartBuffer(UartDevice uart) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((count = uart.read(buffer, buffer.length)) > 0) {
        Log.d(TAG, "Read " + count + " bytes from peripheral");
        Log.d("Message", new String(buffer));
    }
}

@Override
protected void onStart() {
    super.onStart();
        try {
            writeUartData(rxtx, "teste");
            readUartBuffer(rxtx);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PeripheralManagerService manager = new PeripheralManagerService();

    manager.getUartDeviceList();
    List<String> portList = manager.getUartDeviceList();

    if (portList.isEmpty()) {
        Log.i(TAG, "No UART port available on this device:");
    } else {
        Log.i(TAG, "List of RXTX available ports: - " + portList);
    }

    try{
       rxtx = manager.openUartDevice(portList.get(0));
       configureUartFrame(rxtx);
    }catch (IOException e){
        e.printStackTrace();
    }
}}

有人知道那是什么吗?

【问题讨论】:

  • 如果您发布(复制和粘贴)文本代码和相关错误,而不是一些最终会在几天内失效的链接,您可以获得更多关于 SO 的帮助。只是一个想法:)
  • 好的!我已编辑。谢谢
  • 您是直接连接到 RPi3 接头上的 RX/TX 引脚 (8/10),还是这是一根 USB-TTL 电缆?如果您使用的是本机 UART,您是否按照文档正确配置了模式? developer.android.com/things/hardware/…
  • 我在正确的引脚上使用直接 RXTX,是的,我已经按照文档进行了配置。抱歉回复晚了。
  • 最有趣的是..我在这里做了一个测试,将数据从 RP3 的 TX 发送到 RP3 的 RX(给自己).. 它工作得很好。波特率/数据大小/奇偶校验/停止位,在两个程序中都是一样的。

标签: android arduino raspberry-pi3 android-things


【解决方案1】:

我在您的代码中没有看到注册了UartDeviceCallback 的任何地方。您如何轮询 UART 以获取新的传入数据?如果没有注册回调,您的应用程序将需要通过 UartDevice.read() 定期轮询 UART 以获取新的传入字节,我在您的代码中也没有看到。这似乎被调用的唯一地方是在 onStart() 活动回调中一次。

回调是一种更有效的方法,所以我建议添加它。更多详情,请查看UART Peripheral I/O Guide

【讨论】:

  • 我发现发生了什么,我使用的是英特尔 Arduino 101,它使用与 Arduino Uno 不同的配置。使用 Arduino Uno,一切正常。感谢您的回答,最好使用回调来轮询数据。但我真的很想知道我上面提到的这个错误是什么。