Sending serial data to Arduino using RXTX Java library -


i have code reads line of serial data arduino. have arduino set send out whatever data reads in, or if there no data read send out "no data". receiving "no data" java console fine, , arduino code works expected when using terminal. cannot java send data arduino, have tried various methods no luck :( ideally serial event method used send data great. (the receive 1 working).

any appreciated, code shown below:

public class serialcomm implements serialporteventlistener { serialport serialport;     /** port we're going use. */ private static final string port_names[] = {          "/dev/tty.usbserial-a9007ux1", // mac os x                     "/dev/ttyacm0", // raspberry pi         "/dev/ttyusb0", // linux         "com3", // windows }; /** * bufferedreader fed inputstreamreader  * converting bytes characters  * making displayed results codepage independent */ private bufferedreader input; /** output stream port */ private outputstream output; /** milliseconds block while waiting port open */ private static final int time_out = 2000; /** default bits per second com port. */ private static final int data_rate = 115200;  public void initialize() {     commportidentifier portid = null;     enumeration portenum = commportidentifier.getportidentifiers();      //first, find instance of serial port set in port_names.     while (portenum.hasmoreelements()) {         commportidentifier currportid = (commportidentifier) portenum.nextelement();         (string portname : port_names) {             if (currportid.getname().equals(portname)) {                 portid = currportid;                 break;             }         }     }     if (portid == null) {         system.out.println("could not find com port.");         return;     }      try {         // open serial port, , use class name appname.         serialport = (serialport) portid.open(this.getclass().getname(),                 time_out);          // set port parameters         serialport.setserialportparams(data_rate,                 serialport.databits_8,                 serialport.stopbits_1,                 serialport.parity_none);          // open streams         input = new bufferedreader(new inputstreamreader(serialport.getinputstream()));         output = serialport.getoutputstream();          // add event listeners         serialport.addeventlistener(this);         serialport.notifyondataavailable(true);     } catch (exception e) {         system.err.println(e.tostring());     } }  /**  * should called when stop using port.  * prevent port locking on platforms linux.  */ public synchronized void close() {     if (serialport != null) {         serialport.removeeventlistener();         serialport.close();     } }  /**  * handle event on serial port. read data , print it.  */ public synchronized void serialevent(serialportevent oevent) {     if (oevent.geteventtype() == serialportevent.data_available) {         try {             string inputline=input.readline();             system.out.println(inputline);         } catch (exception e) {             system.err.println(e.tostring());         }     }          //trying implement serial event sending data here } 

}

upload sketch arduino prints on serial , connect computer run java program.


Popular posts from this blog