android串口驱动服务怎么开启,Android usb转串口驱动开发
最近做了个平板与单片机的项目,由于使用的平板不支持串口,所以中间借助了usb-串口转换器(PL2303)。
这方面的资料可以说少之又少,几乎没有,我唯一能找到的就是usb-serial-for-android,一个国外的开源项目。
实现了一些主要的转换器的驱动,但是国内一般用得最多的还是PL2303。
我将usb-serial-for-android的驱动简化了下,成功的与串口调试工具通信了,能读,能写。给大家分享下。
或者直接下载
驱动源码:package com.example.android_usb;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import android.widget.Toast;
/**
* PL2303串口驱动
* @author pc
*
*/
public class ProlificSerialDriver {
private static final int USB_READ_TIMEOUT_MILLIS = 5000;
private static final int USB_WRITE_TIMEOUT_MILLIS = 3000;
private final Object mReadBufferLock = new Object();
private final Object mWriteBufferLock = new Object();
private byte[] buffer = new byte[4096];
private ByteArrayOutputStream bufferOS = new ByteArrayOutputStream();
private Context context;
private UsbEndpoint mReadEndpoint;
private UsbEndpoint mWriteEndpoint;
private UsbDeviceConnection connection;
private UsbDevice usbDevice;
public ProlificSerialDriver(Context context, UsbDevice usbDevice){
this.context = context;
this.usbDevice = usbDevice;
}
/**
* 安装
* @param baudRate
* @throws IOException
*/
public void setup(int baudRate) throws IOException{
open();
initPL2303Chip();
ctrlOut(baudRate);
}
private void open() throws IOException{
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
//申请权限
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent("com.prolific.pl2303hxdsimpletest.USB_PERMISSION"), 0);
usbManager.requestPermission(usbDevice, mPermissionIntent);
UsbInterface usbInterface = usbDevice.getInterface(0);
for (int i = 0; i
UsbEndpoint currentEndpoint = usbInterface.getEndpoint(i);
switch (currentEndpoint.getAddress()) {
case 0x83:
mReadEndpoint = currentEndpoint;
break;
case 0x02:
mWriteEndpoint = currentEndpoint;
break;
}
}
connection = usbManager.openDevice(usbDevice);
connection.claimInterface(usbInterface, true);
}
private final void ctrlOut(int baudRate) throws IOException {
//设置传输控制
byte[] lineRequestData = new byte[7];
lineRequestData[0] = (byte) (baudRate & 0xff);
lineRequestData[1] = (byte) ((baudRate >> 8) & 0xff);
lineRequestData[2] = (byte) ((baudRate >> 16) & 0xff);
lineRequestData[3] = (byte) ((baudRate >> 24) & 0xff);
lineRequestData[4] = 0;
lineRequestData[5] = 0;
lineRequestData[6] = (byte) 8;
outControlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_CLASS | 0x01, 0x20, 0, 0, lineRequestData);
}
/**
* 初始化芯片
* @throws IOException
*/
private void initPL2303Chip() throws IOException{
int mDeviceType = getDeviceType();
vendorIn(0x8484, 0, 1);
vendorOut(0x0404, 0, null);
vendorIn(0x8484, 0, 1);
vendorIn(0x8383, 0, 1);
vendorIn(0x8484, 0, 1);
vendorOut(0x0404, 1, null);
vendorIn(0x8484, 0, 1);
vendorIn(0x8383, 0, 1);
vendorOut(0, 1, null);
vendorOut(1, 0, null);
vendorOut(2, (mDeviceType == 0) ? 0x44 : 0x24, null);
}
/**
* 获得设备类型
*/
private int getDeviceType(){
int mDeviceType = 0;
try {
if (usbDevice.getDeviceClass() == 0x02) {
mDeviceType = 1;
} else{
Method getRawDescriptorsMethod = connection.getClass().getMethod("getRawDescriptors");
byte[] rawDescriptors = (byte[]) getRawDescriptorsMethod.invoke(connection);
byte maxPacketSize0 = rawDescriptors[7];
if (maxPacketSize0 == 64) {
mDeviceType = 0;
} else if ((usbDevice.getDeviceClass() == 0x00) || (usbDevice.getDeviceClass() == 0xff)) {
mDeviceType = 2;
} else {
mDeviceType = 0;
}
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mDeviceType;
}
/**
* 写
* @param buf
* @param wlength
* @return
*/
public int write(byte[] buf, int wlength) {
int offset = 0;
byte[] write_buf = new byte[4096];
synchronized (mWriteBufferLock) {
while (offset
int write_size = 4096;
if (offset + write_size > wlength) {
write_size = wlength - offset;
}
System.arraycopy(buf, offset, write_buf, 0, write_size);
int actual_length = this.connection.bulkTransfer(this.mWriteEndpoint, write_buf, write_size, USB_WRITE_TIMEOUT_MILLIS);
if (actual_length
return -1;
}
offset += actual_length;
}
}
return offset;
}
/**
* 读取
* @param timeoutMillis
* @throws IOException
*/
public byte[] read() throws IOException {
synchronized (mReadBufferLock) {
try {
bufferOS.reset();
int number = 0;
while(number != -1){
number = connection.bulkTransfer(mReadEndpoint, buffer, buffer.length, USB_READ_TIMEOUT_MILLIS);
MainActivity.showMessage("number:"+number);
bufferOS.write(buffer, 0, number);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return bufferOS.toByteArray();
}
/**
* 关闭资源
*/
public void close(){
try {
if (connection == null) {
return ;
}
connection.releaseInterface(this.usbDevice.getInterface(0));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 输出的传输
* @throws IOException
*/
private final void outControlTransfer(int requestType, int request, int value, int index, byte[] data) throws IOException {
int length = (data == null) ? 0 : data.length;
int result = connection.controlTransfer(requestType, request, value, index, data, length, USB_WRITE_TIMEOUT_MILLIS);
if (result != length) {
throw new IOException(String.format("ControlTransfer with value 0x%x failed: %d", value, result));
}
}
/**
* 输入的传输
* @throws IOException
*/
private final byte[] inControlTransfer(int requestType, int request, int value, int index, int length) throws IOException {
byte[] buffer = new byte[length];
int result = connection.controlTransfer(requestType, request, value, index, buffer, length, USB_READ_TIMEOUT_MILLIS);
if (result != length) {
throw new IOException(String.format("ControlTransfer with value 0x%x failed: %d", value, result));
}
return buffer;
}
/**
* 写设备控制
*/
private final byte[] vendorIn(int value, int index, int length)throws IOException {
return inControlTransfer(UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_VENDOR, 0x01, value, index, length);
}
/**
* 读设备控制
*/
private final void vendorOut(int value, int index, byte[] data)throws IOException {
outControlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, 0x01, value, index, data);
}
}
android串口驱动服务怎么开启,Android usb转串口驱动开发相关推荐
- linux装pl2303驱动下载,Linux下安装USB转串口驱动(PL2303)
主机:Gentoo Linux 11.2 内核版本:Linux 3.0.6 以前在Ubuntu下USB转串口驱动已经编译进内核,而编译的Gentoo内核没有编译进去,这里将内核中PL2303驱动 co ...
- Android USB转串口通信开发实例详解
好久没有写文章了,年前公司新开了一个项目,是和usb转串口通信相关的,需求是用安卓平板通过usb转接后与好几个外设进行通信,一直忙到最近,才慢慢闲下来,趁着这个周末不忙,记录下usb转串口通信开发的基 ...
- Android USB转串口通信开发基本流程
好久没有写文章了,年前公司新开了一个项目,是和usb转串口通信相关的,需求是用安卓平板通过usb转接后与好几个外设进行通信.一直忙到近期,才慢慢闲下来,趁着这个周末不忙.记录下usb转串口通信开发的基 ...
- linux 下串口转usb不能发送数据包,Linux ,USB转串口驱动,没法读到数据
Linux ,USB转串口驱动,无法读到数据 usb 1-1.1: new full-speed USB device number 5 using ehci-pci usb 1-1.1: New U ...
- usbserial驱动 带感叹号_STM32 USB转串口驱动安装不成功出现黄色感叹号解决方法!...
相信很多人在做USB转串口时出现过串口驱动安装不成功,出现黄色感叹号问题, 出现这种问题一般是驱动安装不成功造成的. 这里我就这个问题总结几个简单的方法. 方法1: 插上USB,利用驱动人生安装驱动. ...
- CH341SER CH340SER USB转串口驱动
CH341SER CH340SER USB转串口驱动适用于同型号的设备,这个USB转串口设备我们可能用不到,但是对于专业的开发者来说可能会用到,将设备与电脑连接后安装一下小编提供的USB转串口驱动程序 ...
- pl2303hxa串口线驱动_PL2303 USB转串口驱动64位(非认证线缆可用)_下载_热门驱动_驱动精灵...
Prolific PL2303 (串口线)USB转串口驱动3.3.2.102版For WinXP-64/Vista-64/Win7-64/Win8-64/Win8.1-64/Win10-64.目前主流 ...
- USB转串口驱动分析(一)
之前追踪代码用的grep命令效率太低了,所以这次下载C代码阅读跳转利器ctags.cscope用于分析代码 因为用的是Centos6.7所以需要用到yum install安装软件 [wuyujun@w ...
- 银河麒麟 安装PL2303GC USB转串口驱动
银河麒麟系统,外接一个设备,连接线是USB转串口线,型号是PL2303,直接接到电脑上,系统没有自动加载这个设备,理论上来说应该识别为/dev/ttyUSB0. 进入USB转串口驱动的目录查看: /u ...
- Linux Ubuntu18.04系统 USB转串口驱动安装,查看串口号
When you plugin your USB-UART converter, and run "> ls /dev/tty*", if you don't see the ...
最新文章
- ATEN旗下品牌K博士强势出击个人级/小型商用市场
- HDLBits 系列(27)孰对孰错 之 Fsm onehot?
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[上]:采用管道处理请求...
- Winform中设置ZedGraph的多条Y轴的标题和刻度不显示十次幂
- c语言课程设计加密程序,C语言课程设计文件加密解密.doc
- 03-树1 树的同构 (25 分)
- Java二十三设计模式之------工厂方法模式
- 5分钟制作Unity过场动画 | Timeline
- 花了一个月精心准备30张可视化大屏模板,可直接套用,拿走不谢
- 似然函数(likelihood function)
- 通过程序得到数据库表之间的关联关系
- 【优化算法】磷虾群算法(KH)【含matlab源码 133期】
- Python学习笔记—— python基础 1. 变量的输出
- IPVS使用的Netfilter Hook点
- 利用一阶谓词逻辑求解猴子摘香蕉问题
- matlab特殊符号名称,MATLAB——matlab特殊符号表【转载】(示例代码)
- Windows 10 build Error !include: could not find: ****StdUtils.nsh
- DKIM、DMARC 和 SPF:设置电子邮件安全
- [软考]之原码、反码、补码和移码
- 机关值班php代码,机关事业单位值班制度