Android串口通讯RS485发送和接收数据

本文介绍了如何在Android上实现RS485串口通信,包括SDK的创建与aar文件打包,DEMO的构建,以及解决串口断流问题的方法。详细步骤包括库文件的拷贝、Gradle配置、MainActivity.java的串口API实现和串口调试过程。
摘要由CSDN通过智能技术生成

最近有个需求是这样子的,客户购买了我们这边的室内可视分机,
客户DIY自己的软件,我们这边需要提供RS485串口通讯demo演示以及sdk集成。
大致需求是这样子,话不多说,往下看。

一、SDK部分

1、拷贝libxxx.so文件到armeabi-v7a目录下

拷贝.so库(用来进行485串口通讯的)到,libs/armeabi-v7a目录下,.so文件名称根据自己项目具体的功能模块命名名称。
拷贝so库

2、删除res目录下所有的资源文件

删除所有资源文件

3、build.gradle(Module:serialport485)

apply plugin: 'com.android.library'

android {
   
    compileSdkVersion 26

    defaultConfig {
   
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
        ndk {
   
            // 设置支持的SO库架构
            abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }

    buildTypes {
   
        release {
   
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
   
        main {
   
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
   
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    configurations.all {
   
        resolutionStrategy.force 'com.android.support:support-annotations:25.3.1'
    }
}

4、serialport485的Library库,AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.leecore.serialport" />

5、串口通讯实现

串口通讯类,静态块加载.so文件,打开/关闭串口,获取输入流/输出流,定义JNI封装接口等等。

/*
 * Copyright 2009 Cedric Priscal
 *
 * 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.
 */

package com.leecore.serialport;

import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @description: 串口通讯
 * @version: v1.0
 * @author: yeyl
 * @date: 2020/12/22 18:46
 * @history:
 */
public class SerialPort {
   

    static {
   
        System.loadLibrary("xxx");
    }

    private static final String TAG = "SerialPort";
    private FileDescriptor mFd;
    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;

    public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
   
        /* Check access permission */
        if (!device.canRead() || !device.canWrite()) {
   
            try {
   
                /* Missing read/write permission, trying to chmod the file */
                Process su;
                su = Runtime.getRuntime().exec("/system/bin/su");
                String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";
                su.getOutputStream().write(cmd.getBytes());
                if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
   
                    throw new SecurityException();
                }
            } catch (Exception e) {
   
                throw new SecurityException();
            }
        }
        mFd = serialPortOpen(device.getAbsolutePath(), baudrate, flags);
        if (mFd == null) {
   
            Log.e(TAG, "native open returns null");
            throw new IOException();
        }
        mFileInputStream = new FileInputStream(mFd);
        mFileOutputStream = new FileOutputStream(mFd);
    }

    // Getters and setters
    public InputStream getInputStream() {
   
        return mFileInputStream;
    }

    public OutputStream getOutputStream() {
   
        return mFileOutputStream;
    }

    public void close() {
   
        try {
   
            mFileInputStream.close();
        } catch (IOException e) {
   
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
   
            mFileOutputStream.close();
        } catch<
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值