Friday, April 18, 2014

Control PL2303HX on Android


Hello everyone!
I now have a problem with Android development is that I am using the Android USB Host function, through the Android phone and microcontroller program to communicate using PL2303HX USB to RS232 or TTL to achieve this function, the current problem is that I can in Android USB device found on the plate, but can not open, set its baud rate, etc, will not try for a long time, I do not know what the brothers have done to develop this area, pointing, thank you!
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
/ / Look here, http://www.2cto.com/kf/201212/174105.html
/ / Put the code directly

package com.android.usb;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;

import com.hoho.android.usbserial.driver.FtdiSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.hoho.android.usbserial.util.SerialInputOutputManager.Listener;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget *;.


public class UsbtestActivity extends Activity {
/ ** Called when the activity is first created. * /
protected TextView txtTips;
protected EditText edSend;
protected EditText edReceive;
protected Button btnSend;

private UsbManager managerme;
private FtdiSerialDriver driver;
/ / Private SerialInputOutputManager rs232ReadWrite;
private ReadThread readThread;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";




/ / Receive RS232 data events
public class ReadListener {

public void onRunError (Exception e) {
/ / TODO Auto-generated method stub

}

public void onNewData (final byte [] data) {
/ / TODO Auto-generated method stub
UsbtestActivity.this.runOnUiThread (new Runnable () {
public void run () {
UsbtestActivity.this.updateReceivedData (data);
}
});
}
};

private ReadListener readListener = new ReadListener ();
/ * Private Listener readListener = new Listener ()
{
public void onNewData (final byte [] data)
{
UsbtestActivity.this.runOnUiThread (new Runnable () {
public void run () {
String recString = data.toString ();
edReceive.setText (recString);
}
});
}

public void onRunError (Exception e)
{

}
};
* /


@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
txtTips = (TextView) this.findViewById (R.id.txtTips);
edSend = (EditText) this.findViewById (R.id.edSend);
edReceive = (EditText) this.findViewById (R.id.edReceive);
btnSend = (Button) this.findViewById (R.id.btnSend);
managerme = (UsbManager) getSystemService (Context.USB_SERVICE);
}

@ Override
protected void onDestroy () {
btnCloseClick (null);

super.onDestroy ();
}

/ / When calling getUsb (), excitation
authorization application for USB devicesprivate final BroadcastReceiver mUsbReceiver = new BroadcastReceiver () {

public void onReceive (Context context, Intent intent) {
String action = intent.getAction ();
/ / Get the authorization
if (ACTION_USB_PERMISSION.equals (action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra (UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra (UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device! = null) {
/ / Call method to set up device communication
UsbDeviceConnection connection = managerme.openDevice (device);
if (connection == null)
{
txtTips.setText (txtTips.getText () + "/ connection is null");
}
else {
driver = new FtdiSerialDriver (device, connection);
open232Port ();
txtTips.setText (txtTips.getText () + "/ success connect 2");
}

}
}
else {
/ / Log.d (TAG, "permission denied for device" + device);
txtTips.setText (txtTips.getText () + "/ permission denied");
}
}
}

/ / USB device is unplugged
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals (action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra (UsbManager.EXTRA_DEVICE);
if (device! = null) {
/ / Call your method that cleans up and closes communication with the device
btnCloseClick (null);
}
}
}
};

private class ReadThread extends Thread {
/ / Private ArrayList mList;
private Boolean IsRun = true;
private ReadListener mListener;
private static final int BUFSIZ = 4096;
private static final int READ_WAIT_MILLIS = 200;
private final ByteBuffer mReadBuffer = ByteBuffer.allocate (BUFSIZ);
/ / Private final byte [] mReadBuffer = new byte [BUFSIZ];



public ReadThread (ReadListener listener) {
super ();
mListener = listener;
}
public void Stop ()
{
IsRun = false;
}
public synchronized ReadListener getListener () {
return mListener;
}
public void run () {

while (IsRun)
{
if (driver! = null)
{
int len ​​= 0;
mReadBuffer.clear ();
try {
len = driver.read (mReadBuffer.array (), READ_WAIT_MILLIS);
} Catch (Exception e) {
/ / TODO: handle exception
}

if (len> 0) {
final ReadListener listener = getListener ();
if (listener! = null) {
try {
final byte [] data = mReadBuffer.toString () getBytes ("gbk");.
listener.onNewData (data);
} Catch (UnsupportedEncodingException e) {
/ / TODO Auto-generated catch block
/ / E.printStackTrace ();
}
}
mReadBuffer.clear ();
}
}
}
}
};

/ / Connect to a USB device, RS-232 port and open
public void getUsb ()
{

HashMap deviceList = managerme.getDeviceList ();
txtTips.setText ("size ():" + deviceList.size ());
Iterator deviceIterator = deviceList.values ​​() iterator ();.
while (deviceIterator.hasNext ()) {

UsbDevice tmpDevice = deviceIterator.next ();
/ / <-! Liang RS232-USB microarrays Prolific 0x067B = 1659/2303 = 8963 ->
/ / <-! DTECH USB-232 cable 0x0403 = 1027/0x6001 = 24577 ->
/ / If (usd.getVendorId () == 1659 && usd.getProductId () == 8963)
if (tmpDevice.getVendorId () == 1027 && tmpDevice.getProductId () == 24577)
{
txtTips.setText (txtTips.getText () + "/ found usb device");

UsbDeviceConnection connection = null;
if (managerme.hasPermission (tmpDevice))
{
connection = managerme.openDevice (tmpDevice);
}

if (connection == null)
{
/ / Authorization
PendingIntent mPermissionIntent = PendingIntent.getBroadcast (this, 0, new Intent (
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION);
/ / Register the USB authorized notifier, if authorized, but there is a message to open the device
registerReceiver (mUsbReceiver, filter);
/ / Application for authorization, until mUsbReceiver successfully created driver
managerme.requestPermission (tmpDevice, mPermissionIntent);
}
else
{
driver = new FtdiSerialDriver (tmpDevice, connection);
open232Port ();
txtTips.setText (txtTips.getText () + "/ success connect 1");

}
/ / Open the serial code in real mUsbReceiver
return;
}
}
}

/ / When the driver connected, open 232, and initializes the reader thread
public void open232Port ()
{
if (driver! = null)
{
try {
driver.open ();
driver.setParameters (9600, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
txtTips.setText (txtTips.getText () + "/ success open");

readThread = new ReadThread (readListener);
readThread.start ();
/ / This is an interface, no need to call to run the interface
/ / Rs232ReadWrite = new SerialInputOutputManager (driver, readListener);
/ / Rs232ReadWrite.run ();
/ / Thread.sleep (1000);
/ / Rs232ReadWrite.run ();

} Catch (Exception e) {
/ / TODO: handle exception
txtTips.setText (txtTips.getText () + "/ open error:" + e.getMessage ());
driver = null;
}
}
else
{
txtTips.setText (txtTips.getText () + "/ connection is null");
}
}

/ / Open the serial port
public void btnOpenClick (View v)
{
btnCloseClick (v);
getUsb ();
}


/ / Close the serial
public void btnCloseClick (View v)
{
if (driver! = null)
{
/ * If (rs232ReadWrite! = null)
{
rs232ReadWrite.stop ();
rs232ReadWrite = null;
} * /
if (readThread! = null)
{
readThread.Stop ();
readThread = null;
}
driver.close ();
driver = null;
}
/ / EdReceive.setText ("close click");
}

public void btnSendClick (View v) throws UnsupportedEncodingException
{
/ / EdReceive.setText ("buton click");
if (driver! = null)
{
byte buffer [] = edSend.getText () toString () getBytes ("gbk");..
byte endChar [] = {0x61, 0x62, 0x63, 0x0d, 0x0a};
try {
driver.write (buffer, 500);
driver.write (endChar, 500);

/ / Rs232ReadWrite.writeAsync (buffer);
/ / Rs232ReadWrite.writeAsync (endChar);
} Catch (Exception e) {
/ / TODO: handle exception
txtTips.setText (txtTips.getText () + e.getMessage ());
}
}
else {
edReceive.setText ("driver is null");
}
}

private void updateReceivedData (byte [] data) {
final String recString = data.toString ();
edReceive.setText (recString);
}

}
Reply:
Thank you too meegowei1 brother, I'll go under test.
Reply:
Have to get, it was my one GND line did not answer, thank you!

No comments:

Post a Comment