在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 基于FT311协议的USB通信实例

基于FT311协议的USB通信实例

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.20M
  • 下载次数:17
  • 浏览次数:263
  • 发布时间:2017-02-15
  • 实例类别:Android平台开发
  • 发 布 人:wodezuiqiao
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 实例 11 通信 1

实例介绍

【实例简介】

    基于FT311协议的USB通信实例

【核心代码】

    /******************************FT311 GPIO interface class******************************************/
public class FT311UARTInterface extends Activity
{

private static final String ACTION_USB_PERMISSION =    "com.UARTLoopback.USB_PERMISSION";
public UsbManager usbmanager;
public UsbAccessory usbaccessory;
public PendingIntent mPermissionIntent;
public ParcelFileDescriptor filedescriptor = null;
public FileInputStream inputstream = null;
public FileOutputStream outputstream = null;
public boolean mPermissionRequestPending = false;
public read_thread readThread;

private byte [] usbdata; 
private byte [] writeusbdata;
private byte  [] readBuffer; /*circular buffer*/
private int readcount;
private int totalBytes;
private int writeIndex;
private int readIndex;
private byte status;
final int  maxnumbytes = 65536;

public boolean datareceived = false;
public boolean READ_ENABLE = false;
public boolean accessory_attached = false;

public Context global_context;

public static String ManufacturerString = "mManufacturer=FTDI";
public static String ModelString1 = "mModel=FTDIUARTDemo";
public static String ModelString2 = "mModel=Android Accessory FT312D";
public static String VersionString = "mVersion=1.0";

public SharedPreferences intsharePrefSettings;

/*constructor*/
public FT311UARTInterface(Context context, SharedPreferences sharePrefSettings){
super();
global_context = context;
intsharePrefSettings = sharePrefSettings;
/*shall we start a thread here or what*/
usbdata = new byte[1024]; 
writeusbdata = new byte[256];
/*128(make it 256, but looks like bytes should be enough)*/
readBuffer = new byte [maxnumbytes];


readIndex = 0;
writeIndex = 0;
/***********************USB handling******************************************/

usbmanager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
// Log.d("LED", "usbmanager" usbmanager);
mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
context.registerReceiver(mUsbReceiver, filter);

inputstream = null;
outputstream = null;
}


public void SetConfig(int baud, byte dataBits, byte stopBits,
byte parity, byte flowControl)
{

/*prepare the baud rate buffer*/
writeusbdata[0] = (byte)baud;
writeusbdata[1] = (byte)(baud >> 8);
writeusbdata[2] = (byte)(baud >> 16);
writeusbdata[3] = (byte)(baud >> 24);

/*data bits*/
writeusbdata[4] = dataBits;
/*stop bits*/
writeusbdata[5] = stopBits;
/*parity*/
writeusbdata[6] = parity;
/*flow control*/
writeusbdata[7] = flowControl;

/*send the UART configuration packet*/
SendPacket((int)8);
}


/*write data*/
public byte SendData(int numBytes, byte[] buffer) 
{
status = 0x00; /*success by default*/
/*
* if num bytes are more than maximum limit
*/
if(numBytes < 1){
/*return the status with the error in the command*/
return status;
}

/*check for maximum limit*/
if(numBytes > 256){
numBytes = 256;
}

/*prepare the packet to be sent*/
for(int count = 0;count<numBytes;count )
{
writeusbdata[count] = buffer[count];
}

if(numBytes != 64)
{
SendPacket(numBytes);
}
else
{
byte temp = writeusbdata[63];
SendPacket(63);
writeusbdata[0] = temp;
SendPacket(1);
}

return status;
}

/*read data*/
public byte ReadData(int numBytes,byte[] buffer, int [] actualNumBytes)
{
status = 0x00; /*success by default*/

/*should be at least one byte to read*/
if((numBytes < 1) || (totalBytes == 0)){
actualNumBytes[0] = 0;
status = 0x01;
return status;
}

/*check for max limit*/
if(numBytes > totalBytes)
numBytes = totalBytes;

/*update the number of bytes available*/
totalBytes -= numBytes;

actualNumBytes[0] = numBytes;

/*copy to the user buffer*/
for(int count = 0; count<numBytes;count )
{
buffer[count] = readBuffer[readIndex];
readIndex ;
/*shouldnt read more than what is there in the buffer,
* so no need to check the overflow
*/
readIndex %= maxnumbytes;
}
return status;
}

/*method to send on USB*/
private void SendPacket(int numBytes)
{
try {
if(outputstream != null){
outputstream.write(writeusbdata, 0,numBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}

/*resume accessory*/
public int ResumeAccessory()
{
// Intent intent = getIntent();
if (inputstream != null && outputstream != null) {
return 1;
}

UsbAccessory[] accessories = usbmanager.getAccessoryList();
if(accessories != null)
{
Toast.makeText(global_context, "Accessory Attached", Toast.LENGTH_SHORT).show();
}
else
{
// return 2 for accessory detached case
//Log.e(">>@@","ResumeAccessory RETURN 2 (accessories == null)");
accessory_attached = false;
return 2;
}

UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if( -1 == accessory.toString().indexOf(ManufacturerString))
{
Toast.makeText(global_context, "Manufacturer is not matched!", Toast.LENGTH_SHORT).show();
return 1;
}

if( -1 == accessory.toString().indexOf(ModelString1) && -1 == accessory.toString().indexOf(ModelString2))
{
Toast.makeText(global_context, "Model is not matched!", Toast.LENGTH_SHORT).show();
return 1;
}

if( -1 == accessory.toString().indexOf(VersionString))
{
Toast.makeText(global_context, "Version is not matched!", Toast.LENGTH_SHORT).show();
return 1;
}

Toast.makeText(global_context, "Manufacturer, Model & Version are matched!", Toast.LENGTH_SHORT).show();
accessory_attached = true;

if (usbmanager.hasPermission(accessory)) {
OpenAccessory(accessory);

else
{
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
Toast.makeText(global_context, "Request USB Permission", Toast.LENGTH_SHORT).show();
usbmanager.requestPermission(accessory,
mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {}

return 0;
}

/*destroy accessory*/
public void DestroyAccessory(boolean bConfiged){

if(true == bConfiged){
READ_ENABLE = false;  // set false condition for handler_thread to exit waiting data loop
writeusbdata[0] = 0;  // send dummy data for instream.read going
SendPacket(1);
}
else
{
SetConfig(9600,(byte)1,(byte)8,(byte)0,(byte)0);  // send default setting data for config
try{Thread.sleep(10);}
catch(Exception e){}

READ_ENABLE = false;  // set false condition for handler_thread to exit waiting data loop
writeusbdata[0] = 0;  // send dummy data for instream.read going
SendPacket(1);
if(true == accessory_attached)
{
saveDefaultPreference();
}
}

try{Thread.sleep(10);}
catch(Exception e){}
CloseAccessory();
}

/*********************helper routines*************************************************/

public void OpenAccessory(UsbAccessory accessory)
{
filedescriptor = usbmanager.openAccessory(accessory);
if(filedescriptor != null){
usbaccessory = accessory;

FileDescriptor fd = filedescriptor.getFileDescriptor();

inputstream = new FileInputStream(fd);
outputstream = new FileOutputStream(fd);
/*check if any of them are null*/
if(inputstream == null || outputstream==null){
return;
}

if(READ_ENABLE == false){
READ_ENABLE = true;
readThread = new read_thread(inputstream);
readThread.start();
}
}
}

private void CloseAccessory()
{
try{
if(filedescriptor != null)
filedescriptor.close();

}catch (IOException e){}

try {
if(inputstream != null)
inputstream.close();
} catch(IOException e){}

try {
if(outputstream != null)
outputstream.close();

}catch(IOException e){}
/*FIXME, add the notfication also to close the application*/

filedescriptor = null;
inputstream = null;
outputstream = null;

System.exit(0);
}

protected void saveDetachPreference() {
if(intsharePrefSettings != null)
{
intsharePrefSettings.edit()
.putString("configed", "FALSE")
.commit();
}
}

protected void saveDefaultPreference() {
if(intsharePrefSettings != null)
{
intsharePrefSettings.edit().putString("configed", "TRUE").commit();
intsharePrefSettings.edit().putInt("baudRate", 9600).commit();
intsharePrefSettings.edit().putInt("stopBit", 1).commit();
intsharePrefSettings.edit().putInt("dataBit", 8).commit();
intsharePrefSettings.edit().putInt("parity", 0).commit();
intsharePrefSettings.edit().putInt("flowControl", 0).commit();
}
}

/***********USB broadcast receiver*******************************************/
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() 
{
@Override
public void onReceive(Context context, Intent intent) 
{
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) 
{
synchronized (this)
{
UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
{
Toast.makeText(global_context, "Allow USB Permission", Toast.LENGTH_SHORT).show();
OpenAccessory(accessory);

else 
{
Toast.makeText(global_context, "Deny USB Permission", Toast.LENGTH_SHORT).show();
Log.d("LED", "permission denied for accessory " accessory);

}
mPermissionRequestPending = false;
}

else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) 
{
saveDetachPreference();
DestroyAccessory(true);
//CloseAccessory();
}else
{
Log.d("LED", "....");
}
}
};

/*usb input data handler*/
private class read_thread  extends Thread 
{
FileInputStream instream;

read_thread(FileInputStream stream ){
instream = stream;
this.setPriority(Thread.MAX_PRIORITY);
}

public void run()
{
while(READ_ENABLE == true)
{
while(totalBytes > (maxnumbytes - 1024))
{
try 
{
Thread.sleep(50);
}
catch (InterruptedException e) {e.printStackTrace();}
}

try
{
if(instream != null)
{
readcount = instream.read(usbdata,0,1024);
if(readcount > 0)
{
for(int count = 0;count<readcount;count )
{    
readBuffer[writeIndex] = usbdata[count];
writeIndex ;
writeIndex %= maxnumbytes;
}

if(writeIndex >= readIndex)
totalBytes = writeIndex-readIndex;
else
totalBytes = (maxnumbytes-readIndex) writeIndex;

//     Log.e(">>@@","totalBytes:" totalBytes);
}
}
}
catch (IOException e){e.printStackTrace();}
}
}
}
}

标签: 实例 11 通信 1

实例下载地址

基于FT311协议的USB通信实例

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警