Android的 - 失去进入的(高速)USB数据 [英] Android - Losing incoming (hi-speed) USB data

查看:201
本文介绍了Android的 - 失去进入的(高速)USB数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在采用Android,我在我读同一设备/流中的Windows的时候不要失去进入的USB数据流的数据丢失。 (我知道Android是不是一个实时操作系统,但也不是Windows和Windows是有没有问题'跟上'的数据。)

When using Android, I'm losing data on an incoming USB data stream that I do not lose when reading the same device/stream in Windows. (I know that Android is not a real-time OS, but neither is Windows, and Windows is having no problem 'keeping up' with the data.)

我有数据进来在大约3.5MB /秒,使用一个FTDI 2232H芯片已经内置了4K缓冲区。中的libusb的bulk_transfer调用一次可以要求16K,因此Android需要收获USB缓冲区的内容,每4毫秒左右。

I have data coming in at about 3.5MB/sec using an FTDI 2232H chip which has a built in 4K buffer. The bulk_transfer calls in libusb can ask for 16K at a time, so Android needs to reap the contents of the USB buffer every 4ms or so.

我曾尝试:用Java编写和C,提高螺纹(和/或过程)的优先级,以它的最高,同步和异步程序,我甚至通过一个单独的缓冲区读取,所以我甚至不每个USB要复制的数据之间的连续读取。 (有没有垃圾回收在传送过程回事)。我只需要缓冲数据的20MB,所以它的所有的RAM。

I have tried: writing in Java and in C, raising the thread (and/or process) priority to it's highest, sync and async routines, and I even pass a separate buffer for each USB read so I don't even have to copy data between successive reads. (There is no garbage collection going on during the transfer.) I only need to buffer 20MB of data, so it's all to RAM.

不过,Android是没有得到身边到USB的数据,有时等待只要12毫秒读取之间,从而导致丢失一组数据。

Still, Android is 'not getting around' to the USB data, sometimes waiting as long as 12ms between reads, causing a bunch of data to be lost.

有没有人有什么想法? DMA?某种形式的'实时'的要求给内核?

Does anyone have any ideas? DMA? Some sort of 'real-time' request to the kernel?

推荐答案

我以前遇到过这样的问题。忘了使用Java,在后台它做的事情数不清数prevent实时访问,如垃圾收集,螺纹加工。也忘了使用事件驱动编程,即使在高优先级的线程,它可能需要很长的时间才处理该事件,并可能会丢失数据。

I've encountered this kind of problem before. Forget using Java, in the background it's doing untold number of things that prevent realtime access, e.g. garbage collection, thread processing. Also forget using event-driven programming, even in high priority threads, it can take a long time before the event is processed and you can lose data.

我固定它是写不友好code的方式!二手C或汇编,并写了这样的查询功能(在C类伪code):

The way I fixed it was to write "unfriendly" code! Used C or assembly, and wrote a polling function like this (in C-like pseudo-code):

#define PAUSE 2 /* Check twice as often as the packet rate */
#define TIMEOUT (500 / PAUSE) /* Abort if half a second of no data */

/* Provide handle, data buffer and size of buffer
   Returns TRUE if full buffer read, FALSE if not, data unread in size
*/ 
BOOL real_time_read(HANDLE handle, BYTE *data, size_t *size)
{
    BOOL result = FALSE;
    int timeout = TIMEOUT;

    set_thread_priority(REALTIME);

    while (is_handle_valid(handle))
    {
        if (is_data_pending(handle))
        {
            size_t count = get_data(handle, data, size);
            data += count;
            *size -= count;
            if (!*size)
            {
                result = TRUE;
                break;
            }
        }
        else if (!--timeout)
            break;

        /* Give a tiny time slice to other processes */
        usleep(PAUSE);
    }

    return result;
}

您提到你试过C,所以它应该是简单的将其转换为真正的功能。避免诱惑,利用方便的功能,你要尽量靠近金属越好。例如。如果O / S功能阅读()依次调用阅读()这反过来又要求 _read(),要使用 _read()。 该设备将同时这是怎么回事明显慢了,但是这是实时访问的权衡。

You mentioned you tried C, so it should be straightforward to convert this to real functions. Avoid the temptation to use convenience functions, you want as close to the metal as possible. E.g. if an O/S function Read() in turn calls read() which in turn calls _read(), you want to be using _read(). The device will be noticeably slower while this is going on, but that's the tradeoff of real-time access.

这篇关于Android的 - 失去进入的(高速)USB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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