我有一个程序,可以从Arduino发送的串行通信中接收温度传感器数据。我成功地在Y轴上绘制了温度,但现在我想在X轴上绘制来自RTC模块的输入数据,该RTC模块具有字符串格式,即";15:48";。
我发现我可以用series.add(int number,int number2)
在X轴上添加数值,但我不知道如何从传入的RTC值在X轴添加String值。(我真的需要使用RTC模块,因为我正在从Arduino向SD卡写入数据,并且值需要匹配(。
这是我正在使用的代码。
package gráfica.temperatura;
import com.fazecast.jSerialComm.SerialPort;
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.time.TimeSeries;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class GráficaTemperatura {
static SerialPort chosenPort;
static int x=0;
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Monitor de Temperatura F&CS Mexico");
window.setSize(600,400);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//CONEXIÓN SERIAL
JComboBox<String> portList = new JComboBox<String>();
JButton connectButton = new JButton("Conectar");
JPanel topPanel = new JPanel();
topPanel.add(portList);
topPanel.add(connectButton);
window.add(topPanel, BorderLayout.NORTH);
//AÑADE OPCIONES A LISTA DE CONEXION
SerialPort[] portNames = SerialPort.getCommPorts();
for(int i = 0; i < portNames.length; i++)
portList.addItem(portNames[i].getSystemPortName());
XYSeries series = new XYSeries("Temperatura");
XYSeries series2 = new XYSeries("Temperatura 2")
XYSeriesCollection dataset = new XYSeriesCollection(series);
dataset.addSeries(series2);
JFreeChart chart = ChartFactory.createXYLineChart("FCS Temp. Monitor", "Hora", "Temperatura °C", dataset);
window.add(new ChartPanel(chart), BorderLayout.CENTER);
//
//CONFIGURACION DE BOTONES
connectButton.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0){
if(connectButton.getText().equals("Conectar")){
//conectar serial
chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()){
connectButton.setText("Desconectado");
portList.setEnabled(false);
}
//RECIBIR DATOS
Thread thread = new Thread(){
@Override public void run(){
Scanner scanner = new Scanner(chosenPort.getInputStream());
while(scanner.hasNextLine()){
try{
String line = scanner.nextLine();
float number = Float.parseFloat(line);
String line2 =scanner.nextLine();
float number2 = Float.parseFloat(line2);
String tiempo =scanner.nextLine();
double tiemp = Double.parseDouble(tiempo);
series.add(x++, number);
series2.add(x++, number2);
window.repaint();
}catch(Exception e){}
}
scanner.close();
}
};
thread.start();
}else{
chosenPort.closePort();
portList.setEnabled(true);
connectButton.setText("Conectar");
}
}
});
window.setVisible(true);
}
}
我通过制作TimeSeries而不是XYSeries来解决问题,然后我只扫描来自arduino的传入时间字符串,并使用将其解析为Date
//READ THE INCOMING SENSOR VALUE
String line = scanner.nextLine();
double number = Double.parseDouble(line);
//READ THE INCOMING TIME STRING
String sDate1=scanner.nextLine();
Date date1=new SimpleDateFormat("HH:mm").parse(sDate1);
//ADD IT TO A TIME SERIES CHART
series.add(new Minute(date1), number);