multithreading - FTDI Android - create new activity -
this code able make android device usb host hardware model. can read data hardware correctly in main activity. however, moved activity, still works data reading incorrect. instance, i'm trying write data read file. first activity input filename , button send activity. code below in second activity
public class temp extends activity { private fileoutputstream outputstream; public static d2xxmanager ftd2xx= null; handler mhandler = new handler(); ft_device ftdev = null; int devcount = 0; usbdevice device = null; textview text =null; string temp = null; _4dpoint p = null; int rd = 0; byte[] byt = null; byte[] fdata = null; string outp = ""; string from_serial = ""; int min = -1; string filename; context c; final runnable updateresults = new runnable() { @override public void run() { // todo auto-generated method stub text.settext("" + min + '\n' + temp); } }; public void getdata(){ try { outputstream = openfileoutput(filename, context.mode_private); byt = new byte[256];//{(byte)'a','b','c','d',}; toast.maketext(getbasecontext(), "start " + filename , toast.length_long).show(); text = (textview)findviewbyid(r.id.test2); device = (usbdevice) getintent().getparcelableextra("usb"); ftd2xx = d2xxmanager.getinstance(c); ftd2xx.addusbdevice(device); devcount = ftd2xx.createdeviceinfolist(c); if (devcount > 0) { ftdev = ftd2xx.openbyusbdevice(c, device); } if( ftdev.isopen() == true ) { ftdev.setbitmode((byte)0 , d2xxmanager.ft_bitmode_reset); ftdev.setbaudrate(38400); ftdev.setdatacharacteristics(d2xxmanager.ft_data_bits_8, d2xxmanager.ft_stop_bits_1, d2xxmanager.ft_parity_none); ftdev.setflowcontrol(d2xxmanager.ft_flow_none, (byte) 0x0b, (byte) 0x0d); thread t = new thread() { public void run() { int i; while(true){ rd=0; while (rd==0){ rd = ftdev.read(byt, 14); } for(i=0; i<rd; i++) outp += (char)byt[i]; from_serial = new string(outp); p = new _4dpoint(from_serial); temp = string.format("%s: %f %f %f %f %d\n", from_serial, p.r, p.g, p.b, p.l, p.camera); try { outputstream.write(temp.getbytes()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } outp = ""; mhandler.post(updateresults); } } }; t.start(); } } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (d2xxexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_color); // show button in action bar. setupactionbar(); intent intent = getintent(); filename = intent.getstringextra("file name"); c = this; getdata(); }
the set should fine since it's reading data hardware, data read incorrect. also, i'm wondering why need create new thread while reading data. tried not creating new thread , didn't work well, still have no idea why? tried contact person wrote code read data no reply. appreciated :)
you state receive data, therefor think should @ ftdev settings. try example set ftdev.setbaudrate(115200)
(this worked me) or try playing other ftdev
settings little bit.
the settings use in programm are:
int baudrate = 115200; byte stopbit = 1; /*1:1stop bits, 2:2 stop bits*/ byte databit = 8; /*8:8bit, 7: 7bit*/ byte parity = 0; /* 0: none, 1: odd, 2: even, 3: mark, 4: space*/ byte flowcontrol = 1; /*0:none, 1: flow control(cts,rts)*/
if won't work, wise first check data communication computer program e.g. or analyse incomming 'wrong' data.
Comments
Post a Comment