简体   繁体   English

从Android中的USB串行端口读取数据

[英]reading data from usb serial port in Android

I use prolifit PL23023 chip for send and receive data to my android app. 我使用prolifit PL23023芯片向我的android应用发送和接收数据。 I Handled driver thing about pl2303 and I can write and read data over serial. 我处理了有关pl2303的驱动程序,并且可以通过串行方式读写数据。 but the problem is when for example I receive a 15 byte data,its read in a 11byte data and a 4byte data. 但是问题是,例如当我收到一个15字节的数据时,它读入了11字节的数据和4字节的数据。 no data loss,its give me all data but in a 11byte packets. 没有数据丢失,它给了我所有数据,但是只有一个11byte的数据包。 I controlled every thing about buffer size and it seems no problem. 我控制了有关缓冲区大小的所有内容,似乎没有问题。 This is method that reads data: 这是读取数据的方法:

    @Override
    public int read(byte[] dest, int timeoutMillis) throws IOException {
      //  synchronized (mReadBufferLock) {
            int readAmt = Math.min(dest.length, mReadBuffer.length);

            int numBytesRead = mConnection.bulkTransfer(mReadEndpoint, mReadBuffer,
                    readAmt, timeoutMillis);
            if (numBytesRead < 0) {
                return 0;
            }
            System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
            return numBytesRead;
        //}
    }

and bulkTransfer() method finally executed in native methods and we do not know about what is going in bulkTransfer(). 和bulkTransfer()方法最终在本机方法中执行,而我们不知道bulkTransfer()会发生什么。

and interesting thing about this functionality is that when I put a break point in first of this line. 关于此功能的有趣之处在于,当我在该行的第一行放置一个断点时。

   int numBytesRead = mConnection.bulkTransfer(mReadEndpoint, mReadBuffer,
                    readAmt, timeoutMillis);

and run the program in Debug mode, the value of numBytesRead in 15 byte example sets to 15 and every thing is ok! 并在Debug模式下运行该程序,将15字节示例中的numBytesRead的值设置为15,一切正常! I dont know what is happening. 我不知道发生了什么。 in debug mode I can get more than 11 byte packets but in normal mode I can get only 11 byte packets. 在调试模式下,我可以获得11个字节以上的数据包,但在正常模式下,我只能获取11个字节的数据包。

Nothing is wrong with the system, only with the expectations. 系统没有错,只有期望。 This is the nature of serial ports - data arrives at the timing with which the source sends it, which could be timing that keeps the line fully busy, or potentially years between characters. 这就是串行端口的本质-数据到达时是源发送的时间,这可能是使线路完全繁忙的时间,或者可能是字符之间的年限

Independently of that, your USB implementation queries the USB converter chip for any data it has received, and it will give you whatever it has up to that time. 独立于此,您的USB实现将向USB转换器芯片查询其接收到的任何数据,并且它将为您提供到那时为止的所有数据。 It cannot know if more is on the way from the source, so it merely gives you what it has up to that time. 它无法确定从源头开始是否还有更多东西,因此它仅能为您提供到那时为止的一切。

If you want to reliably obtain complete "messages" you will need to keep reading and concatenating the results until you reach whatever condition defines the end of a "message" for you - for example, a newline character. 如果您想可靠地获取完整的“消息”,则需要继续阅读并连接结果,直到达到为您定义“消息”末尾的任何条件(例如换行符)为止。 (It is possible this will mean that one read contains the end of one message and the start of a next.) As you have seen, slowing your program down can increase the chances that a full "message" has been received at the converter by the time you do the first read, but that isn't reliable (also keep in mind that the maximum data size for one USB read in this mode is fairly small). (这可能意味着一次读取包含一条消息的结尾和下一条消息的开头。)如您所见,放慢程序速度,可以增加在转换器处接收到完整“消息”的机会,进行第一次读取的时间,但这并不可靠(还要记住,在此模式下一次USB读取的最大数据量很小)。

Normally you could do this reading in a loop, but beware on Android that you must not execute blocking operations on the UI thread, so you will probably need to do this collection in a background thread. 通常,您可以循环读取此内容,但是请注意,在Android上一定不要对UI线程执行阻止操作,因此您可能需要在后台线程中执行此收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM