如何结合Java的情节code和Java串行code阅读Arduino的传感器值? [英] How to combine Java plot code and Java serial code to read Arduino sensor value?
问题描述
进展:code我使用的可以在这里找到:的https:/ /dl.dropbox.com/u/2108381/MyArduinoPlot.java
我希望这有助于理解我的挑战。在此先感谢您的时间。
UPDATE: The code I am using can be found here: https://dl.dropbox.com/u/2108381/MyArduinoPlot.java I hope this helps to understand my challenge. Thanks in advance for your time.
我想从一个Arduino读出传感器值和使用Java库的JFreeChart图表它们。
I would like to read sensor values from an Arduino and chart them using the Java library JFreeChart.
我发现在互联网上的一些code(见下文),现在,我想在code结合起来,绘制一个动态折线图和code在Arduino的值来读取。无论codeS分别做工作,但我被困在结合他们两个。
I found some code on the internet (see below) and now, I would like to combine the code for plotting a dynamic line chart and the code for reading in the Arduino values. Both codes do work separately, but I got stuck in combining them both.
在code密谋动态折线图就是从这里开始(图随机数据):
的http://dirtyhandsphp.blogspot.in/2012/07/how-to-draw-dynamic-line-or-timeseries.html
The code for plotting a dynamic line chart is from here (plots random data): http://dirtyhandsphp.blogspot.in/2012/07/how-to-draw-dynamic-line-or-timeseries.html
在code在Java中的Arduino读取值就是从这里开始:
http://arduino.cc/playground/Interfacing/Java
The code to read in Arduino values in Java is from here: http://arduino.cc/playground/Interfacing/Java
我认为(在Java中的新手,虽然)的相关部分是在这里:
I assume (newbie in Java, though) that the relevant part is here:
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
// the code I tried
// String MyValue = new String(chunk);
// Double Value = Double.valueOf(MyValue);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
在这里:
public void actionPerformed(final ActionEvent e) {
// Original Code
final double factor = 0.9 + 0.2*Math.random();
this.lastValue = this.lastValue * factor;
final Millisecond now = new Millisecond();
this.series.add(new Millisecond(), this.lastValue);
System.out.println("Current Time in Milliseconds = " + now.toString()+", Current Value : "+this.lastValue);
// my code
// this.series.add(new Millisecond(), Value);
}
我怎样才能让公共同步无效的serialEvent
返回传感器值以及如何将它添加到 this.series.add
部分?
How can I make the public synchronized void serialEvent
return the sensor value and how can I add it to the this.series.add
part?
我在Java中的新手。
I am a newbie in Java.
任何直接的帮助或联系到其他网站/职位大大AP preciated。感谢您的时间。
Any direct help or linkage to other websites/posts is greatly appreciated. Thanks for your time.
推荐答案
在此例如,一个 javax.swing.Timer中的
定期增加了一个新值集
在定时器中的的ActionListener
。你会想从外面去做。下面是如何进行的大纲:
In this example, a javax.swing.Timer
periodically adds a new value to the dataset
in the timer's ActionListener
. You'll want to do it from outside. Here's an outline of how to proceed:
-
移动
集
和newData code>起来,成为的 的实例变量的:
Move
dataset
andnewData
up to become instance variables:
DynamicTimeSeriesCollection dataset;
float[] newData = new float[1];
添加了附加数据以图表的集
方法:
public synchronized void addData(byte[] chunk) {
for (int i = 0; i < chunk.length; i++) {
newData[0] = chunk[i];
dataset.advanceTime();
dataset.appendData(newData);
}
}
从调用方法的serialEvent()
:
demo.addChunk(chunk);
这篇关于如何结合Java的情节code和Java串行code阅读Arduino的传感器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!