更新记录

1.0.9(2020-04-04)

修复回调异常bug open方法新增流控,流控未经过测试,使用前请自测 流控默认不使用,老用户不用管

1.0.8(2020-03-23)

同步版本

1.0.7(2020-03-20)

新增持续监听、移除监听

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:5.0 - 9.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 ×

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


注意:HBuilderX3.0.0及以上版本此插件云端打包会失败,请开发者谨慎使用

声明

此插件不再维护,此插件不再维护,此插件不再维护

插件已开源,需自定义的请自行下载

码云地址:https://gitee.com/NF-Bo/serial_port_plugin

使用说明

使用此插件设备必须root

请先试用->选择你的项目->制作自定义基座

云打包:manifest.json -> App 原生插件配置 -> 选择云端插件 (需要离线打包请联系作者,联系方式在底部)

引用方式

const port = uni.requireNativePlugin('hpb-serialport');

插件接口

open(options, callback)

send(options, callback)

addListener(callback)

addListenerAndKeepAlive(callback)

removeListener(callback)

getStatus(callback)

close()

使用说明

open(options, callback)参数说明

参数 类型 参数说明
options json 参数配置
callback function 回调方法

open->options{object}

参数 类型 是否可空 参数说明
path string 串口地址(比如:/dev/ttyS0, /dev/ttyS1)
baudRate int 波特率,默认 9600
parity int 奇偶校验,0 None, 1 Odd, 2 Even 默认 0
dataBits int 数据位,5 - 8 默认 8
stopBit int 停止位,1 或 2 默认 1
flowCon int 1.0.9 新增 串口流控定义,1 硬件流控 或 2 软件流控 默认 0 不使用流控

open->callback

参数 类型 参数说明
status Boolean 是否打开
msg String 提示

send(options, callback)参数说明

参数 类型 参数说明
options json 指令数组
callback function 回调方法

send->options{object} 试用 send 方法之前必须先设置监听回调 addListener

参数 类型 是否可空 参数说明
sends array 向串口发送的指令(16 进制字符串,比如:['FF0F0C010203']), 发送的是 16 进制字符串 不发送指令则留空[]

send->callback

参数 类型 参数说明
status Boolean 是否成功
msg String 提示

addListener(callback)参数说明 ps: 改方法只监听一次回调,需要持续回调请使用下面的addListenerAndKeepAlive

参数 类型 参数说明
callback function 回调方法

addListener->callback

参数 类型 参数说明
status Boolean 是否成功
bytes byte[] 串口返回的数据
size int 串口返回的数据大小

addListenerAndKeepAlive(callback)参数说明 ps:持续监听回调

参数 类型 参数说明
callback function 回调方法

addListenerAndKeepAlive->callback 当 send 发送指令(sends:[...])为一个以上时,addListenerAndKeepAlive 会发送剩余的指令,不需要再次调用 send

参数 类型 参数说明
status Boolean 是否成功
bytes byte[] 串口返回的数据
size int 串口返回的数据大小

removeListener(callback)参数说明

参数 类型 参数说明
callback function 回调方法

removeListener->callback

参数 类型 参数说明
status Boolean 是否移除成功
msg String 提示

getStatus(callback)参数说明

参数 类型 参数说明
callback function 回调方法

getStatus->callback

参数 类型 参数说明
status Boolean 是否打开
msg String 提示

close()参数说明

关闭串口

使用方法

    const port = uni.requireNativePlugin('hpb-serialport');
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {
            port.open({
                path: "/dev/ttyS0",
                baudRate: 9600, //可空,默认9600
                parity: 0, //可空,默认0
                dataBits: 8 ,//可空,默认8
                stopBit: 1 //可空,默认1
            }, res=>{
                console.log(res);
                console.log("status->"+res.status);
                if(res.status){
                    console.log(res.msg)
                    //串口开启之后设置数据监听回调
                    this.addListenerAndKeepAlive();
                }else{
                    console.log(res.msg)
                }
            })
        },
        methods: {
            //串口开启之后设置数据监听回调
            addListenerAndKeepAlive(){
                port.addListenerAndKeepAlive(async result=>{
                    if(result.status){
                        let hex = await this.bytesToHexString(result.bytes, result.size);
                        console.log(hex);
                        uni.showToast({
                            icon: 'none',
                            title: hex
                        });
                    }else{
                        uni.showToast({
                            icon: 'none',
                            title: result.msg
                        });
                    }

                })
            },
            send(){
                port.send({
                    sends: ["FF0A01A20B010B3B48FE", "FF0A01A20B010B3B48FE"] //不发送,留[]
                }, async res=>{
                    if(res.status){
                        uni.showToast({
                            icon: 'none',
                            title: res.msg
                        });
                    }else{
                        uni.showToast({
                            icon: 'none',
                            title: res.msg
                        });
                    }
                })
            },
            status(){
                port.getStatus(res=>{
                    console.log(res);
                    console.log("status->"+res.status);
                    if(res.status){
                        console.log(res.msg)
                    }else{
                        console.log(res.msg)
                    }
                })
            },
            close(){
                port.close();
            },
            bytesToHexString(inarray, size) {
                return new Promise(res => {
                    let i, j, x;
                    let hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                        "B", "C", "D", "E", "F"
                    ];
                    let out = "";
                    for (j = 0; j < size; ++j) {
                        x = parseInt(inarray[j]) & 0xff;
                        i = (x >> 4) & 0x0f;
                        out += hex[i];
                        i = x & 0x0f;
                        out += hex[i];
                    }
                    res(out)
                })
            },
        }
    }

定制

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

请参考开源项目地址的开源协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问