Detailed explanation of Android USB to serial communication development example

Detailed explanation of Android USB to serial communication development example

I haven't written for a long time. Years ago, the company opened a new project related to USB to serial communication. The demand is to use Android Tablet to communicate with several peripherals after USB transfer. I haven't been busy until recently. While I'm not busy this weekend, I recorded the basic process of USB to serial communication development.

We developed and used the USB host mode, that is, Android Tablet as the host and USB peripheral as the slave for data communication. The whole development process can be summarized as follows:

1. Equipment found

Through the class provided by the usbmanager system, we can list all USB devices currently connected. What we mainly need is the usbdevice object. The official notes on the usbdevice class are as follows:

Yes, this class represents the USB device connected to Android.

2. Turn on the device

Next, we need to open the USB device we just found. We can imagine the connection between the tablet and USB peripherals as a channel. The two sides can communicate only after the door of the channel is opened.

Generally speaking, when accessing a USB device for the first time on an Android device without customization, we do not have access permission by default. Therefore, we must first judge whether we have access permission to the currently opened USB device:

Here we declare a broadcast usbpermissionreceiver. After receiving the broadcast with successful authorization, we will do some other processing:

Next, we need to find the interface usbinterface with data transmission function, and find the data input and output port usbinterpoint from it. Generally, a USB device has multiple usbinterfaces, and we generally need the first one, so:

Similarly, a usbinterface has multiple usbendpoints, including control port and data port. Therefore, we need to find the two data input and output ports according to the type and data flow direction:

Finally, to really open the USB device, we need to establish a usbdevice connection with the USB peripheral. Its notes briefly explain its purpose:

Its acquisition is also very simple. Just one code:

Here, in theory, the connection between the tablet and USB peripherals has been established, and the data can be started. However, in most cases, we need to configure the USB serial port, such as baud rate, stop bit, data control, etc. otherwise, the data received will be garbled due to different configurations on both sides. The specific configuration depends on the serial port chip you use. At present, PL2303 and ch340 are popular. Due to the space problem, I'll send you a private letter from a friend who needs to configure the serial port code.

3. Data transmission

Here, we can transfer data with USB peripherals. First, let's see how to send data to USB devices.

1. Send data to USB peripheral

In the second step, we have obtained the data output port usbendpointin, through which we send data to the external device. See how to use:

Bulktransfer this function is used to transmit data at a given port. The first parameter is the port of this transmission. Here we use the output port. The second parameter is the data to be sent. The type is byte array. The third parameter represents the length of the data to be sent. The last parameter is timeout. The return value represents the number of bytes successfully sent. If - 1 is returned, That is, the sending failed.

2. Accept the data sent by USB peripherals

Similarly, we have found the data input port usbendpointin. Because the data input is irregular, we can open another thread to receive data. The code for receiving data is as follows:

The above is the basic process of USB to serial communication. It is not very comprehensive in some places. For example, there should be other methods to receive USB peripheral data. Please correct the deficiencies.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>