【问题标题】:Connecting Raspberry PI 3 with Android Things to Arduino [closed]将带有 Android Things 的 Raspberry PI 3 连接到 Arduino [关闭]
【发布时间】:2017-11-23 11:32:19
【问题描述】:

我的问题是关于如何与 Arduino 通信 Android of Things 应用程序。我想在我的 Android of Things 项目中使用所有甜美廉价的 Arduino 传感器库。我找到了两个源链接并尝试过,但没有得到值。

class Arduino(uartDevice: String = "UART0"): AutoCloseable {
    private val TAG = "Arduino"
    private val uart: UartDevice by lazy {
        PeripheralManagerService().openUartDevice(uartDevice).apply {
            setBaudrate(115200)
            setDataSize(8)
            setParity(UartDevice.PARITY_NONE)
            setStopBits(1)
        }
    }

    fun read(): String {
        val maxCount = 8
        val buffer = ByteArray(maxCount)
        var output = ""
        do {
            val count = uart.read(buffer, buffer.size)
            output += buffer.toReadableString()
            if(count == 0) break
            Log.d(TAG, "Read ${buffer.toReadableString()} $count bytes from peripheral")
        } while (true)
        return output
    }

    private fun ByteArray.toReadableString() = filter { it > 0.toByte() }
            .joinToString(separator = "") { it.toChar().toString() }

    fun write(value: String) {
        val count = uart.write(value.toByteArray(), value.length)
        Log.d(TAG, "Wrote $value $count bytes to peripheral")
    }

    override fun close() {
        uart.close()
    }
}

我在堆栈上找到了这个Raspberry Pi to Arduino Communication 源,但讨论与我的问题无关。我想要从 arduino 到 android things RPI 3 的所有值。

我们如何使用 java 通过 android 框架从 arduino 获取值?

【问题讨论】:

  • 看看this 项目。它有一个重要的部分:双向逻辑电平转换器。

标签: android arduino communication android-things


【解决方案1】:

Pi 和 Arduino 中嵌入了多种通信协议。最简单的方法是通过 UART 交换数据。

您的 Arduino 代码可以持续传输数据,而树莓派可以有代码来接收它。如果您的数据不适合单个字节,您可以编写额外的自定义逻辑来对其进行编码和解码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-04-29
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多