پاسخ : ارتباط اندروید یا AVR
چرا تاپیک خاک گرفت؟یادگرفتید تموم شد دیگه؟
چرا تاپیک خاک گرفت؟یادگرفتید تموم شد دیگه؟



package ista.osc.serial;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.ftdi.j2xx.D2xxManager;
import com.ftdi.j2xx.FT_Device;
import com.ftdi.j2xx.D2xxManager.D2xxException;
public class FtdiUART {
//read variables
public static final int readLength = 512;
public int readcount = 0;
byte[] readData;
char[] readDataToText;
public boolean bReadThreadGoing = false;
public readThread read_thread;
static int iEnableReadFlag = 1;
boolean uart_configured = false;
public int iavailable = 0;
Handler ReadHandler;
D2xxManager ftdid2xx = null;
FT_Device ftDevice = null;
D2xxManager.DriverParameters d2xxDrvParameter;
int DevCount = -1;
Context myContext;
public FtdiUART(Context context,Handler handler){
myContext=context;
ReadHandler=handler;
}
public void createDeviceList()
{
try {
ftdid2xx = D2xxManager.getInstance(myContext);
} catch (D2xxException e) {
e.printStackTrace();
}
DevCount=ftdid2xx.createDeviceInfoList(myContext);
}
public int connectFunction()
{
if (DevCount > 0)
{
ftDevice = ftdid2xx.openByLocation(myContext, 0);
ftDevice.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
if (ftDevice.isOpen()) {
Toast.makeText(myContext, "Device open ok",1 ).show();
if(false == bReadThreadGoing)
{
read_thread = new readThread(ReadHandler);
read_thread.start();
bReadThreadGoing = true;
}
return 1;
}
else {
Toast.makeText(myContext, "Device open no ok",0 ).show();
return -1;
}
}
else{
Toast.makeText(myContext, "No Device",0 ).show();
return -1;
}
}
public void disconnectFunction()
{
DevCount=-1;
bReadThreadGoing = false;
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(ftDevice != null)
{
synchronized(ftDevice)
{
if( true == ftDevice.isOpen())
{
ftDevice.close();
}
}
}
}
public void SendMessage(String writeText) {
if (ftDevice.isOpen() == false) {
Log.e("j2xx", "SendMessage: device not open");
return;
}
ftDevice.setLatencyTimer((byte) 16);
// ftDev.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));
String writeData = writeText;
byte[] OutData = writeData.getBytes();
ftDevice.write(OutData, writeData.length());
}
public void EnableRead (){
ftDevice.purge((byte) (D2xxManager.FT_PURGE_TX));
ftDevice.restartInTask();
}
public void DisableRead (){
ftDevice.stopInTask();
}
private class readThread extends Thread
{
Handler mHandler;
readThread(Handler h){
mHandler = h;
this.setPriority(Thread.MIN_PRIORITY);
}
@Override
public void run()
{
int i;
while(true == bReadThreadGoing)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
synchronized(ftDevice)
{
iavailable = ftDevice.getQueueStatus();
if (iavailable > 0) {
if(iavailable > readLength){
iavailable = readLength;
}
ftDevice.read(readData, iavailable);
for (i = 0; i < iavailable; i++) {
readDataToText[i] = (char) readData[i];
}
Message msg = mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
}
}
}
}
public void SetConfig(int baud, byte dataBits, byte stopBits, byte parity, byte flowControl)
{
if(ftDevice==null)
return;
if (ftDevice.isOpen() == false) {
Log.e("j2xx", "SetConfig: device not open");
return;
}
// configure our port
// reset to UART mode for 232 devices
ftDevice.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
ftDevice.setBaudRate(baud);
switch (dataBits) {
case 7:
dataBits = D2xxManager.FT_DATA_BITS_7;
break;
case 8:
dataBits = D2xxManager.FT_DATA_BITS_8;
break;
default:
dataBits = D2xxManager.FT_DATA_BITS_8;
break;
}
switch (stopBits) {
case 1:
stopBits = D2xxManager.FT_STOP_BITS_1;
break;
case 2:
stopBits = D2xxManager.FT_STOP_BITS_2;
break;
default:
stopBits = D2xxManager.FT_STOP_BITS_1;
break;
}
switch (parity) {
case 0:
parity = D2xxManager.FT_PARITY_NONE;
break;
case 1:
parity = D2xxManager.FT_PARITY_ODD;
break;
case 2:
parity = D2xxManager.FT_PARITY_EVEN;
break;
case 3:
parity = D2xxManager.FT_PARITY_MARK;
break;
case 4:
parity = D2xxManager.FT_PARITY_SPACE;
break;
default:
parity = D2xxManager.FT_PARITY_NONE;
break;
}
ftDevice.setDataCharacteristics(dataBits, stopBits, parity);
short flowCtrlSetting;
switch (flowControl) {
case 0:
flowCtrlSetting = D2xxManager.FT_FLOW_NONE;
break;
case 1:
flowCtrlSetting = D2xxManager.FT_FLOW_RTS_CTS;
break;
case 2:
flowCtrlSetting = D2xxManager.FT_FLOW_DTR_DSR;
break;
case 3:
flowCtrlSetting = D2xxManager.FT_FLOW_XON_XOFF;
break;
default:
flowCtrlSetting = D2xxManager.FT_FLOW_NONE;
break;
}
ftDevice.setFlowControl(flowCtrlSetting, (byte) 0x0b, (byte) 0x0d);
uart_configured = true;
Toast.makeText(myContext, "Config done", Toast.LENGTH_SHORT).show();
}
}
دیدگاه