Git Product home page Git Product logo

sample-uartloopback's Introduction

UART Loopback

This sample demonstrates basic data read and write operations over a UART. The application creates a loopback interface that immediately echoes any data read from the receive (RX) buffer to the transmit (TX) buffer.

Note: The Android Things Console will be turned down for non-commercial use on January 5, 2022. For more details, see the FAQ page.

Screenshots

UART Loopback sample demo

(Watch the demo on YouTube)

Pre-requisites

  • Android Things compatible board with an available UART port
  • Android Studio 2.2+
  • FTDI TTL-232R or compatible USB-TTL converter

Schematics

Schematics for Raspberry Pi 3

NOTE: Raspberry Pi 3 shares the UART pins between multiple ports, including the serial debugging console. Refer to the mode matrix for more details.

Check in the USB TTL cable's documentation for the TX and RX wire colors:

  • connect the cable TX wire to the board RX pin
  • connect the cable RX wire to the board TX pin

Build and install

  1. On Android Studio, click on the "Run" button.

If you prefer to run on the command line, type

./gradlew installDebug
adb shell am start com.example.androidthings.loopback/.LoopbackActivity
  1. Plug the USB-TTL converter into a host PC, start your favorite terminal program (minicom, screen, RealTerm, etc.), and connect to the USB-TTL port at 115200 baud.

  2. Type characters into the terminal and observe them echo back to you from the device.

License

Copyright 2016 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

sample-uartloopback's People

Contributors

atn832 avatar irataxy avatar mangini avatar proppy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sample-uartloopback's Issues

uart write behavior

Hi,

I want to know if it's possible to know when the uart has transmitted the data out of the interface.

My experience is that uart write method buffers the data, but I need to know the exact moment that there are no more bytes to send so I can toggle a GPIO. One way is to make write method blocking, which I understand is not possible.

I was trying to see if the native API offer additional control over this so I can write my low level app in C++ but it seems there is no additional uart core support different than AT API.

Will appreciate your comments,

The length of each receiving of UART

I'm going to use android things to implement the modbus protocol,But I've found that when android things receives a piece of data, it's automatically divided into many segments. Is there any way to get it to be done like a single chip?
image
image

UART callback not triggered in Raspberry pi running dp6.0?

UART callbacks not triggering on Raspberry Pi, running Android Things DP 6.0. Do we need edit the config or cmdline files to change serial & bluetooth parameters, as we did in previous developer previews? I use a USB serial software called CoolTerm for Mac to check the connection. I also checked with a UART device ( Rs232 converted to UART) too. It is not working. The connections are correct. When the Rapsberry pi boots, I am able to read console log from the Usb serial software.
log:
............
[ 3.591832] usb 1-1: New USB device found, idVendor=0424, idProduct=9514
[ 3.591837] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 3.595054] hub 1-1:1.0: USB hub found
[ 3.595113] hub 1-1:1.0: 5 ports detected
[ 3.606690] mmc1: new high speed SDIO card at address 0001
[ 3.622230] Key type dns_resolver registered
[ 3.627361] Registering SWP/SWPB emulation handler
[ 3.632860] registered taskstats version 1
[ 3.640041] runtimepinconfig: UART0 stopping at 2 pins out of 3
[ 3.646069] runtimepinconfig: UART0 stopping at 2 pins out of 3
rpi3:/ $

But unable to get the UART callback when try to give input either from the serial software or from UART device.

Android things using UART on Intel Edison

Hi,
The sample was tried using an Intel Edison but it seems not to work at all.

image

The data you are seeing is not sent by me.

The code for transferUartData() is mentioned below

private void transferUartData() {
        if (mLoopbackDevice != null) {
            // Loop until there is no more data in the RX buffer.
            try {
                //Log.e(TAG, "Reading1");
                byte[] buffer = new byte[CHUNK_SIZE];
                mLoopbackDevice.write(buffer, 45);
                int read;
                while ((read = mLoopbackDevice.read(buffer, buffer.length))!=0) {
                    mLoopbackDevice.write(buffer, read);
                    Log.e(TAG, String.valueOf(read));
                }
            } catch (IOException e) {
                Log.w(TAG, "Unable to transfer data over UART", e);
            }
        }
    }

And I am not sending anything through the cable.

If possible, guide me to implement the same thing on an Intel Edison on an Arduino breakout board. Do I need to do any extra steps for the UART or anything will be helpful.

Cannot receive data on RPI3 (cannot write to communication pipe)

Hello, I tried this sample and it does not work for me.
I tried it on RPI3 with Android Things developer preview 4.
I turned off the UART debugging mode of RP3 by changing "console=serial0,115200" to "console=tty0" in cmdline.txt, and adding "dtoverlay=pi3-disable-bt" and removing "enabled_uart=1" & "core_freq=400" in config.txt file.

When I run this sample, I typed some characters in terminal program but I cannot get any echo back.

From the logs, I guess there are some problems opening some files related to communication pipe.
Can you give me any ideas to solve this situation?
Thanks

Below is the logs from logcat.

06-14 00:34:15.117 7959-7959/? E/cutils-trace: Error opening trace file: No such file or directory (2)
                                               
                                               [ 06-14 00:34:15.145   156:  156 I/         ]
                                               free_cache(3075) avail 2575781888
06-14 00:34:15.268 7959-7959/? W/art: Unknown instruction set features for ARM CPU variant (generic) using conservative defaults
06-14 00:34:15.283 7959-7959/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
                                      
                                      [ 06-14 00:34:15.295   156:  156 I/         ]
                                      free_cache(28380) avail 2575777792
06-14 00:34:15.358 7959-7959/? E/memtrack: Couldn't load memtrack module (No such file or directory)
06-14 00:34:15.358 7959-7959/? E/android.os.Debug: failed to load memtrack module: -2
06-14 00:34:15.361 7959-7959/? I/Radio-JNI: register_android_hardware_Radio DONE
06-14 00:34:15.397 7959-7959/? D/AndroidRuntime: Calling main entry com.android.commands.wm.Wm
06-14 00:34:15.402 7959-7959/? D/AndroidRuntime: Shutting down VM
06-14 00:34:15.756 156-156/? I/SELinux: SELinux: Loaded file_contexts contexts from /file_contexts.bin.
06-14 00:34:15.758 410-435/system_process I/PackageManager.DexOptimizer: Running dexopt (dex2oat) on: /data/app/vmdl1792012311.tmp/base.apk pkg=com.example.androidthings.loopback isa=arm vmSafeMode=false debuggable=true target-filter=interpret-only oatDir = /data/app/vmdl1792012311.tmp/oat sharedLibraries=/system/framework/com.google.android.things.jar
06-14 00:34:15.891 8038-8038/? W/dex2oat: Unknown instruction set features for ARM CPU variant (generic) using conservative defaults
06-14 00:34:15.892 8038-8038/? I/dex2oat: /system/bin/dex2oat --compiler-filter=interpret-only --debuggable
06-14 00:34:15.892 8038-8038/? E/cutils-trace: Error opening trace file: No such file or directory (2)
06-14 00:34:16.066 8038-8038/? I/dex2oat: dex2oat took 175.564ms (threads: 4) arena alloc=9KB (9800B) java alloc=111KB (114104B) native alloc=1199KB (1228760B) free=1360KB (1392680B)
                                          
                                          --------- beginning of system
06-14 00:34:16.281 410-435/system_process E/PackageManager: Backup Manager not found!
06-14 00:34:16.303 410-435/system_process I/art: Starting a blocking GC Explicit
06-14 00:34:16.429 410-435/system_process I/art: Explicit concurrent mark sweep GC freed 24161(1639KB) AllocSpace objects, 26(868KB) LOS objects, 33% free, 6MB/9MB, paused 2.442ms total 125.404ms
                                                 
                                                 [ 06-14 00:34:16.430   156:  156 E/         ]
                                                 Couldn't opendir /data/app/vmdl1792012311.tmp: No such file or directory
06-14 00:34:16.434 410-988/system_process I/System.out: Success
06-14 00:34:16.489 410-449/system_process I/InputReader: Reconfiguring input devices.  changes=0x00000010
06-14 00:34:16.557 410-423/system_process W/Searchables: No global search activity found
06-14 00:34:16.593 809-8050/com.google.android.gms I/PeopleContactsSync: triggerPendingContactsCleanup: no accounts
06-14 00:34:16.600 809-8050/com.google.android.gms D/Wear_Controller: Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.example.androidthings.loopback
06-14 00:34:16.696 809-911/com.google.android.gms I/Icing: Usage reports 0 indexed 0 rejected 0 imm upload false
06-14 00:34:16.706 809-911/com.google.android.gms I/Icing: Usage reports 0 indexed 0 rejected 0 imm upload false
06-14 00:34:16.712 809-8051/com.google.android.gms W/IcingInternalCorpora: getNumBytesRead when not calculated.
06-14 00:34:17.715 809-7908/com.google.android.gms I/Icing: Indexing 552A18E96A12CBAD55F637AC8023C0E0F78C68F3 from com.google.android.gms
06-14 00:34:17.794 809-7908/com.google.android.gms D/Icing: Loaded CLD2 Version V2.0 - 20141016
06-14 00:34:17.804 809-7908/com.google.android.gms I/Icing: Indexing done 552A18E96A12CBAD55F637AC8023C0E0F78C68F3
06-14 00:34:18.209 8054-8054/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 2000 <<<<<<
06-14 00:34:18.219 8054-8054/? D/AndroidRuntime: CheckJNI is OFF
06-14 00:34:18.220 8054-8054/? E/cutils-trace: Error opening trace file: No such file or directory (2)
06-14 00:34:18.356 8054-8054/? W/art: Unknown instruction set features for ARM CPU variant (generic) using conservative defaults
06-14 00:34:18.371 8054-8054/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
06-14 00:34:18.447 8054-8054/? E/memtrack: Couldn't load memtrack module (No such file or directory)
06-14 00:34:18.447 8054-8054/? E/android.os.Debug: failed to load memtrack module: -2
06-14 00:34:18.450 8054-8054/? I/Radio-JNI: register_android_hardware_Radio DONE
06-14 00:34:18.485 8054-8054/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
06-14 00:34:18.523 410-649/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.androidthings.loopback/.LoopbackActivity} from uid 2000 on display 0
06-14 00:34:18.548 8054-8054/? D/AndroidRuntime: Shutting down VM
06-14 00:34:18.565 410-422/system_process I/ActivityManager: Start proc 8064:com.example.androidthings.loopback/u0a29 for activity com.example.androidthings.loopback/.LoopbackActivity
06-14 00:34:18.576 8064-8064/? I/art: Late-enabling -Xcheck:jni
06-14 00:34:18.578 8064-8064/? W/art: Unknown instruction set features for ARM CPU variant (generic) using conservative defaults
06-14 00:34:18.761 8064-8064/com.example.androidthings.loopback W/System: ClassLoader referenced unknown path: /data/app/com.example.androidthings.loopback-1/lib/arm
06-14 00:34:18.893 8064-8064/com.example.androidthings.loopback D/LoopbackActivity: Loopback Created
06-14 00:34:18.887 163-163/? I/peripheralman: type=1400 audit(0.0:36): avc: denied { dac_override } for capability=1 scontext=u:r:peripheralman:s0 tcontext=u:r:peripheralman:s0 tclass=capability permissive=1
06-14 00:34:18.887 163-163/? I/peripheralman: type=1400 audit(0.0:37): avc: denied { read write } for name="ttyAMA0" dev="tmpfs" ino=9297 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
06-14 00:34:18.887 163-163/? I/peripheralman: type=1400 audit(0.0:38): avc: denied { open } for path="/dev/ttyAMA0" dev="tmpfs" ino=9297 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
06-14 00:34:18.887 163-163/? I/peripheralman: type=1400 audit(0.0:39): avc: denied { ioctl } for path="/dev/ttyAMA0" dev="tmpfs" ino=9297 ioctlcmd=5401 scontext=u:r:peripheralman:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
06-14 00:34:19.097 410-649/system_process W/art: Long monitor contention with owner android.display (431) at void com.android.server.wm.WindowAnimator$1.doFrame(long)(WindowAnimator.java:138) waiters=0 in float com.android.server.wm.WindowManagerService.getCurrentAnimatorScale() for 149ms
06-14 00:34:19.879 410-440/system_process W/art: Long monitor contention with owner android.display (431) at void com.android.server.wm.WindowAnimator$1.doFrame(long)(WindowAnimator.java:138) waiters=0 in int com.android.server.wm.WindowManagerService.addWindow(com.android.server.wm.Session, android.view.IWindow, int, android.view.WindowManager$LayoutParams, int, int, android.graphics.Rect, android.graphics.Rect, android.graphics.Rect, android.view.InputChannel) for 342ms
06-14 00:34:20.287 410-440/system_process W/art: Long monitor contention with owner android.display (431) at void com.android.server.wm.WindowAnimator$1.doFrame(long)(WindowAnimator.java:138) waiters=0 in int com.android.server.wm.WindowManagerService.addWindow(com.android.server.wm.Session, android.view.IWindow, int, android.view.WindowManager$LayoutParams, int, int, android.graphics.Rect, android.graphics.Rect, android.graphics.Rect, android.view.InputChannel) for 407ms
06-14 00:34:20.329 8064-8064/com.example.androidthings.loopback I/Choreographer: Skipped 46 frames!  The application may be doing too much work on its main thread.
06-14 00:34:20.474 410-431/system_process I/ActivityManager: Displayed com.example.androidthings.loopback/.LoopbackActivity: +1s927ms
06-14 00:34:20.536 410-661/system_process I/WindowManager: Destroying surface Surface(name=com.android.iotlauncher/com.android.iotlauncher.IoTLauncher) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.AppWindowToken.notifyAppStopped:389 com.android.server.wm.WindowManagerService.notifyAppStopped:4456 com.android.server.am.ActivityStack.activityStoppedLocked:1252 com.android.server.am.ActivityManagerService.activityStopped:6902 
06-14 00:34:20.981 410-431/system_process I/WindowManager: Destroying surface Surface(name=Starting com.example.androidthings.loopback) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.WindowStateAnimator.finishExit:565 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:491 com.android.server.wm.WindowAnimator.updateWindowsLocked:303 com.android.server.wm.WindowAnimator.animateLocked:704 
06-14 00:34:21.547 410-480/system_process E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
06-14 00:35:09.632 163-270/? E/peripheralman: [0614/003509:ERROR:uart_driver_sysfs.cc(388)] Can't write to the communication pipe. Something is not right with client

I cannot receive data on my RPi3B

Hello,
I tired this project on my RPi3B. the read function seemed cannot read the data.
the following while function cannot be entered.
while ((read = mLoopbackDevice.read(buffer, buffer.length)) > 0) { mLoopbackDevice.write(buffer, read); }
I have tested the mLoopbackDevice.write() function singly, and that worked well.
But when I tested the mLoopbackDevice.read() function singly (without write() function). My serial port debug tools on computer still can received the data what I sent, it seemed like the data was returned by other level but not the app.

Installation failed

10/22 12:17:24: Launching 'app' on rockchip rk3399-mid.
Installation did not succeed.
The application could not be installed: INSTALL_FAILED_MISSING_SHARED_LIBRARY
Installation failed due to: 'null'

App get crashed when i use PeripheralManager.

Hi ,
The app is getting chrashed when i use PeripheralManager.getInstance();
below is error given.

Caused by: java.lang.RuntimeException: Stub!
        at com.google.android.things.pio.PeripheralManager.getInstance(PeripheralManager.java:21)
        at com.asiczen.usb1.UartActivity.onCreate(UartActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:6858)

UART divides message when writing it

Hi,
I'm using this sample code with a PICO PI IMX6UL.
The problem is when I send a String like "abcdefghijklmno". I receive it on the other side divided, it means not as a single string, like "abc" then "de" then "fghijk" then "lmno".
And messages are received into random way, not the same number of parts, and not the same length of each part.
My UART config is as following:
BAUD_RATE = 115200;
DATA_BITS = 8;
STOP_BITS = 1;
CHUNK_SIZE = 256;
and PARITY_NONE

On the other side, I use UsbSerial library on an android phone (https://github.com/felHR85/UsbSerial)

No problem when reading data.
I need to read data in one piece. Any help please!
Thanks.

App got crashed when launch into physical device

Dear all,

We are trying to communicate android device to dsPIC series MCU using PL2303 USB to Uart converter.
We check all hardware on windows and linux PC and it is working ok.

We just download this sample source code for JAVA language and open it in linux pc where android studio 4.0 with gradle version 6.1 are available.
We connect physical device (MOTO G5 with Android 8, Redmi Note 9 pro with Android 10) on PC USB and run app from android studio both both device individually.
But both time we got same error as mentioned below.

Installation did not succeed.
The application could not be installed: INSTALL_FAILED_MISSING_SHARED_LIBRARY

List of apks:
[0] '/home/user/AndroidStudioProjects/UARTLoopback/app/build/outputs/apk/debug/app-debug.apk'
Installation failed due to: 'null'

To resolve this issue we tried some google search where most common solution we found is as below link.
[https://github.com/androidthings/new-project-template/issues/1]

After adding android:required="false" this line in AndroidManifest.xml file we are able to run app on device.
But whenever we open app on device app got forced stop and showing below error logcat.
consollog.txt

We would link to overcome with this problem ASAP.

Waiting for your quick reply.

Thanks and Best Regards,
Jehan Patel

UART Cant open

E/Exception: UART cant opencom.google.android.things.pio.PioException: android.os.ServiceSpecificException: UART0 is already in use (code 16)

Random code problem

I want to input the content I input in aruino at Log, but there is a garbled problem and how to solve it.
tim 20181105160538
tim 20181105160635

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.