Android串口通讯RS485发送和接收数据 您所在的位置:网站首页 485文件 Android串口通讯RS485发送和接收数据

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

2024-06-28 09:24| 来源: 网络整理| 查看: 265

最近有个需求是这样子的,客户购买了我们这边的室内可视分机, 客户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 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 (IOException e) {


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有