Return to site

Download USB Serial (Communication Class

broken image


  1. Serial.println('Hello world'); delay(1000); Upload this piece of code to Arduino, Arduino would send the string 'Hello World' to the serial port for every 1 second. Use Serial Monitor of the Arduino IDE to see the serial content You can use the Serial Monitor of the Arduino IDE to check the serial content that are sent by Arduino.
  2. Class UART – duplex serial communication bus¶ UART implements the standard UART/USART duplex serial communications protocol. At the physical level it consists of 2 lines: RX and TX. The unit of communication is a character (not to be confused with a string character) which can be 8 or 9 bits wide. UART objects can be created and initialised.
  1. Download Usb Serial (communication Class Of Lies
  2. Usb Serial Adapter
USB-RS232 Serial Converter Driver Download

The second reason to have USB is for serial communication — the ability to transfer data (e.g. Strings) in and out between the device and a host computer through the USB port. This is useful especially for debugging (i.e. Printing values to a serial monitor), unfortunately this feature is missing in most V-USB projects.

Windows 7 64-bit & 32-bit, Windows Vista 64-bit & 32-bit, Windows XP, Windows 2000, Windows Me, Windows 98

Important note regarding Windows 7 and Vista:
The driver MUST be installed 'as Administrator'
Right-click the downloaded EXE file and choose Run as administrator

Download the Windows installer for the USB RS-232 adaptor:
Download for Windows 7/Vista/XP (32-bit)
Download for Windows 7/Vista (32-bit and 64-bit)

If for some reason the above driver does not work, try one of the following:
Download for Windows 7/Vista/XP (32-bit and 64-bit - Alternate driver)
Download for Windows XP, 2000, 98/ME (32-bit only)

For older cables (purchased prior to October, 2010):
Download for Windows 7/Vista/XP (32-bit and 64-bit)
Download for Windows Vista (32-bit only)

Run the installer, then after rebooting, plug the USB end of the adapter into one of the USB ports on the computer.

NOTE: For Windows 7, please use RC build 7100 or RTM Final version. (Installer program will not run on early Windows 7 or Beta versions)

Check the software driver installation

1) Open the Device Manager dialog from Control Panel

  • Windows Vista: Start >> Control Panel >> System >> Hardware >> Device Manager
  • Windows XP: Start >> Control Panel >> System >> Hardware >> Device Manager
  • Windows 2000: Start >> Settings >> Control Panel >> System >> Hardware >> Device Manager
  • Windows 98/ME: Start >> Settings >> Control Panel >> System >> Device Manager

2) Click the plus sign (+) next to 'Ports'.

3) If the device is properly installed, 'Serialio.com USB Serial Port' or 'Prolific USB-to-Serial COM Port' will be displayed.


The software driver is properly installed, but the USB-RS232 adaptor does not work as expected

One of the common reasons that software may not work as expected with a USB-Serial adaptor when the devcie is properly installed, is many older programs (and even some new ones) only support a limited number of COM ports (e.g. only COM1 - COM4)

If the USB-RS232 adapter is installed as COM5, it may need to be reassigned to COM1 - COM4 to work with some software.

The following outlines how to to reassign the COM port on Windows XP/2K/Me/98SE

Windows XP

1) Follow steps 1-3 of 'Check the software driver installation'above.

Usb communication driver

2) Right-click on the 'Prolific USB to Serial Port', then click Properties

3) Click on the 'Port Settings' tab. Click the 'Advanced' button.

4) In the 'COM Port Number' drop-down select the desired port number. For example if the software requires COM2, select COM2. Note the COM port must have '(in use)' listed next to it, if it does then another device with that name is already installed in the system. After selecting the desire port number, click 'OK.'

5) Click 'OK' again, and the device will show up as being on the same COM port that it was before (i.e., COM5), but should show up on the selected port name when the Device Manager dialog is closed and opened again.

6) Close the Device Manager. Launch the software to use with the new COM port.

Windows 2000, Me, and 98 SE (Second Edition)

1) Follow steps 1 2 of 'Check the software driver installation'above.

2) Right-click on the 'Prolific USB to Serial Port', then click Properties.

3) Click on the 'Port Settings' tab. Click the 'Advanced' button.

4) Uncheck 'Use automatic settings'.

5) Select 'Input/Output range'. Click on 'Change Settings'.

6) Type '02E8-02EF' in the Value box. This value will change the port to COM 4. If COM 4 is in use, you must choose another port. The values for each port are.

  • COM 1: 03F8-03FF
  • COM 2: 02F8-02FF
  • COM 3: 03E8-03EF
  • COM 4: 02E8-02EF

7) Enter the value, then click OK. Click OK again, then click Yes when the 'Creating a Forced Configuration' window appears. Click OK.

8) Restart the computer. The Device Manager can be checked, to see the COM port change.

Download Usb Serial (communication Class Of Lies

Note: Adaptor may not be exactly as pictured above.
back

Android Things now supports USB Host (since DP3), which allows a user space Android application to talk to a custom USB device.

To discover this feature, we will create a custom USB sensor, and forward all events to the Android Things board over USB.

You can see below a video of what we will achieve: the USB device will be an Arduino NFC reader. When scanning a tag (or an amiibo), the Arduino will forward NFC data to the Android Things board over USB.


Attention, please

There are two different ways to communicate with a USB device via Android Things.

  • If the device is a USB-Serial and if it reports a /dev/tty* device handle through the kernel, then you won't need to use any USB Host API. Instead, try to call PeripheralManager.getUartDeviceList(). If you can see a new UART endpoint, this means that you can directly use the UART API to communicate with the USB device. No need to use USB APIs at all (if you don't need those).

  • If the device does not report a new /dev/tty* after being plugged in (e.g. the kernel doesn't have the driver built-in), you are forced to use the USB layer. You will need to manipulate the USB Host API, which allows a regular user space application to communicate with USB devices without root privileges or support needed from the Linux kernel.

In a previous post, we already saw how to communicate with a serial device over UART.
This time, we will use the USB Host API, and the serial communication will be performed using a third party library. That way, the code we will write here will not be specific to Android Things, but will also work on any Android (3.1 and above) smartphone.

Flashing the Arduino

We will start with something simple: we first want the Arduino to continuously send the 'Hello!' value to the serial port every second, at a 115 200 baud rate.

When we connect the Arduino to the Android Things board, we expect our app to receive the 'Hello!' text every second. And for that, we'll first have to edit the AndroidManifest.xml.

AndroidManifest.xml

We want to be notified when an external USB device is attached to the Android device.
This can be done adding a new intent-filter entry to the Activity that should be started by the system when a USB device is plugged in.
Also, we will add a meta-data element pointing to an external XML resource file (here in res/xml/device_filter.xml) that declares identifying information about the device that we want to detect.

If the device_filter.xml file has the following content, we will be notified each time any USB device is plugged in:

This is not exactly what we want. We only want to be notified when the Arduino is plugged in, and ignore all other USB devices, so we will add a specific rule.

After connecting the Arduino to the Android Things board, we can consult the kernel logs using dmesg to get some device information:

We only want to be notified when the Arduino (idVendor=0x2341) is connected, so we will specify this vendor id into the usb-device tag:

Note here that the vendor-id expects a decimal value, not a hexadecimal one.

This filter will be enough for us. For a complete list of what we can do, please refer to the USB Host documentation

Starting a USB connection

Our Activity now receives an intent each time the Arduino is plugged in.

We will first list all the connected USB devices, and open a USB connection if the Arduino was found:

The startSerialConnection method will use felHR85's USBSerial library to open a serial connection between the Arduino and the Android device:

The UsbSerialDevice.read() method takes an UsbReadCallback reference which will be called each time data is received. Here is a simple implementation:

We convert the byte[] data to a UTF-8 String, and log this data.

Now, each time data is sent from the Arduino over USB, the callback is triggered and data is logged. We can run adb logcat to confirm that we receive the 'Hello!' message from the Arduino every second:

For a complete and optimized source code, take a look at github.com/Nilhcem/usbfun-androidthings.

Adding some more fun

Printing 'Hello!' to the logs every second is quite boring.
A fast way to add some fun to this project is to connect an NFC module to the Arduino and send NFC tag data from this module to the Android Things board over USB.

I used Elechouse's PN532 Module over SPI, using this sketch.

To simplify, I am only forwarding the tag ID to the Android Things board. And the Android app displays an appropriate image depending on the received ID.

Porting this project to an Android smartphone

As explained in the introduction, the project uses the USB Host API from the Android SDK, and is therefore fully compatible with any Android smartphones or tablets (minSdk=12). If you own a USB-OTG cable, you can plug an Arduino directly to your phone.

Below, a demonstration (with sound) of an Android phone, sending data to an Arduino over USB to play music on a piezo buzzer. (source code is available here)


There's still one difference however: when a USB device is connected, a UI dialog is shown and the user needs to grant the USB permission to access the device. Checking whether the permission is granted was skipped on the Android Things source code project we wrote, as, similar to runtime permissions, you don't have to check/request USB permission on Android Things since there may be no display attached, and therefore, no ways for users to grant those permissions. They are allowed by default.

Going further

A better example of what you can do with the USB Host API is the official USB enum sample.
This project iterates over all the USB devices discovered by the host and prints their interfaces and endpoints.

Reading the source code is interesting here as we can learn about USB descriptors.

Usb Serial Adapter

For example, after a USB device has been detected, it is opened using the same API we used in this article (via UsbManager.openDevice()), then, instead of opening a serial connection, the code immediately queries the USB device and configuration(s) descriptors:

When reading the USB specs, it is specified that all USB devices must support at least a default endpoint. Any transfer that targets this endpoint is called a control transfer, and is a way for the host to obtain device information.

  • 0x80 is the data direction. Here, it means 'IN' (from the USB device to the host)
  • 0x06 is the request type to get a descriptor
  • 0x100 is for a device descriptor (0x200 for a configuration descriptor)
  • 0x00 is the descriptor index (default descriptor)

Then, the code receives data from the buffer, as specified in the documentation.

And so on. Once we understand that, the sample code is much easier to read.

Download usb serial (communication class 9

2) Right-click on the 'Prolific USB to Serial Port', then click Properties

3) Click on the 'Port Settings' tab. Click the 'Advanced' button.

4) In the 'COM Port Number' drop-down select the desired port number. For example if the software requires COM2, select COM2. Note the COM port must have '(in use)' listed next to it, if it does then another device with that name is already installed in the system. After selecting the desire port number, click 'OK.'

5) Click 'OK' again, and the device will show up as being on the same COM port that it was before (i.e., COM5), but should show up on the selected port name when the Device Manager dialog is closed and opened again.

6) Close the Device Manager. Launch the software to use with the new COM port.

Windows 2000, Me, and 98 SE (Second Edition)

1) Follow steps 1 2 of 'Check the software driver installation'above.

2) Right-click on the 'Prolific USB to Serial Port', then click Properties.

3) Click on the 'Port Settings' tab. Click the 'Advanced' button.

4) Uncheck 'Use automatic settings'.

5) Select 'Input/Output range'. Click on 'Change Settings'.

6) Type '02E8-02EF' in the Value box. This value will change the port to COM 4. If COM 4 is in use, you must choose another port. The values for each port are.

  • COM 1: 03F8-03FF
  • COM 2: 02F8-02FF
  • COM 3: 03E8-03EF
  • COM 4: 02E8-02EF

7) Enter the value, then click OK. Click OK again, then click Yes when the 'Creating a Forced Configuration' window appears. Click OK.

8) Restart the computer. The Device Manager can be checked, to see the COM port change.

Download Usb Serial (communication Class Of Lies

Note: Adaptor may not be exactly as pictured above.
back

Android Things now supports USB Host (since DP3), which allows a user space Android application to talk to a custom USB device.

To discover this feature, we will create a custom USB sensor, and forward all events to the Android Things board over USB.

You can see below a video of what we will achieve: the USB device will be an Arduino NFC reader. When scanning a tag (or an amiibo), the Arduino will forward NFC data to the Android Things board over USB.


Attention, please

There are two different ways to communicate with a USB device via Android Things.

  • If the device is a USB-Serial and if it reports a /dev/tty* device handle through the kernel, then you won't need to use any USB Host API. Instead, try to call PeripheralManager.getUartDeviceList(). If you can see a new UART endpoint, this means that you can directly use the UART API to communicate with the USB device. No need to use USB APIs at all (if you don't need those).

  • If the device does not report a new /dev/tty* after being plugged in (e.g. the kernel doesn't have the driver built-in), you are forced to use the USB layer. You will need to manipulate the USB Host API, which allows a regular user space application to communicate with USB devices without root privileges or support needed from the Linux kernel.

In a previous post, we already saw how to communicate with a serial device over UART.
This time, we will use the USB Host API, and the serial communication will be performed using a third party library. That way, the code we will write here will not be specific to Android Things, but will also work on any Android (3.1 and above) smartphone.

Flashing the Arduino

We will start with something simple: we first want the Arduino to continuously send the 'Hello!' value to the serial port every second, at a 115 200 baud rate.

When we connect the Arduino to the Android Things board, we expect our app to receive the 'Hello!' text every second. And for that, we'll first have to edit the AndroidManifest.xml.

AndroidManifest.xml

We want to be notified when an external USB device is attached to the Android device.
This can be done adding a new intent-filter entry to the Activity that should be started by the system when a USB device is plugged in.
Also, we will add a meta-data element pointing to an external XML resource file (here in res/xml/device_filter.xml) that declares identifying information about the device that we want to detect.

If the device_filter.xml file has the following content, we will be notified each time any USB device is plugged in:

This is not exactly what we want. We only want to be notified when the Arduino is plugged in, and ignore all other USB devices, so we will add a specific rule.

After connecting the Arduino to the Android Things board, we can consult the kernel logs using dmesg to get some device information:

We only want to be notified when the Arduino (idVendor=0x2341) is connected, so we will specify this vendor id into the usb-device tag:

Note here that the vendor-id expects a decimal value, not a hexadecimal one.

This filter will be enough for us. For a complete list of what we can do, please refer to the USB Host documentation

Starting a USB connection

Our Activity now receives an intent each time the Arduino is plugged in.

We will first list all the connected USB devices, and open a USB connection if the Arduino was found:

The startSerialConnection method will use felHR85's USBSerial library to open a serial connection between the Arduino and the Android device:

The UsbSerialDevice.read() method takes an UsbReadCallback reference which will be called each time data is received. Here is a simple implementation:

We convert the byte[] data to a UTF-8 String, and log this data.

Now, each time data is sent from the Arduino over USB, the callback is triggered and data is logged. We can run adb logcat to confirm that we receive the 'Hello!' message from the Arduino every second:

For a complete and optimized source code, take a look at github.com/Nilhcem/usbfun-androidthings.

Adding some more fun

Printing 'Hello!' to the logs every second is quite boring.
A fast way to add some fun to this project is to connect an NFC module to the Arduino and send NFC tag data from this module to the Android Things board over USB.

I used Elechouse's PN532 Module over SPI, using this sketch.

To simplify, I am only forwarding the tag ID to the Android Things board. And the Android app displays an appropriate image depending on the received ID.

Porting this project to an Android smartphone

As explained in the introduction, the project uses the USB Host API from the Android SDK, and is therefore fully compatible with any Android smartphones or tablets (minSdk=12). If you own a USB-OTG cable, you can plug an Arduino directly to your phone.

Below, a demonstration (with sound) of an Android phone, sending data to an Arduino over USB to play music on a piezo buzzer. (source code is available here)


There's still one difference however: when a USB device is connected, a UI dialog is shown and the user needs to grant the USB permission to access the device. Checking whether the permission is granted was skipped on the Android Things source code project we wrote, as, similar to runtime permissions, you don't have to check/request USB permission on Android Things since there may be no display attached, and therefore, no ways for users to grant those permissions. They are allowed by default.

Going further

A better example of what you can do with the USB Host API is the official USB enum sample.
This project iterates over all the USB devices discovered by the host and prints their interfaces and endpoints.

Reading the source code is interesting here as we can learn about USB descriptors.

Usb Serial Adapter

For example, after a USB device has been detected, it is opened using the same API we used in this article (via UsbManager.openDevice()), then, instead of opening a serial connection, the code immediately queries the USB device and configuration(s) descriptors:

When reading the USB specs, it is specified that all USB devices must support at least a default endpoint. Any transfer that targets this endpoint is called a control transfer, and is a way for the host to obtain device information.

  • 0x80 is the data direction. Here, it means 'IN' (from the USB device to the host)
  • 0x06 is the request type to get a descriptor
  • 0x100 is for a device descriptor (0x200 for a configuration descriptor)
  • 0x00 is the descriptor index (default descriptor)

Then, the code receives data from the buffer, as specified in the documentation.

And so on. Once we understand that, the sample code is much easier to read.

Conclusion

This post introduced an example of how to use the USB Host API with Android Things.
In a real-life scenario, you won't need to use an Arduino to bring NFC support to Android Things, as you can develop the NFC driver directly, and even if you need to, you may be able to communicate with the Arduino using the UART API directly. You may not need to use the USB Host API + a third party dependency.

If you own a USB-Serial device and if this latter is recognized by the kernel, then using the UART API is the easiest and recommended way to communicate between both devices. But if you need to access any kind of USB device from an app, then you'll be happy to have a user-space USB support on Android Things.

As Marcos Placona explained in a blog post, some components need super-fast drivers (sending signals that alternate from vcc to ground in a few microseconds). While this is not possible yet with Android Things, you could use a microcontroller that acts as a proxy and directs data to the Android Things board over USB, to use those components in your Android Things projects.





broken image