WUP-CH34X ch34x系列芯片USB转串口通信uniapp插件使用说明

插件地址:WUP-CH34X 系列芯片USB转串口通信安卓库

简介

本文档是针对 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 接口
    在这里插入图片描述

前言

插件目前不支持uniapp x,这个暂时没搞明白是uniapp x兼容问题还是我写法有问题,有android.hardware.usb.UsbDevice开发经验的可以评论区交流一下。

插件支持uniapp的vue2和vue3版本

本插件只支持单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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值