This article about how communication between Arduino and Java via USB. Due to “Arduino Playground” http://playground.arduino.cc/Interfacing/Java that describes how to communicate between Java and Arduino via USB port. This article is adapted to an osscilloscope.
Arduino
In the example, Arduino Uno to generate 25% PWM wave on PIN3 and then connects PIN3 and A0 together. In the loop() function, read an analog value from A0 and send thru USB by Serial.println() function.void setup() { Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } pinMode(3,OUTPUT); //PWM 25% duty cycle on pin 3 analogWrite(3,64); } void loop() { int in = analogRead(A0); Serial.println(in); }
Java
The Java project “Simple oscilloscope” created by Netbean IDE and use JFreeChart library for creating graphs. The Simple oscilloscope implements SerialPortEventListener, when data is incoming on USB port the program call function serialEvent(SerialPortEvent oEvent). In serialEvent(SerialPortEvent oEvent) receive analog value and convert to volt and add to graph series.import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Enumeration; import javax.swing.JFrame; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.data.time.Millisecond; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.ui.ApplicationFrame; public class SimpleOscilloscope extends ApplicationFrame implements SerialPortEventListener { SerialPort serialPort; /** * The port we're normally going to use. */ private static final String PORT_NAMES[] = { "/dev/tty.usbserial-A9007UX1", // Mac OS X "/dev/ttyUSB0", // Linux "COM11", // Windows }; /** * Max supply voltage. */ private static final double MAX_SUPPLY_SOURCE = 5; /** * Max scale of analog precision */ private static final double MAX_AD_PRECISION = 1024; /** * A BufferedReader which will be fed by a InputStreamReader converting the * bytes into characters making the displayed results codepage independent */ private BufferedReader input; /** * The output stream to the port */ //private OutputStream output; /** * Milliseconds to block while waiting for port open */ private static final int TIME_OUT = 2000; /** * Default bits per second for COM port. */ // private static final int DATA_RATE = 57600; private static final int DATA_RATE = 115200; public SimpleOscilloscope(final String title) { super(title); this.series = new TimeSeries("PWM PIN3", Millisecond.class); final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); final JPanel content = new JPanel(new BorderLayout()); content.add(chartPanel); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(content); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent we) { System.out.println("close:"); close(); System.exit(0); } }); } public void initialize() { CommPortIdentifier portId = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); //First, Find an instance of serial port as set in PORT_NAMES. while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); for (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, and use class name for the 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 the 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()); } } /** * This should be called when you stop using the port. This will prevent * port locking on platforms like Linux. */ public synchronized void close() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } } /** * Handle an event on the serial port. Read the data and print it. */ @Override public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = input.readLine(); System.out.println(inputLine); double volt = new Double(inputLine) * MAX_SUPPLY_SOURCE / MAX_AD_PRECISION; addData(volt); } catch (Exception e) { System.err.println(e.toString()); } } } private TimeSeries series; private JFreeChart createChart(final XYDataset dataset) { final JFreeChart result = ChartFactory.createTimeSeriesChart( "Arduino Simple Osscilloscope", "Time", "Volt", dataset, true, true, false); final XYPlot plot = result.getXYPlot(); ValueAxis axis = plot.getDomainAxis(); axis.setAutoRange(true); axis.setFixedAutoRange(600.0); axis = plot.getRangeAxis(); axis.setRange(-1.0, 6.0); return result; } public void addData(double data) { this.series.add(new Millisecond(), data); } }
Download
- SimpleScope.ino Create on Arduino 1.0.1.
- SimpleOsscilloscope.zip Netbeans Java Application project on windows os.
Hello! Do you can update the project zip file??
ReplyDelete