Arduino Java SerialEvent not being called -
i following simple example found here (example one), have arduino connected raspberry pi , read data arduino on pi in java.
the issue serialevent method never called, implies no data coming in. however, when open serial monitor can see data being read correctly.
the correct serial port being used well.
here java code.
//this class: // - starts communication arduino. // - reads data coming in arduino , // converts data in useful form. // - closes communication arduino. //code builds upon great example: //http://www.csc.kth.se/utbildning/kth/kurser/dh2400/interak06/serialwork.java //the addition being conversion incoming characters numbers. //load libraries import java.io.*; import java.util.toomanylistenersexception; //load rxtx library import gnu.io.*; class arduinocomm implements serialporteventlistener { //used in process of converting read in characters- //-first in string , number. string rawstr=""; //declare serial port variable serialport myserialport; //declare input steam inputstream in; boolean stop=false; public void start(string portname,int baudrate) { stop=false; try { //finds , opens port commportidentifier portid = commportidentifier.getportidentifier(portname); myserialport = (serialport)portid.open("my_java_serial" + portname, 2000); system.out.println("serial port found , opened"); //configure port try { myserialport.setserialportparams(baudrate, myserialport.databits_8, myserialport.stopbits_1, myserialport.parity_none); system.out.println("serial port params set: "+baudrate); } catch (unsupportedcommoperationexception e) { system.out.println("probably unsupported speed"); } //establish stream reading port try { in = myserialport.getinputstream(); } catch (ioexception e) { system.out.println("couldn't streams"); } // read "in" in separate thread, api gives events try { myserialport.addeventlistener(this); myserialport.notifyondataavailable(true); system.out.println("event listener added"); } catch (toomanylistenersexception e) { system.out.println("couldn't add listener"); } } catch (exception e) { system.out.println("port in use: "+e); } } //used close serial port public void closeserialport() { try { in.close(); stop=true; myserialport.close(); system.out.println("serial port closed"); } catch (exception e) { system.out.println(e); } } //reads incoming data packets arduino. public void serialevent(serialportevent event) { //reads in data while data available while (event.geteventtype()== serialportevent.data_available && stop==false) { try { //------------------------------------------------------------------- //read in available character char ch = (char)in.read(); //if read character letter means have found identifier. if (character.isletter(ch)==true && rawstr!="") { //convert string containing characters since last identifier integer int value = integer.parseint(rawstr); if (ch=='a') { system.out.println("value is: "+value); } if (ch=='b') { system.out.println("value b is: "+value); } //reset rawstr ready next reading rawstr = (""); } else { //add incoming characters string. //only add characters string if digits. //when arduino starts first characters sends through s-t-a-r-t- //and avoid adding these characters add characters if digits. if (character.isdigit(ch)) { rawstr = ( rawstr + character.tostring(ch)); } else { system.out.print(ch); } } } catch (ioexception e) { } } } }
and here arduino sketch
//ardunio code part 01 //first define values sent //note: java code go example reads- //-in integers values have sent integers int valuea = 21; int valueb = 534; void setup() { serial.begin(115200); serial.println("start"); } void loop() { //we send value coupled identifier character //that both marks end of value , value is. serial.print(valuea); serial.print("a"); serial.print(valueb); serial.print("b"); //a delay slow program down human pace. delay(500); }
i have read changing serial.print serial.write new way this, changing had no result.
so reason no events being triggered due program exiting , ending before had chance to.
to solve added thread loop inside inside main thread.
public static void main(string[] args) throws exception { arduinocomm = new arduinocomm (); main.start("/dev/ttyusb0",115200); thread t=new thread() { public void run() { try { //messy implementation works demo purpose while(true){ //optional sleep thread.sleep(500); } } catch (interruptedexception ie) {} } }; t.start(); system.out.println("started"); }
bit of oversight on part.
Comments
Post a Comment