用于 Android 和 Arduino Uno 之间 USB 串行通信的读取器线程 [英] Reader thread for USB serial communication between Android and Arduino Uno

查看:57
本文介绍了用于 Android 和 Arduino Uno 之间 USB 串行通信的读取器线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 android 的 USB 主机 API 在 Arduino UNO 和我的 android 智能手机之间进行通信.到目前为止一切都很好.我每秒通过函数 Serial.print() 从我的 Arduino 发送一个测试字符串Try data".两个设备的波特率都设置为 9600.Android 智能手机充当主机.

我使用的一些教程是:

http://android.serverbox.ch/?p=549

http://android-er.blogspot.in/2014/09/send-data-from-android-to-arduino-uno.html

以及 android usb 主机文档.我已经尝试过 mik3y 的 physicaloid 和另一个库,但它们不起作用.在这两种情况下设备都不会打开.

我正在使用一个线程来接收来自 Arduino 的数据(下面的所有代码都在 onCreate() 中

val reader = object : Thread(){覆盖乐趣运行(){var buf = ByteArray(endpointIn.maxPacketSize)connection.bulkTransfer(endpointIn, buf, buf.size, 0)tv.text = String(buf)//tv是一个textview的id}}阅读器开始()

问题是只接收到来自测试字符串的字母 T.如果相反,我将 buf 的声明移出线程类,则只会读取 4 个字符.我无法理解为什么只读取有限的字符.如果我将 run 方法的最后 2 行代码放入无限 while 循环中,应用程序就会崩溃(无法弄清楚原因).我知道线程似乎同时运行,但实际上它的主线程运行了一段时间,然后是读取器,它们两个都不会同时运行.它的转换非常快,因此,它们看起来好像是同时运行的.这可能是某些信息丢失的原因吗?

请帮助编写读者线程的代码或者有什么更好的方法吗?

提前致谢

解决方案

好不容易弄明白了.即使函数调用中的参数为 64,Android 手机也不会在每次批量传输中读取 64 字节.这是因为线程执行得更快.它有时读取 4 个字节,有时读取 2 个字节.解决方案是:使用环形缓冲区或仅使用 substring() 方法来获取相关字符.修改后的代码如下所示:

val reader = object : Thread(){覆盖乐趣运行(){var buf = ByteArray(64)而(真){var len = connection.bulkTransfer(endpointIn, buf, buf.size, 0)如果(长度> 0){val msg = String(buf)tvAppend(msg.substring(0, len))//tv是sv里面的textviewsv.fullScroll(View.FOCUS_DOWN)//sv 是一个滚动视图}}}}阅读器开始()

I am trying to communicate between Arduino UNO and my android smartphone using USB Host API of android. So far everything is good. I am sending a test string "Try data" from my Arduino through the function Serial.print() every second. Baud rate has been set to 9600 for both the devices. Android smartphone is acting as Host.

Some tutorials I used are :

http://android.serverbox.ch/?p=549

http://android-er.blogspot.in/2014/09/send-data-from-android-to-arduino-uno.html

along with the android usb host documentation. I have tried physicaloid and another library by mik3y but they are not working. Device doesn't open in both the cases.

I am using a thread over which I receive data from Arduino(all code below is in onCreate()

val reader = object : Thread(){
        override fun run() {
            var buf = ByteArray(endpointIn.maxPacketSize)
            connection.bulkTransfer(endpointIn, buf, buf.size, 0)
            tv.text = String(buf)//tv is the id of a textview 
        }
    }
 reader.start()

The problem is that only letter T from the test string is being received. If instead, I move the declaration of buf out of the thread class, only 4 characters are being read. I am unable to understand why only limited characters are being read. If I instead put the last 2 lines of code of the run method in an infinite while loop, the app crashes (not able to figure out why). I know that threads appear to be running simultaneously but actually its the main thread running for some time and then the reader, both of them don't run at the same time. Its the transition which is very fast and thus, they appear as if they appear to be running simultaneously. Can this be the reason why some information is being lost?

Please help in writing the code for reader thread or is there any better way to do that?

Thanks in advance

解决方案

Finally after lot of time, I figured it out. The android phone doesn't read 64 bytes in every bulk transfer even though the parameter in the function call is 64. This is because the thread is executing much faster. It reads sometimes 4, sometimes 2 bytes. The solution is : use a ring buffer or just use the substring() method to get the relevant characters. The modified code looks like this :

val reader = object : Thread(){
        override fun run() {
            var buf = ByteArray(64)
            while(true){
                var len = connection.bulkTransfer(endpointIn, buf, buf.size, 0)
                if(len > 0) {
                    val msg = String(buf) 
                    tvAppend(msg.substring(0, len)) //tv is the textview inside sv
                    sv.fullScroll(View.FOCUS_DOWN) //sv is a scrollview
                }
            }
        }
    }
reader.start()

这篇关于用于 Android 和 Arduino Uno 之间 USB 串行通信的读取器线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆