您的位置:首页 > 其它

libusb数据传输文档 翻译

2010-12-20 22:35 344 查看
Synchronous and asynchronous device I/O

同步和异步的设备I / O

Introduction

介绍

If
you're using libusb in your application, you're probably wanting to
perform I/O with devices - you want to perform USB data transfers.

如果你正在使用libusb在你的应用程序,你可能想要执行I / O设备——你想作USB传输数据了。

libusb
offers two separate interfaces for device I/O. This page aims to
introduce the two in order to help you decide which one is more suitable
for your application. You can also choose to use both interfaces in
your application by considering each transfer on a case-by-case basis.

libusb提供了两个独立的接口为设备的I / O。这一页旨在介绍两项为了帮助你决定哪一个更适合你的程序。您也可以选择同时使用,根据具体情况分别使用在不同的地方。

Once you have read through the following discussion, you should consult the detailed API documentation pages for the details:

当你看过通过以下讨论,你应该找详细的API文档页面,为这些细节:

* Synchronous device I/O

*同步装置I / O

* Asynchronous device I/O

*异步设备I / O

Transfers at a logical level

逻辑传输

At a logical level, USB transfers typically happen in two parts. For example, when reading data from a endpoint:

在逻辑,的USB传输通常发生在两部分。例如,当阅读数据从一个端点:

1. A request for data is sent to the device

1。数据请求被送去设备

2. Some time later, the incoming data is received by the host

2。一段时间以后,主机接受到数据

or when writing data to an endpoint:

或在向一个端点
写数据
:

1. The data is sent to the device

1。数据被送去装置

2. Some time later, the host receives acknowledgement from the device that the data has been transferred.

2。主机一段时间后接受到数据

There
may be an indefinite delay between the two steps. Consider a fictional
USB input device with a button that the user can press. In order to
determine when the button is pressed, you would likely submit a request
to read data on a bulk or interrupt endpoint and wait for data to
arrive. Data will arrive when the button is pressed by the user, which
is potentially hours later.

可能会有无限的时间间隔两个步骤。考虑一个虚构的USB输入设备与一个按钮让用户进行控制。,你就按确定提交一份申请读的数据到一个体积或中断端点,等待数据到达的。数据到达主机的时间可能很久以后。

libusb
offers both a synchronous and an asynchronous interface to performing
USB transfers. The main difference is that the synchronous interface
combines both steps indicated above into a single function call, whereas
the asynchronous interface separates them.

libusb既提供了一同步和异步界面表演USB转移。主要的区别是同步接口,结合成一个单一的上面指出的步骤被调用的函数,而异步接口把他们分开了。

The synchronous interface

同步接口

The
synchronous I/O interface allows you to perform a USB transfer with a
single function call. When the function call returns, the transfer has
completed and you can parse the results.

同步I / O接口只有一个单一的函数。当函数调用返回时,传送完成了,你可以解析结果。

If
you have used the libusb-0.1 before, this I/O style will seem familar
to you. libusb-0.1 only offered a synchronous interface.

如果你已经用过,这libusb-0.1的I / O风格似乎熟悉libusb-0.1。只是提供了一种同步接口。

In our input device example, to read button presses you might write code in the following style:

在我们的输入装置,例如,你可能读按钮按写代码在接下来的风格:

unsigned char data[4];

无符号字符数据[4]。

int actual_length,

int r = libusb_bulk_transfer(handle, EP_IN, data, sizeof(data), &actual_length, 0);

if (r == 0 && actual_length == sizeof(data)) {

// results of the transaction can now be found in the data buffer

/ /数据现在已经在缓冲区了

// parse them here and report button press

/ /在这里解析数据

} else {

error();

}

}

The main advantage of this model is simplicity: you did everything with a single simple function call.

该模型的最大好处就是简单:你做的每一件事都有一个单一的简单的函数调用。

However,
this interface has its limitations. Your application will sleep inside
libusb_bulk_transfer() until the transaction has completed. If it takes
the user 3 hours to press the button, your application will be sleeping
for that long. Execution will be tied up inside the library - the entire
thread will be useless for that duration.

然而,这个接口,它是一个阻塞函数,程序会再次函数出阻塞。

Another
issue is that by tieing up the thread with that single transaction
there is no possibility of performing I/O with multiple endpoints and/or
multiple devices simultaneously, unless you resort to creating one
thread per transaction.

另一个问题是,你的为每一个数据操作创建一个线程

Additionally, there is no opportunity to cancel the transfer after the request has been submitted.

此外没有办法撤销已经发出的传输请求

For details on how to use the synchronous API, see the synchronous I/O API documentation pages.

有关如何使用同步API,看到同步I / O API文档页。

The asynchronous interface

异步界面

Asynchronous
I/O is the most significant new feature in libusb-1.0. Although it is a
more complex interface, it solves all the issues detailed above.

异步I / O最重要libusb-1.0新特点。尽管这是一个更复杂的界面,解决了上述所有的问题。

Instead
of providing which functions that block until the I/O has complete,
libusb's asynchronous interface presents non-blocking functions which
begin a transfer and then return immediately. Your application passes a
callback function pointer to this non-blocking function, which libusb
will call with the results of the transaction when it has completed.

而不是提供其功能,块,直至I / O拥有完备的,libusb的异步界面显示了阻塞的功能开始,然后立即返回转会。你的应用程序通过一个回调函数指针这个阻塞的功能,会打电话给libusb交易的结果在完成。

Transfers which have been submitted through the non-blocking functions can be cancelled with a separate function call.

转让提交了的通过调节功能,就能取消和一个单独的函数的电话。

The
non-blocking nature of this interface allows you to be simultaneously
performing I/O to multiple endpoints on multiple devices, without having
to use threads.

这种特性中的非分块界面,让您要同时执行I / O到多个端点在多种设备,而不需要使用线程。

This added flexibility does come with some complications though:

这增加了一些并发症弹性确实需要付出虽然:

*
In the interest of being a lightweight library, libusb does not create
threads and can only operate when your application is calling into it.
Your application must call into libusb from it's main loop when events
are ready to be handled, or you must use some other scheme to allow
libusb to undertake whatever work needs to be done.

*的兴趣是一种较轻的图书馆,libusb不会创造螺纹和能操纵的死亡时间当你的应用要求。你的应用程序必须叫进了libusb从它的主回路当事件准备处理方式,你还是要利用其他方案允许libusb进行一切工作需要做的事。

* libusb also needs to be called into at certain fixed points in time in order to accurately handle transfer timeouts.

* libusb也需要被称为在某一固定的时间点为了正确处理转移暂停。

*
Memory handling becomes more complex. You cannot use stack memory
unless the function with that stack is guaranteed not to return until
the transfer callback has finished executing.

*记忆体处理就变得更为复杂了。你不能用堆栈记忆的作用,除非是保证不会叠加才回来完成转会回调函数执行。

*
You generally lose some linearity from your code flow because
submitting the transfer request is done in a separate function from
where the transfer results are handled. This becomes particularly
obvious when you want to submit a second transfer based on the results
of an earlier transfer.

“你一般失去一些代码中线性流因为提交转会请求做是在一个单独的函数的转会结果处理。这变得尤为明显当你想递交第二转会结果基础上早前的转移。

Internally, libusb's synchronous interface is expressed in terms of function calls to the asynchronous interface.

libusb内部,同步接口是以异步实现的

For details on how to use the asynchronous API, see the asynchronous I/O API documentation pages.

有关如何使用异步API,看到异步I / O API文档页。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: