更新记录

1.0.1(2024-08-10)

完善文档说明以及示例等。

1.0.0(2024-08-08)

针对 CH340/CH341/CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104/CH9143 的 USB 转串口安卓库。


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 4.23,Android:5.0,iOS:不支持 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

WUP-CH34X 安卓USB转串口库(不兼容uni-app x)

开发文档

UTS 语法 UTS API插件 UTS 组件插件 Hello UTS

插件使用说明

简介

本文档是针对 CH340/CH341/CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104/CH9143 的 USB 转串口安卓库的开发说明文档。

主要介绍如何使用芯片的 USB 转异步串口功能(以下简称 CH34XUART), 以及 Android 下如何使用 APK 操作实现串口通讯。该功能基于 Android USB Host 协议完成,用户可 调用相关的接口 API 实现与 Android 设备进行通讯。

CH34X 串口库提供的 Android 接口需要基于 Android 5.0 及以上版本系统,使用 CH34X 串口库 Android 驱动条件:

  1. 基于 Android 5.0 及以上版本系统
  2. Android 设备具有 USB Host 或 OTG 接口

前言

本插件只支持单USB设备转单串口单USB设备转多串口

方法引入

import { sayHiParam, 需引入的其他方法 } from "@/uni_modules/WUP-CH34X"; 

定义设备索引

定义一个数字类型用于存储,使用的设备索引

data() {
    return {
        deviceInx: 0,
    }
},

方法

插件测试方法

sayHiParam ("wup-ch34x", (res) => {
    console.log(res)  
    // {"code":0,"msg":"hi wup-ch34x"}
})

获取所有符合要求的USB设备列表

获取设备后可以根据设备参数选择所需的设备(参数根据自己实际情况进行调整)或者根据用户选择的设备,记录下设备索引

enumDevice(function (res) {
    for (let i = 0; i < res.devices.length; i++) {
        // 设备索引 i
    }
})

打开选择的USB设备

  • deviceInx: 设备索引,来源设备列表
    openDevice(deviceInx, function (res) {
    console.log("open", res)
    })

关闭选择的USB设备

  • deviceInx: 设备索引,来源设备列表
    closeDevice(deviceInx, (res) => {
    console.log("close", res)
    })

关闭所有USB设备

closeAllDevice((res) => {
    console.log("closeAll", res)
})

设置打开的设备串口参数

  • serialNumber:串口号,注意这个串口号不是设备参数中的,而是设备的串口序号,单设备单串口默认:0;单设备多串口:0,1,2,···
  • baud:串口波特率
  • dataBit:数据位 5,6,7,8
  • stopBit:停止位 1,2
  • parityBit:校验位 0 NONE;1 ODD;2 EVEN;3 MARK;4 SPACE
  • flow:流 true 开启,false 关闭
    setSerialParameter(serialNumber, baud, dataBit, stopBit, parityBit, flow, (res) => {
    console.log("setSerialParameter", res)
    })

发送串口数据

  • serialNumber:串口号,注意这个串口号不是设备参数中的,而是设备的串口序号,单设备单串口默认:0;单设备多串口:0,1,2,···
  • commandStr:十六进制命令字符串,如:574B4C590900820183
  • timeout:超时时间,单位:毫秒
    writeData(serialNumber, commandStr, timeout, (res) => {
    console.log("writeData", res)
    })

主动读取串口数据(不推荐)

该方法读取缓冲区数据,缓冲区数据会累积,所以如果间隔一次不读取,会把两次累积的数据一起读取出来,这个需要自行做处理。

  • serialNumber:串口号,注意这个串口号不是设备参数中的,而是设备的串口序号,单设备单串口默认:0;单设备多串口:0,1,2,···
  • timeout:超时时间,可以根据返回的数据长度做调整,避免数据读取不全,单位:毫秒
    readData(serialNumber, timeout, (re) => {
    console.log("readData", re)
    })

注册串口数据回调(推荐)

该方法注册监听成功后会有数据回调,监听到有串口数据后也会回调(含串口号serialNumber和串口数据data),在使用过程中注意区分。

  • timeout:超时时间,可以根据返回的数据长度做调整,避免数据读取不全,插件限制不小于200,单位:毫秒
    registerDataCallback(timeout, (res) => {
    console.log("registerDataCallback", res)
    })

    注册成功

    {"code":0,"msg":"register data callback successful"}

    串口数据返回

    {"data":"574B4C590B008····","code":0,"msg":"data callback successful","serialNumber":0}

解除注册串口数据回调

removeDataCallback((res) => {
    console.log("removeDataCallback", res)
})

监听设备状态变化

所有符合条件的设备,不只是打开的设备

setUsbStateListener(function (res) {
    console.log("setUsbStateListener", res)
})

数据返回说明

返回的数据中除了基本信息外,还包括设备信息device,以及状态信息status,status有以下三种状态:

  • 10:设备拔出
  • 20:设备插入
  • 30:设备权限请求结果

判断USB设备是否已经打开

  • deviceInx: 设备索引,来源设备列表
    isConnected(deviceInx, function (res) {
    console.log("isConnected", res)
    })

获取所有已经打开的USB设备

getConnectedDevices((res) => {
    console.log("getConnectedDevices", res)
})

获取打开的USB设备的芯片类型

getChipType((res) => {
    console.log("getChipType", res)
})

获取打开的USB设备的串口数量

getSerialCount((res) => {
    console.log("getSerialCount", res)
})

示例代码

<template>
    <view>
        <view style="margin-top: 50px;padding: 0 15px;">
            <button @click="enumDevice" type="primary" style="margin-bottom: 15px;">设备列表</button>
            <button @click="openDevice" type="primary" style="margin-bottom: 15px;">打开设备</button>
            <button @click="closeDevice" type="warn" style="margin-bottom: 15px;">关闭设备</button>
            <button @click="closeAllDevice" type="warn" style="margin-bottom: 15px;">关闭所有设备</button>
            <button @click="setSerialParameter" type="primary" style="margin-bottom: 15px;">设置串口</button>
            <button @click="registerDataCallback" type="primary" style="margin-bottom: 15px;">监听串口数据</button>
            <button @click="writeData" type="primary" style="margin-bottom: 15px;">写串口数据</button>
            <button @click="writeData1" type="primary" style="margin-bottom: 15px;">读写串口数据</button>
            <button @click="isCon" type="primary" style="margin-bottom: 15px;">设备状态</button>
            <button @click="getConnectedDevices" type="primary" style="margin-bottom: 15px;">已连接设备</button>
            <button @click="getChipType" type="primary" style="margin-bottom: 15px;">设备芯片类型</button>
            <button @click="getSerialCount" type="primary" style="margin-bottom: 15px;">设备串口数</button>
            <button @click="removeDataCallback" type="primary" style="margin-bottom: 15px;">移除串口监听</button>
        </view>
        <view style="padding: 15px 30px;">
            {{msg}}
        </view>
    </view>
</template>

<script>
    import { closeAllDevice, closeDevice, enumDevice, getChipType, getConnectedDevices, getSerialCount, isConnected, openDevice, readData, registerDataCallback, removeDataCallback, sayHi, sayHiParam, sayHiSync, setSerialParameter, setUsbStateListener, writeData } from "@/uni_modules/WUP-CH34X"; 
    export default {
        data() {
            return {
                msg: "",
                deviceInx: 0,
            }
        },
        onLoad() {
            let that = this
            setUsbStateListener(function (res) {
                that.msg = JSON.stringify(res)
                console.log("setUsbStateListener", res)
            })
        },
        methods: {
            removeDataCallback () {
                let that = this
                removeDataCallback((res) => {
                    that.msg = JSON.stringify(res)
                    console.log("removeDataCallback", res)
                })
            },
            getSerialCount () {
                let that = this
                getSerialCount((res) => {
                    that.msg = JSON.stringify(res)
                    console.log("getSerialCount", res)
                })
            },
            getChipType () {
                let that = this
                getChipType((res) => {
                    that.msg = JSON.stringify(res)
                    console.log("getChipType", res)
                })
            },
            getConnectedDevices () {
                let that = this
                getConnectedDevices((res) => {
                    that.msg = JSON.stringify(res)
                    console.log("getConnectedDevices", res)
                })
            },
            isCon () {
                let that = this
                isConnected(that.deviceInx, function (res) {
                    that.msg = JSON.stringify(res)
                    console.log("isConnected", res)
                })
            },
            enumDevice () {
                let that = this
                enumDevice(function (res) {
                    that.msg = JSON.stringify(res)
                    for (let i = 0; i < res.devices.length; i++) {
                        // 根据设备信息区分设备,自动选择设备

                        // CH344Q设备
                        // if (res.devices[i].mVendorId == 6790 && res.devices[i].mProductId == 21973) {
                            // that.deviceInx = i
                            // console.log("deviceInx", that.deviceInx)
                        // }

                        // CH340设备
                        if (res.devices[i].mVendorId == 6790 && res.devices[i].mProductId == 29987) {
                            that.deviceInx = i
                            console.log("deviceInx", that.deviceInx)
                        }
                    }
                    console.log("devices", res)
                })
            },
            openDevice () {
                let that = this
                openDevice(that.deviceInx, function (res) {
                    that.msg = JSON.stringify(res)
                    console.log("open", res)
                })
            },
            closeDevice () {
                let that = this
                closeDevice(that.deviceInx, (res) => {
                    that.msg = JSON.stringify(res)
                    console.log("close", res)
                })
            },
            closeAllDevice () {
                let that = this
                closeAllDevice((res) => {
                    that.msg = JSON.stringify(res)
                    console.log("closeAll", res)
                })
            },
            setSerialParameter () {
                let that = this
                setSerialParameter(0, 9600, 8, 1, 0, false, (res) => {
                    that.msg = JSON.stringify(res)
                    console.log("setSerialParameter", res)
                })
            },
            writeData () {
                let that = this
                writeData(0, "574B4C590900820183", 2000, (res) => {
                    console.log("writeData", res)
                    that.msg = JSON.stringify(res)
                })
            },
            writeData1 () {
                let that = this
                writeData(0, "574B4C590900820183", 2000, (res) => {
                    console.log("writeData1", res)
                    that.msg = JSON.stringify(res)
                    readData(0, 500, (re) => {
                        that.msg = JSON.stringify(res) + JSON.stringify(re)
                        console.log("readData", re)
                    })
                })
            },
            registerDataCallback () {
                let that = this
                registerDataCallback(200, (res) => {
                    console.log("registerDataCallback", res)
                    that.msg = JSON.stringify(res)
                })
            }

        }
    }
</script>

隐私、权限声明

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

串口读写权限

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

插件不采集任何数据

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

暂无用户评论。

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