java - 将多个 Arduino 传感器值数据绘制到 Java GUI 中
问题描述
我正在做一个完整的 Arduino 项目。下一阶段是让我将传感器值打印到 GUI 上。我正在使用 JFreeChart 来执行此操作。我设法绘制了一个传感器读数,但是我无法绘制另外两个。我设法绘制的唯一传感器读数是 LDR,但是,我相信它将它们全部绘制在一条线上,但我希望这是分开的。因此,由于总共有三个传感器,我希望折线图上有三条单独的线。我正在努力添加系列和数据集以使其正常工作。我还设法创建了单独的图表,但它们都输出相同的结果,但这不应该是它们应该输出不同结果的情况。这是我的代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
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.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import com.fazecast.jSerialComm.SerialPort;
public class SensorGraph {
static SerialPort chosenPort;
static int x = 0;
public static void main(String[] args) {
// create and configure the window
JFrame window = new JFrame();
window.setTitle("Sensor Graph GUI");
window.setSize(600, 400);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a drop-down box and connect button, then place them at the top of the window
JComboBox<String> portList = new JComboBox<String>();
JButton connectButton = new JButton("Connect");
JPanel topPanel = new JPanel();
topPanel.add(portList);
topPanel.add(connectButton);
window.add(topPanel, BorderLayout.NORTH);
// populate the drop-down box
SerialPort[] portNames = SerialPort.getCommPorts();
for(int i = 0; i < portNames.length; i++)
portList.addItem(portNames[i].getSystemPortName());
// create the line graph
XYSeries series = new XYSeries("Light Sensor Readings");
XYSeries series2 = new XYSeries("Pressure Pad Readings");
XYSeries series3 = new XYSeries("Ultrasonic Sensor Readings");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("Light Sensor Readings", "Time (seconds)", "ADC Reading", dataset);
window.add(new ChartPanel(chart), BorderLayout.CENTER);
// configure the connect button and use another thread to listen for data
connectButton.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0) {
if(connectButton.getText().equals("Connect")) {
// attempt to connect to the serial port
chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()) {
connectButton.setText("Disconnect");
portList.setEnabled(false);
}
// create a new thread that listens for incoming text and populates the graph
Thread thread = new Thread(){
@Override public void run() {
Scanner scanner = new Scanner(chosenPort.getInputStream());
while(scanner.hasNextLine()) {
try {
String line = scanner.nextLine();
int number = Integer.parseInt(line);
series.add(x++, 1023 - number);
window.repaint();
} catch(Exception e) {}
}
scanner.close();
}
};
thread.start();
} else {
// disconnect from the serial port
chosenPort.closePort();
portList.setEnabled(true);
connectButton.setText("Connect");
series.clear();
x = 0;
}
}
});
// show the window
window.setVisible(true);
}
}
解决方案
推荐阅读
- java - Java 实例变量设置为 0
- python - Django 表单错误:
- 语
- 输入值列表。
- 语
- dialogflow-es - 如何在 dialogFlow 中构建一系列是/否问题?
- node.js - 在 .env 文件中存储私有值是否安全?
- java - 使用递归方法在java中打印文本
- java - facing error while unmarshaling from sml to java
- sql - SQL在同一张表上选择两个相同的列但具有不同的ORDER
- angular - After upgrading to Angular 9, problem with the command "ng lint --fix"
- asp.net-web-api - Web API Clear Button
- python - After Add New model.How to Django Table Migrate without losing data