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

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

问题描述

我正在尝试使用android的USB Host 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尝试了类固醇和另一个库,但它们无法正常工作.两种情况下都无法打开设备.

我正在使用一个线程,通过它我可以从Arduino接收数据(下面的所有代码都在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()

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

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

预先感谢

解决方案

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

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()

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天全站免登陆