最近做了个平板与单片机的项目,由于使用的平板不支持串口,所以中间借助了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 ProlificSerialDri