基于Android的串口聊天室 (基于tiny4412) 一

一、平台介绍

硬件平台: tiny4412ADK + S700 4GB Flash

Android版本:Android-5.0.2

Linux版本: Linux-3.0.86

Bootloader:Superboot

 

作者:彭东林

邮箱:pengdonglin137@163.com

 

image

二、代码目录

下面的例子是基于Andrioid源码中自带的一个串口应用SerialChat,Android已经把与之相关的Freamwork、JNI代码实现了,其中直接在JNI中调用了Linux的系统调用,没有遵循Android的HAL架构,根本就没有提供HAL。

下面是相关的代码路径:

APP:

frameworks/base/tests/SerialChat/

SerialManager:

frameworks/base/core/java/android/hardware/SerialManager.java

SerialService:

frameworks/base/services/core/java/com/android/server/SerialService.java

SerialPort:

frameworks/base/core/java/android/hardware/SerialPort.java

JNI:

frameworks/base/services/core/jni/com_android_server_SerialService.cpp

frameworks/base/core/jni/android_hardware_SerialPort.cpp

AIDL:

frameworks/base/core/java/android/hardware/ISerialManager.aidl

三、编译运行

  • 编译

root@ubuntu:~/tiny4412_android5/android-5.0.2# . setenv 
including device/lge/mako/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/friendly-arm/tiny4412/vendorsetup.sh
including device/moto/shamu/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
including device/asus/deb/vendorsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/asus/tilapia/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including sdk/bash_completion/adb.bash

root@ubuntu:~/tiny4412_android5/android-5.0.2# mmm frameworks/base/tests/SerialChat/
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=5.0.2
TARGET_PRODUCT=full_tiny4412
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a9
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.13.0-32-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=LRX22G
OUT_DIR=out
============================================
make: Entering directory `/root/tiny4412_android5/android-5.0.2'
target Java: SerialChat (out/target/common/obj/APPS/SerialChat_intermediates/classes)
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes-jarjar.jar
Copying: out/target/common/obj/APPS/SerialChat_intermediates/emma_out/lib/classes-jarjar.jar
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.jar
Proguard: out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar
ProGuard, version 4.10
Reading program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-junit_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar]
Preparing output jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar]
  Copying resources from program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar]
target Dex: SerialChat
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.dex
target Package: SerialChat (out/target/product/tiny4412/obj/APPS/SerialChat_intermediates/package.apk)
Install: out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk
make: Leaving directory `/root/tiny4412_android5/android-5.0.2'

#### make completed successfully (13 seconds) ####
  • 安装和修改

adb install –r out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk

点击运行,会出错退出。

现在作如下修改:

1、frameworks/base/tests/SerialChat/Android.mk

#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed 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.
#

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := SerialChat

LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

2、frameworks/base/tests/SerialChat/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
     Licensed 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.serialchat"
    android:sharedUserId="android.uid.system">

    <uses-permission android:name="android.permission.SERIAL_PORT"/>

    <application android:label="Serial Chat">
        <activity android:name="SerialChat" android:label="Serial Chat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

3、frameworks/base/core/res/res/values/config.xml

为什么要修改这个文件呢?在这个文件中配置那些设备节点作为串口设备,在tiny4412平台一共有四个串口,COM0和COM3在底板上用DB9引了出来,对应的设备节点是/dev/ttySAC0、/dev/ttySAC1、/dev/ttySAC2、/dev/ttySAC3,所以修改如下:


 <!-- List of paths to serial ports that are available to the serial manager.
         for example, /dev/ttyUSB0
  -->
<string-array translatable="false" name="config_serialPorts">
    <item>"/dev/ttySAC0"</item>
    <item>"/dev/ttySAC1"</item>
    <item>"/dev/ttySAC2"</item>
    <item>"/dev/ttySAC3"</item>
</string-array>

4、frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java

在frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java中:

    @Override
    public void onResume() {
        super.onResume();

        String[] ports = mSerialManager.getSerialPorts();
        if (ports != null && ports.length > 0) {
            try {
                mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);
                if (mSerialPort != null) {
                    new Thread(this).start();
                }
            } catch (IOException e) {
            }
        }
    }

从上面的代码可以发现,调用getSerialPorts返回的就是在config.xml中配置的串口设备,这里ports[0]就是”/dev/ttySAC0”,在这里我们使用COM3,对应的是ports[3],也就是/dev/ttySAC3,所以这里作如下修改:

mSerialPort = mSerialManager.openSerialPort(ports[3], 115200);

重新编译:

mmm frameworks/base/core/res/   # 编译出来的是framework-res.apk,替换/system/framework下的framework-res.apk
mmm frameworks/base/tests/SerialChat/ # 先卸载,在安装 (卸载 adb uninstall com.android.serialchat)

四、测试

下面是测试结果:

  • 这个是串口调试助手,运行在PC机上。

image

  • 下面是tiny4412上运行SerialChat,输入完毕点击image即可发送。

image

 

下载地址: http://pan.baidu.com/s/1nWHP0

完。

posted @ 2015-06-20 17:35  摩斯电码  阅读(3574)  评论(0编辑  收藏  举报