Files
ch934x_serial/docs/CH934X_Android_开发说明.md
Developer 4813e79c80 docs(ch934x_serial): 更新CH934X Android开发说明文档
- 将文档结构从目录导航改为概述和接口说明格式
- 新增CH934X/CH348系列芯片的Android开发基础说明
- 详细描述getInstance、init、enumDevice等核心接口的功能和参数
- 完善串口参数设置、数据读写、GPIO控制等功能接口文档
- 添加Modem状态控制、中断处理、错误计数等高级功能说明
- 补充设备连接管理、芯片类型识别等相关接口说明
2026-07-07 15:29:57 +08:00

383 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CH934X 系列芯片串口 Android 程序开发说明
## 概述
本文档是针对 CH934X/CH348 系列的 USB 转串口安卓库的开发说明文档。
CH934X 串口提供的 Android 接口需要基于 Android 4.4 及以上版本系统,使用 CH934X串口 Android 驱动条件:
- 基于 Android 4.4 及以上版本系统
- Android 设备具有 USB Host 或 OTG 接口
## 接口说明
### getInstance
public static CH934XManager getInstance()用于获取全局唯一实例
| | |
|---|---|
| **返回** | 返回全局唯一实例 |
### init
public void init(android.app.Application application)初始化上下文,注册动态广播监听设备状态变化
| | |
|---|---|
| **参数** | application - 全局上下文 |
### enumDevice
public ArrayList<UsbDevice> enumDevice() throws UartLibException枚举当前的 CH934X 设备
| | |
|---|---|
| **返回** | 设备列表 |
| **抛出** | UartLibException |
### getChipType
public ChipType getChipType(@NonNull UsbDevice usbDevice)根据 UsbDevice 获取芯片类型
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | 如果为 null 则表示参数USB 设备并不是 CH934X |
### openDevice
public boolean openDevice(@NonNull UsbDevice usbDevice)
throws .UartLibException,NoPermissionException,ChipException
打开设备
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | true 成功;false 失败 |
| | |
|---|---|
| **抛出** | UartLibException<br>NoPermissionException ChipException |
### requestPermission
public void requestPermission(@NonNullContext context,
@NonNullUsbDevice usbDevice) throws UartLibException
请求 USB 设备权限
| | |
|---|---|
| **参数** | context 上下文<br>usbDevice - USB 设备 |
| **抛出** | UartLibException |
### setUsbStateListener
public void setUsbStateListener(@NonNullIUsbStateChange usbStateListener)监听设备的状态变化
| | |
|---|---|
| **参数** | usbStateListener 设备状态监听调 |
### getSerialCount
public int getSerialCount(@NonNull UsbDevice usbDevice)获取设备的串口数目
| | |
|---|---|
| **参数** | usbDevice USB 设备 |
| **返回** | 返串口数目;如果为负,说明获取失败 |
### setSerialParameter
public boolean setSerialParameter(@NonNull
UsbDevice usbDevice, int serialNumber, int baud,
int dataBit, int stopBit, int parityBit, boolean flow)
throws .UartLibException,ChipException
设置串口参数
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber - 串口号 baud - 波特率 dataBit - 数据位 stopBit - 停止位 parityBit - 校验位<br>flow - 流控 |
| | |
|---|---|
| **返回** | true 设置成功;false 设置失败 |
| **抛出** | UartLibException<br>ChipException |
### writeData
public int writeData(@NonNull
UsbDevice usbDevice, int serialNumber, byte[] data,
int length, int timeout)
throws UartLibException,ChipException发送数据
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber - 串口号 data - 待发送的数据<br>length - 待发送的数据的长度<br>timeout - 超时时间 |
| **返回** | 发送成功的数据的长度 |
| **抛出** | UartLibException<br>ChipException |
### readData
public byte[] readData(@NonNull UsbDevice usbDevice,int serialNumber) throws ChipException
读取数据
| | |
|---|---|
| **参数** | usbDevice - USB 设备<br>serialNumber - 串口号 |
| **返回** | 读取到的数据 |
| **抛出** | ChipException |
### registerDataCallback
public void registerDataCallback(@NonNull UsbDevice usbDevice,
IDataCallback dataCallback) throws ChipException
注册串口数据 调, 解除注册使用 registerDataCallback(device,null) 方法, 或者 removeDataCallback(device)方法
| | |
|---|---|
| **参数** | usbDevice - USB 设备<br>dataCallback 调 |
| **返回** | 读取到的数据 |
| **抛出** | ChipException |
### removeDataCallback
public void removeDataCallback(@NonNull UsbDevice usbDevice)解除注册串口数据回调
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
### setBreak
public boolean setBreak(@NonNull UsbDevice usbDevice,int serialNumber,
boolean valid) throws Exception
设置 Break 信号
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber - 串口号<br>valid – 是否有效(低电平标识有效) |
| **返回** | true 设置成功;false 设置失败 |
| **抛出** | Exception |
### setDTR
public boolean setDTR(UsbDevice usbDevice,int serialNumber,boolean dtr)
throws UartLibException, ChipException
设置 DTR 信号
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber - 串口号<br>dtr true 有效;false 无效 |
| **返回** | true 设置成功;false 设置失败 |
| **抛出** | ChipException<br>UartLibException |
### setRTS
public boolean setRTS(UsbDevice usbDevice,int serialNumber,boolean rts) throws
UartLibException, ChipException
设置 RTS 信号
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber - 串口号<br>rts true 有效;false 无效 |
| **返回** | true 设置成功;false 设置失败 |
| **抛出** | ChipException<br>UartLibException |
### registerModemStatusCallback
public void registerModemStatusCallback(@NonNull UsbDevice usbDevice,
IModemStatus modemStatus) throws Exception注册 Modem 输入信号状态的调
| | |
|---|---|
| **参数** | usbDevice - USB 设备<br>modemStatus 状态回调 |
| | |
|---|---|
| **抛出** | Exception |
### querySerialErrorCount
public int querySerialErrorCount(@NonNull UsbDevice usbDevice, int serialNumber,@NonNull SerialErrorType errorType)
throws Exception
查询串口错误状态
| | |
|---|---|
| **参数** | usbDevice - USB 设备 serialNumber 串口号<br>errorType 错误类型 |
| **返回** | 出现错误的次数 |
| **抛出** | Exception |
### getCurrentMode
public Mode getCurrentMode(UsbDevice usbDevice,int serialNumber)
获取当前串口的模式。初始默认状态为普通模式(仅针对 CH934X 型号设备,CH348 无效)
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **参数** | serialNumber-串口序号 |
| **返回** | Mode.NORMAL 普通模式;Mode.HARDFLOW 硬件流控模式;<br>Mode.GPIO GPIO 模式; |
| **抛出** | UartLibException |
### getSpecificType
public SpecificChipType getSpecificType(UsbDevice usbDevice)
throws UartLibException
获取芯片具体型号
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | 芯片具体型号 |
| **抛出** | UartLibException |
### getGPIOCount
public int getGPIOCount(UsbDevice usbDevice) throws UartLibException获取 GPIO 数量
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | GPIO 具体数量(从 0 开始使用) |
| **抛出** | UartLibException |
### getGPIOGroup
public int getGPIOGroup(UsbDevice usbDevice) throws UartLibException
获取 GPIO 组
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | GPIO 组数量(从 0 开始使用) |
| **抛出** | UartLibException |
### enableGPIO
public boolean enableGPIO(@NonNull UsbDevice usbDevice, ChipType chipType, int gpioGroup, int enable) throws UartLibException
使能串口的 GPIO 功能,如果当前模式为硬件流控模式,需要先退出。
| | |
|---|---|
| **参数** | usbDevice - USB 设备 chipType 芯片类型 gpioGroup GPIO 组号 enable 使能状态<br>CH9344: 1 使能组内所有 GPIO;0 失能组内所有GPIO<br>CH348: bits0-7 对应 GPIO[0*N-7*N], 1 使能;0 失能 |
| **返回** | true 操作成功;false 操作失败 |
| **抛出** | UartLibException |
### setGPIODir
public boolean setGPIODir(@NonNull UsbDevice usbDevice,int gpioGroup, int gpioNumber, @NonNull GPIO_DIR dir) throws UartLibException
设置 GPIO 口的方向,成功后会同步至缓存
| | |
|---|---|
| **参数** | usbDevice - USB 设备 gpioGroup GPIO 组号 gpioNumber GPIO 序号<br>dir -方向 |
| **返回** | true 操作成功;false 操作失败 |
| **抛出** | UartLibException |
### queryGPIODirFromCache
public GPIO_DIR queryGPIODirFromCache(@NonNull UsbDevice usbDevice, int gpioGroup, int gpioNumber) throws UartLibException
从缓存中获取某个 GPIO 的方向。初始每个 GPIO 初始默认方向是 GPIO_DIR.IN
| | |
|---|---|
| **参数** | usbDevice - USB 设备<br>gpioGroup GPIO 组号 gpioNumber -GPIO 序号 |
| **返回** | GPIO_DIR.IN IN 方向;GPIO_DIR.OUT OUT 方向 |
| **抛出** | UartLibException |
### setGPIOValue
public boolean setGPIOValue(@NonNull UsbDevice usbDevice, int gpioGroup, int gpioNumber, @NonNull GPIO_VALUE value) throws UartLibException
设置 GPIO 口的值,成功后会同步至缓存
| | |
|---|---|
| **参数** | usbDevice - USB 设备 gpioGroup GPIO 组号 gpioNumber -GPIO 序号<br>value - GPIO 值 |
| **返回** | true 操作成功;false 操作失败 |
| **抛出** | UartLibException |
### getGPIOValue
public boolean getGPIOValue(@NonNull UsbDevice usbDevice) throws UartLibException
从硬件获取 GPIO 值,成功后会刷新缓存
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | true 操作成功;false 操作失败 |
| **抛出** | UartLibException |
### queryGPIOValueFromCache
public GPIO_VALUE queryGPIOValueFromCache(@NonNull UsbDevice usbDevice,int gpioGroup,int gpioNumber) throws UartLibException
从缓存中获取某个 GPIO 值。使用此方法前需要先成功使用 getGPIOValue()方法,刷新缓存。初始每个 GPIO 初始默认值是GPIO_VALUE.LOW
| | |
|---|---|
| **参数** | usbDevice - USB 设备 gpioGroup GPIO 组号<br>gpioNumber -GPIO 序号 |
| **返回** | GPIO_VALUE.HIGH 高电平;GPIO_VALUE.LOW 低电平 |
| **抛出** | UartLibException |
### isConnected
public boolean isConnected(@NonNull UsbDevice usbDevice)设备是否已经被打开
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
| **返回** | true 已经被打开;false 没有被打开 |
### getConnectedDevices
public ArrayList<UsbDevice> getConnectedDevices()获取当前已经被打开的设备列表
### disconnect
public void disconnect(@NonNull UsbDevice usbDevice)断开连接
| | |
|---|---|
| **参数** | usbDevice - USB 设备 |
### close
public void close(@NonNull Context context)释放资源。断开所有连接设备