java - Output on buffer write using serial monitor of arduino -
i trying send data serial monitor of arduino , make led on , of can't yet problem in code kindly check here arduino code
int led = 12; // led connected digital pin 13 int pts = 2; // powertail switch 2 connected digital pin 2 int recv = 0; // byte received on serial port void setup() { // initialize onboard led (led), powertail (pts) , serial port pinmode(led, output); pinmode(pts, output); digitalwrite(led,low); serial.begin(9600); } void loop() { // if serial port available, read incoming bytes if (serial.available() > 0) { recv = serial.read(); // if 'y' (decimal 121) received, turn led/powertail on // other 121 received, turn led/powertail off if (recv == 121){ digitalwrite(led, high); digitalwrite(pts,low); } else { digitalwrite(led, low); digitalwrite(pts,high); } // confirm values received in serial monitor window serial.print("--arduino received: "); serial.println(recv); } }
here java code
import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.filenotfoundexception; import java.io.printwriter; import java.util.calendar; import java.util.date; import java.util.scanner; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import com.fazecast.jserialcomm.serialport; import javax.swing.jlabel; import javax.swing.imageicon; import javax.swing.swingconstants; public class connection { static serialport chosenport; public static void main(string[] args) throws filenotfoundexception { // create , configure window jframe window = new jframe(); window.settitle("automatic car wash"); window.setsize(1000, 600); window.setdefaultcloseoperation(jframe.exit_on_close); window.getcontentpane().setlayout(null); date dateandtime = calendar.getinstance().gettime(); printwriter out = new printwriter("incom record save.txt"); // create drop-down box , connect button, place them @ top of window jcombobox<string> portlist = new jcombobox<string>(); jbutton connectbutton = new jbutton("connect"); jpanel toppanel = new jpanel(); toppanel.setbounds(0, 0, 584, 33); toppanel.add(portlist); toppanel.add(connectbutton); window.getcontentpane().add(toppanel); jlabel stageone = new jlabel("stage 1 gif"); stageone.seticon(new imageicon("e:\\thesis\\first stage gif.gif")); stageone.setbounds(76, 95, 160, 120); stageone.setvisible(false); window.getcontentpane().add(stageone); jlabel stageoneimage = new jlabel("stage 1 image "); stageoneimage.seticon(new imageicon("c:\\users\\dell\\desktop\\stagoneimage.jpg")); stageoneimage.setbounds(76, 95, 160, 120); window.getcontentpane().add(stageoneimage); jlabel totalcars = new jlabel("number of cars"); totalcars.sethorizontalalignment(swingconstants.center); totalcars.setbounds(156, 270, 138, 14); window.getcontentpane().add(totalcars); jlabel lbltotalcars = new jlabel("total cars="); lbltotalcars.sethorizontalalignment(swingconstants.center); lbltotalcars.setbounds(104, 270, 81, 14); window.getcontentpane().add(lbltotalcars); jlabel totalincome = new jlabel("0"); totalincome.sethorizontalalignment(swingconstants.center); totalincome.setbounds(365, 270, 92, 14); window.getcontentpane().add(totalincome); jlabel rs = new jlabel("rs"); rs.sethorizontalalignment(swingconstants.center); rs.setbounds(416, 270, 46, 14); window.getcontentpane().add(rs); jlabel datetime = new jlabel("todays date , time"); datetime.sethorizontalalignment(swingconstants.center); datetime.setbounds(184, 294, 173, 25); datetime.settext(dateandtime.tostring()); window.getcontentpane().add(datetime); jbutton btnsave = new jbutton(" save"); btnsave.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { out.close(); } }); btnsave.setbounds(26, 295, 89, 23); window.getcontentpane().add(btnsave); jlabel lbltotalincome = new jlabel("total income="); lbltotalincome.sethorizontalalignment(swingconstants.center); lbltotalincome.setbounds(304, 270, 92, 14); window.getcontentpane().add(lbltotalincome); jlabel = new jlabel("thanks"); thanks.sethorizontalalignment(swingconstants.center); thanks.seticon(new imageicon("c:\\users\\dell\\desktop\\thanks.gif")); thanks.setbounds(36, 333, 312, 218); thanks.setvisible(false); window.getcontentpane().add(thanks); jlabel lblstagetwogif = new jlabel("stage 2 gif"); lblstagetwogif.setbounds(236, 95, 160, 120); window.getcontentpane().add(lblstagetwogif); jlabel stagetwoimage = new jlabel("stage2image"); stagetwoimage.seticon(new imageicon("c:\\users\\dell\\desktop\\stagetwoimage.jpg")); stagetwoimage.setbounds(236, 95, 160, 120); window.getcontentpane().add(stagetwoimage); jlabel stage3gif = new jlabel("stage3gif"); stage3gif.setbounds(395, 95, 160, 120); window.getcontentpane().add(stage3gif); jlabel stage3image = new jlabel("stageimage3"); stage3image.seticon(new imageicon("c:\\users\\dell\\desktop\\stagethree.jpg")); stage3image.setbounds(395, 95, 160, 120); window.getcontentpane().add(stage3image); jlabel stagefourimage = new jlabel("stagefourimage"); stagefourimage.seticon(new imageicon("c:\\users\\dell\\desktop\\stagefour.jpg")); stagefourimage.setbounds(555, 95, 160, 120); window.getcontentpane().add(stagefourimage); jlabel stagefourgif = new jlabel("stagefourgif"); stagefourgif.setbounds(555, 95, 160, 120); window.getcontentpane().add(stagefourgif); jlabel stagefiveimage = new jlabel("stagefiveimage"); stagefiveimage.seticon(new imageicon("c:\\users\\dell\\desktop\\stage 5image.jpg")); stagefiveimage.setbounds(715, 95, 160, 120); window.getcontentpane().add(stagefiveimage); jlabel stagefivegif = new jlabel("stagefivegif"); stagefivegif.setbounds(715, 95, 160, 120); window.getcontentpane().add(stagefivegif); // populate drop-down box serialport[] portnames = serialport.getcommports(); for(int = 0; < portnames.length; i++) portlist.additem(portnames[i].getsystemportname()); // configure connect button , use thread listen data connectbutton.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { if(connectbutton.gettext().equals("connect")) { // attempt connect serial port chosenport = serialport.getcommport(portlist.getselecteditem().tostring()); chosenport.setcomporttimeouts(serialport.timeout_scanner, 0, 0); if(chosenport.openport()) { connectbutton.settext("disconnect"); portlist.setenabled(false); } thread thread = new thread(){ @override public void run() { // wait after connecting, bootloader can finish try {thread.sleep(100); } catch(exception e) {} // enter infinite loop sends text arduino printwriter output = new printwriter(chosenport.getoutputstream()); while(true) { output.println("y"); output.flush(); try {thread.sleep(100); } catch(exception e) {} } } }; thread.start(); // create new thread listens incoming text , populates graph thread thread2 = new thread(){ @override public void run() { int count = 0; int total_income = 0; int car_price=300; scanner scanner = new scanner(chosenport.getinputstream()); while(scanner.hasnextline()) { try { string line = scanner.nextline(); int number = integer.parseint(line); system.out.println(number); if(number==1) { count++; stageone.setvisible(true); stageoneimage.setvisible(false); total_income=count*car_price; out.println("date= "+dateandtime+" number of cars="+count+" total income="+total_income); totalcars.settext(string.valueof(count)); totalincome.settext(string.valueof( total_income)); thanks.setvisible(true); } } catch(exception e) {} } scanner.close(); } }; thread2.start(); } else { // disconnect serial port chosenport.closeport(); portlist.setenabled(true); connectbutton.settext("connect"); } } }); // show window window.setvisible(true); } }
i sovled problem changing output.println("y"); output.write("y"); , worked chorm response
Comments
Post a Comment