uniapp原生插件之安卓串口操作原生插件

news2024/9/28 4:31:56

插件介绍

安卓串口操作原生插件,支持设置串口,波特率,停止位,数据位,校验位,流控以及延迟,支持粘包处理解决分包问题,支持多串口操作,无需root

插件地址

安卓串口操作原生插件 - DCloud 插件市场

超级福利

uniapp 插件购买超级福利

详细使用文档

uniapp 安卓串口操作原生插件使用文档

用法

在需要使用插件的页面加载以下代码

const module = uni.requireNativePlugin("leven-serial-SerialModule");

多串口操作

如果您的项目涉及到多串口的话,可以多引入一些module,目前插件提供了以下几个module

const module = uni.requireNativePlugin("leven-serial-SerialModule");
const module2 = uni.requireNativePlugin("leven-serial-SerialTwoModule");
const module3 = uni.requireNativePlugin("leven-serial-SerialThreeModule");
const module4 = uni.requireNativePlugin("leven-serial-SerialFourModule");
const module5 = uni.requireNativePlugin("leven-serial-SerialFiveModule");

如果您不想这样使用,多串口可以直接使用const module = uni.requireNativePlugin("leven-serial-SerialModule");然后在使用时打开不用时关闭即可(即按需使用)
如果多串口一直保持打开状态的话就需要引入上面的module

页面内容

<template>
  <view>
    <view>
      <uni-card title="安卓串口操作原生插件">
        <view>
          <button type="primary" @click="logStr = ''">清空日志</button>
          <button type="primary" @click="getAllDevices">获取所有的设备列表</button>
          <button type="primary" @click="getAllDevicesPath">获取所有的设备路径</button>
          <button type="primary" @click="setPort">设置串口</button>
          <button type="primary" @click="setBaudRate">设置波特率</button>
          <button type="primary" @click="setStopBits">设置停止位</button>
          <button type="primary" @click="setDataBits">设置数据位</button>
          <button type="primary" @click="setParity">设置校验位</button>
          <button type="primary" @click="setFlowCon">设置流控</button>
          <button type="primary" @click="setDelay">设置延迟</button>
          <button type="primary" @click="open">开启串口</button>
          <button type="primary" @click="close">关闭串口</button>
          <button type="primary" @click="onListenerBytes">监听字节串口消息</button>
          <button type="primary" @click="sendBytes">发送字节数据</button>
          <button type="primary" @click="onListenerHex">监听十六进制串口消息</button>
          <button type="primary" @click="sendHex">发送16进制数据</button>
          <button type="primary" @click="onListenerText">监听ASCII串口消息</button>
          <button type="primary" @click="sendText">发送ASCII数据</button>
          <button type="primary" @click="isOpen">串口是否开启</button>
          <button type="primary" @click="getSerialProperty">当前串口属性</button>
        </view>
      </uni-card>
      <uni-card title="粘包处理">
        <view>
          <button type="primary" @click="setStaticLenStickPackage">固定长度粘包处理</button>
          <button type="primary" @click="setVariableLenStickPackage">可变长度粘包处理</button>
          <button type="primary" @click="setSpecifiedStickPackage">首尾特殊字符粘包处理</button>
        </view>
      </uni-card>
    </view>
    <view>
      <uni-card class="uni-card-box" title="日志">
        <view><text style="font-size: 14px; flex-wrap: wrap;">{{logStr}}</text></view>
      </uni-card>
    </view>
  </view>
</template>

<script>
  const module = uni.requireNativePlugin("leven-serial-SerialModule");
  export default {
    data() {
      return {
        logStr: ""
      }
    },
    mounted() {
      // 动态开启应用权限
      // this.openAndroidPermission();
    },
    methods: {
      // 获取所有设备列表
      getAllDevices() {
        let that = this;
        module.getAllDevices(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 获取所有设备路径
      getAllDevicesPath() {
        let that = this;
        module.getAllDevicesPath(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置串口
      setPort() {
        let that = this;
        module.setPort("/dev/ttyS1", res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置波特率
      setBaudRate() {
        let that = this;
        module.setBaudRate(9600, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置停止位
      setStopBits() {
        let that = this;
        module.setStopBits(1, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置数据位
      setDataBits() {
        let that = this;
        module.setDataBits(1, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置校验位
      setParity() {
        let that = this;
        module.setParity(1, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置流控
      setFlowCon() {
        let that = this;
        module.setFlowCon(1, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 设置延迟
      setDelay() {
        let that = this;
        module.setDelay(500, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 开启串口
      open() {
        let that = this;
        module.open(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 关闭串口
      close() {
        let that = this;
        module.close(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 监听字节串口消息
      onListenerBytes() {
        let that = this;
        module.onListenerBytes(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 发送字节数据
      sendBytes() {
        let that = this;
        module.sendBytes("ab", res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 监听十六进制串口消息
      onListenerHex() {
        let that = this;
        module.onListenerHex(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 发送16进制数据
      sendHex() {
        let that = this;
        module.sendHex("F001020F", res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 监听ASCII消息串口消息
      onListenerText() {
        let that = this;
        module.onListenerText(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 发送ASCII数据
      sendText() {
        let that = this;
        module.sendText("ABCDEFG", res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 串口是否开启
      isOpen() {
        let that = this;
        module.isOpen(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 当前串口属性
      getSerialProperty() {
        let that = this;
        module.getSerialProperty(res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 固定长度粘包处理
      setStaticLenStickPackage() {
        let that = this;
        module.setStaticLenStickPackage({
          length: 16
        }, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 固定长度粘包处理
      setVariableLenStickPackage() {
        let that = this;
        module.setVariableLenStickPackage({
          byteOrder: 1,
          lenSize: 2,
          lenIndex: 0,
          offset: 0
        }, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 首尾特殊字符粘包处理
      setSpecifiedStickPackage() {
        let that = this;
        module.setSpecifiedStickPackage({
          head: "^",
          tail: "$"
        }, res => {
          that.writeLog(JSON.stringify(res));
        })
      },
      // 写日志
      writeLog(str) {
        console.log(str)
        let logStr = uni.$lv.date.format(null, "yyyy-mm-dd hh:MM:ss") + " " + str + "\n";
        this.logStr = logStr + this.logStr;
      }
    }
  }
</script>

<style>

</style>

 插件方法

  • 1.获取所有设备列表
  • 2.获取所有设备路径
  • 3.监听串口消息(已删除)
  • 4.设置串口
  • 5.设置波特率
  • 6.设置停止位
  • 7.设置数据位
  • 8.设置校验位
  • 9.设置流控
  • 10.设置延迟
  • 11.开启串口
  • 12.关闭串口
  • 13.监听字节串口消息
  • 14.发送字节消息
  • 15.监听十六进制串口消息
  • 16.发送16进制消息
  • 17.监听ASCII消息串口消息
  • 18.发送ASCII消息
  • 19.串口是否开启
  • 20.当前串口属性
  • 21.粘包处理
    • 固定长度粘包处理
    • 可变长度粘包处理
    • 首尾特殊字符粘包处理

具体方法的使用请参考详细使用文档 

联系作者

购买插件前请先试用,试用通过再购买。在试用中如果遇到任何问题,可与作者联系,QQ:334106817,将全力协助你使用本插件

预览图片
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1181577.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2023年【危险化学品经营单位安全管理人员】考试资料及危险化学品经营单位安全管理人员考试试卷

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2023年危险化学品经营单位安全管理人员考试资料为正在备考危险化学品经营单位安全管理人员操作证的学员准备的理论考试专题&#xff0c;每个月更新的危险化学品经营单位安全管理人员考试试卷祝您顺利通过危险化学品经…

【深度神经网络(DNN)】实现车牌识别

文章目录 前言一、数据集介绍二、步骤1.导包2.参数配置3.数据处理4.模型定义5.模型训练6.模型预测 总结 前言 课内实践作业 车牌识别 一、数据集介绍 1.车牌识别数据集&#xff1a;VehicleLicense车牌识别数据集包含16151张单字符数据&#xff0c;所有的单字符均为严格切割且…

力扣:67.二进制求和

class Solution { public:string addBinary(string a, string b) {string ans;reverse(a.begin(), a.end()); // 将字符串a反转reverse(b.begin(), b.end()); // 将字符串b反转int n max(a.size(), b.size()), carry 0; // 获取a和b的最大长度&#xff0c;并初始化进位为0for…

Python合并拼接图片

目录 图片二维合并拼接&#xff08;类似九宫格&#xff09;图片纵向合并拼接举例18张图片合并为2张九宫格图片18张图片合并为2张纵向图片 使用前需要安装PIL库&#xff0c;以下代码使用的Pillow(10.1.0) pip install pillow图片二维合并拼接&#xff08;类似九宫格&#xff09…

趣玩行为商城:用智能消费行为开启财富新生活!

随着当前的消费主力转移至90后和Z时代&#xff0c;购物和消费习惯已经发生了翻天覆地的变化。以往那种大超市&#xff0c;线下大卖场、超级Mall综合体逐渐式微&#xff0c;以“健康生活”、“智能消费”“社区直达”为主的消费理念逐渐兴盛&#xff0c;消费者开始更多地关注此类…

sqli-labs-1

文章目录 Less-01Less-02Less-03Less-04 Less-01 1.输入不同的id值&#xff0c;可以获取不同的用户信息&#xff1a; 2.在sql后拼接’or11–&#xff0c;并没有获取到所有用户的信息&#xff0c;猜测可能用了limit语句 3.构造错误的sql语句&#xff0c;果然有limit报错: …

简历考察点1_《基于 VUE2.0 前后端分离的个人博客系统》

项目名称&#xff1a;《基于 Vue2.0①前后端分离的个人博客系统》 项目描述&#xff1a;提供最新技术资讯、开发知识和分享的博客平台&#xff0c;功能模块包括&#xff1a;支持不同用户登录注册、编辑并发布博客、上传图片、评论留言、根据访问量查询最热文章和标签、根据日期…

VM虚拟机逆向---[羊城杯 2021]Babyvm 复现【详解】

文章目录 前言题目分析汇编脚本分析汇编exp 后言 前言 无 题目分析 &unk_804B0C0里面是opcode&#xff0c;sub_1C8里面有个mprotect&#xff0c;用了一个SMC加密。 我使用的是动态调试&#xff0c;因为是ELF文件&#xff0c;链接一下linux&#xff0c;进行动调&#xff…

Android11修改连接WiFi后AP端显示的设备名

修改build.prop文件 1.修改 /system/build.prop 最后添加&#xff0c;xxx 为自己设置的设备名&#xff1a; net.hostnamexxx 2. 重启、重连wifi&#xff0c;从热点或路由器后台查看设备名即为修改后的名称 代码里动态配置 暴力手段&#xff1a;grep -rn “net.hostname” *…

matlab中的iddata函数的初步理解和使用程序举例

matlab中的iddata函数的初步理解和程序举例 一、iddata函数功能 iddata函数常用于系统识别分析领域数据分析方面。该函数在时域或频域中&#xff0c;将用于系统识别的输入输出数据及其特性数据的生成对象数据类型。即&#xff0c;可以使用iddata函数封装要标识的系统的输入和…

【Transformer从零开始代码实现】(一)输入部件:embedding+positionalEncoding

Transformer总架构图 输入相关组件 输入部分&#xff1a; 源文本嵌入层位置编码器目标文本嵌入层位置编码器 &#xff08;1&#xff09;Embedding 首先&#xff0c;需要对输入的内容进行向量化。 1&#xff09;先导示例 nn.Embedding示例&#xff1a; # 10代表嵌入的数…

即插即用篇 | YOLOv8 引入反向残差注意力模块 iRMB | 《ICCV 2023 最新论文》

论文地址:https://arxiv.org/abs/2301.01146 代码地址:https://github.com/zhangzjn/EMO 本论文着重于开发现代、高效、轻量级的模型,用于进行密集预测,同时在参数、FLOPs和性能之间进行权衡。倒置残差块(IRB)作为轻量级CNN的基础设施,但在基于注意力的研究中尚未找到对…

目标检测中的评价指标

目标检测中的评价指标 将检测目标分为正样本和负样本。 真阳性&#xff08;true positives , TP&#xff09; : 正样本被正确识别为正样本。 假阳性&#xff08;false positives, FP&#xff09;: 负样本被错误识别为正样本。 假阴性&#xff08;false negatives, FN&#…

技术分享 | app自动化测试(Android)-- 参数化用例

参数化是自动化测试的一种常用技巧&#xff0c;可以将测试代码中的某些输入使用参数来代替。以百度搜索功能为例&#xff0c;每次测试搜索场景&#xff0c;都需要测试不同的搜索内容&#xff0c;在这个过程里面&#xff0c;除了数据在变化&#xff0c;测试步骤都是重复的&#…

Java附件和base64相互转换

1 文件转base64 声明&#xff1a;我用的是Hutool的Base64下的api package cn.hutool.core.codec; 首先找一张图片 很简单&#xff0c;直接使用Base64的encode方法就可以拿到文件的base64码&#xff1a; File file new File("D:\\Tools\\Images\\北极熊.jpg");String…

软件测试面试题汇总,(测试技术+人力资源+进阶规划)含2023面试题和答案总结

什么是兼容性测试&#xff1f;单元测试的策略有哪些&#xff1f;当开发人员说不是BUG时&#xff0c;你如何应付&#xff1f;等&#xff0c;尾部有最新BAT的Python高级自动化工程师面试题目和答案福利&#xff0c;想要的就快来领走吧&#xff01;&#xff08;领取方式见文末&…

【机器学习3】有监督学习经典分类算法

1 支持向量机 在现实世界的机器学习领域&#xff0c; SVM涵盖了各个方面的知识&#xff0c; 也是面试题目中常见的基础模型。 SVM的分类结果仅依赖于支持向量&#xff0c;对于任意线性可分的两组点&#xff0c;它 们在SVM分类的超平面上的投影都是线性不可分的。 2逻辑回归 …

安装 MinGW

实际上是将 GCC&#xff08;C语言编译器&#xff09; 移植到了 Windows 平台下。 1、网上下载 下载安装器 mingw-get-setup.exe&#xff0c;路径https://osdn.net/projects/mingw/ 2、打开点击install 3、选择路径continue 4、文件加载完成之后选择continue 5、勾选这两个 6…

数据结构与算法之美学习笔记:17 | 跳表:为什么Redis一定要用跳表来实现有序集合?

目录 前言如何理解“跳表”&#xff1f;用跳表查询到底有多快&#xff1f;跳表是不是很浪费内存&#xff1f;高效的动态插入和删除跳表索引动态更新解答开篇内容小结 前言 本节课程思维导图&#xff1a; 二分查找底层依赖的是数组随机访问的特性&#xff0c;所以只能用数组来实…

润色论文Prompt

你好&#xff0c;我现在开始写论文了&#xff0c;我希望你可以扮演帮我润色论文的角色我写的论文是关于xxxxx领域的xxxxx&#xff0c;我希望你能帮我检查段落中语句的逻辑、语法和拼写等问题我希望你能帮我检查以下段落中语句的逻辑、语法和拼写等问题同时提供润色版本以符合学…