Write string value to the arduino or the serial port using java -
i trying write string data serial port, cannot write string value arduino serial port. can me solve this.
this java code:
portlist = commportidentifier.getportidentifiers(); while (portlist.hasmoreelements()) { portid = (commportidentifier) portlist.nextelement(); if (portid.getporttype() == commportidentifier.port_serial) { if (portid.getname().equals("com9")) { try { serialport = (serialport) portid.open("simplewriteapp", 2000); } catch (portinuseexception e) { system.out.println("error1 "+ e); } try { outputstream = serialport.getoutputstream(); } catch (ioexception e) { system.out.println("error2 "+ e); } try { serialport.setserialportparams(9600, serialport.databits_8, serialport.stopbits_1, serialport.parity_none); } catch (unsupportedcommoperationexception e) { system.out.println("error3 "+ e); } try { string number = "1"; byte arduino[] = number.getbytes(); for(int i=0; i<arduino.length; i++){ outputstream.write(arduino[i]); } outputstream.flush(); } catch (ioexception e) { system.out.println("error is4 "+ e); } } } }
Comments
Post a Comment