java - how to send data to com port in android using uart wired connection? -
i can send data ttys1 using terminel of android. how can using code in application? had tried libraries not working me. can suggest solution? code using rxtxcomm.jar, showing error *'couldn't find "librxtxserial.so"'. * can provide solution? in advance..
public void open() { enumeration port_list = commportidentifier.getportidentifiers(); while (port_list.hasmoreelements()) { // list of ports commportidentifier port_id = (commportidentifier) port_list.nextelement(); if (port_id.getname().equals("/dev/ttys1")) { // attempt open try { serialport port = (serialport) port_id.open("portlistopen", 20); system.out.println("opened successfully"); try { int baudrate = 115200; // port.setserialportparams(baudrate, serialport.databits_7, serialport.stopbits_1, serialport.parity_even); port.setdtr(true); /* port.setflowcontrolmode( serialport.flowcontrol_none); * */ system.out.println("properties set"); } catch (unsupportedcommoperationexception e) { system.out.println(e); } try { //input = new serialreader(in); port.addeventlistener((serialporteventlistener) this); system.out.println("listeners attached" + this); } catch (toomanylistenersexception e) { system.out.println("too many listeners"); } port.notifyondataavailable(true); //port.notifyonoutputempty(true); //sendmessage(port,"@pl"); //port.close (); try { inputstream = port.getinputstream(); system.out.println("inputstream" + inputstream.available()); outputstream = (outputstream) port.getoutputstream(); } catch (ioexception e) { system.out.println(e); } //set created variables global variables port = port; inputstream = inputstream; outputstream = outputstream; } catch (portinuseexception pe) { system.out.println("open failed"); string owner_name = port_id.getcurrentowner(); if (owner_name == null) { system.out.println("port owned unidentified app"); } else // owner name not returned correctly unless // java program. { system.out.println(" " + owner_name); } } } } } public static void sendmessage(string port, string msg) { if (port != null) { try { outputstream.write(msg.getbytes()); outputstream.flush(); try { thread.sleep(2000); // sure data xferred before closing system.out.println("read called"); //simpleread read = new simpleread(); //int read = global_variables.inputstream.read(); //system.out.println("read call ended"+read); } catch (exception e) { } } catch (ioexception ex) { ex.printstacktrace(); } } } public void serialevent(serialportevent event) { system.out.println(event.geteventtype()); switch (event.geteventtype()) { case serialportevent.data_available: system.out.println("inside event handler data available"); byte[] readbuffer = new byte[20]; try { while (inputstream.available() > 0) { int numbytes = inputstream.read(readbuffer); } system.out.print(new string(readbuffer)); system.exit(1); } catch (ioexception e) { system.out.println(e); } break; } }
Comments
Post a Comment