gpt4 book ai didi

java - DynamicTimeSeriesCollection 和串行通信

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:15 25 4
gpt4 key购买 nike

我正在使用 DynamicTimeSeriesCollection 将串行端口的测量结果绘制到我的图表中。我想使用我在另一篇文章中找到的代码。

我想要实现这样的目标: Using JFreeChart to display recent changes in a time series

我发现的另一篇文章中的代码:

How to combine Java plot code and Java serial code to read Arduino sensor value?

1. Move dataset and newData up to become instance variables:

DynamicTimeSeriesCollection dataset;
float[] newData = new float[1];

2. Add a method that appends your data to the chart's dataset:

public synchronized void addData(byte[] chunk) {
for (int i = 0; i < chunk.length; i++) {
newData[0] = chunk[i];
dataset.advanceTime();
dataset.appendData(newData);
}
}

3. Invoke the method from serialEvent():

demo.addChunk(chunk);

这是我在 SerialCommunication 类中的串行事件方法:

public void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try
{
Date d = new Date();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String datetime = dt.format(d);
String inputLine = input.readLine();

String[] measurements = inputLine.split(",");
double humidity = Double.parseDouble(measurements[0]);
double temp = Double.parseDouble(measurements[1]);
double ldr = Double.parseDouble(measurements[2]);
humidityList.add(humidity);
tempList.add(temp);
ldrList.add(ldr);

System.out.println(datetime+" > "+inputLine);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}

以及串行通信类中的ArrayList:

private ArrayList<Double> humidityList = new ArrayList<Double>();
private ArrayList<Double> tempList = new ArrayList<Double>();
private ArrayList<Double> ldrList = new ArrayList<Double>();

这是我在 GUI 类中声明 ArrayList 的方法:

private static ArrayList<Double> m1;
private static ArrayList<Double> m2;
private static ArrayList<Double> m3;


public GUI(final String title,ArrayList<Double> humidity,ArrayList<Double> temp,ArrayList<Double> ldr)
{

super(title);
m1 = humidity;
m2 = temp;
m3 = ldr;

我确实尝试修改通过测量发现的代码,但没有成功。知道如何实现这一目标吗?

最佳答案

SwingWorker 是处理这个问题的最佳方法。

  • 创建一个类 Measurement包含三个Double属性。

  • 延长SwingWorker<Measurement, Measurement>

  • doInBackground() 的实现中, publish()每个Measurement当它到达时。

  • process() 的实现中,按照建议更新图表的数据集 herehere .

关于java - DynamicTimeSeriesCollection 和串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23395295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com