Java GUI подключиться к Arduino
В настоящее время я пытаюсь сделать телеметрическую систему для автомобиля. Я изо всех сил пытаюсь подключить термометр к Arduino. Я знаю, что мне нужно изменить значение при создании нового термометра, но как я могу подключить его через последовательный канал? Вот мой код
Кроме того, график был подключен и работал отлично, пока я не переместил его в отдельную функцию. Есть идеи, почему это произошло?
public class Comm {
public static SerialPort chosenPort;
private static int x=0;
static JFrame window = new JFrame();
static JTabbedPane tabbedPane = new JTabbedPane();
static JComboBox<String> portList = new JComboBox<String>();
static JButton connectButton = new JButton("Connect");
static XYSeries series1 = new XYSeries("Speed");
static XYSeries series2 = new XYSeries("Another Graph");
static XYSeries series3 = new XYSeries("Another jfjv Graph");
static final JPanel panel2 = new JPanel();
static JFreeChart chart1;
static final JPanel panel1 = new JPanel();
public static void main(String[] args)
{
//Sets the default window operations
window.setTitle("Telemetry");
window.setSize(1940, 1030);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new GridLayout(1, 1));
connectButton();
getPorts();
//Creates a new Graph
SensorGraph graph1 = new SensorGraph();
inputStream();
//Creates a thermometer
newThermometer("Temperature", 23.5);
newThermometer("Velocity", 75.4);
window.setVisible(true);
window.getContentPane().add(tabbedPane);
}
public static void connectButton()
{
//Creates the drop down box and connect button
JPanel topPanel = new JPanel();
topPanel.add(portList);
topPanel.add(connectButton);
tabbedPane.add(topPanel, BorderLayout.SOUTH);
}
public static void getPorts()
{
//Gets all available port names and connects
SerialPort[] ports = SerialPort.getCommPorts();
for(int i=0; i<ports.length; i++)
{
portList.addItem(ports[i].getSystemPortName());
}
}
public static void inputStream() {
//Starts input stream
connectButton.addActionListener(new ActionListener() { //makes the connect button have an action
@Override public void actionPerformed(ActionEvent arg0) {
if(connectButton.getText().equals("Connect")) { //if the user presses the connect button
//attempt to connect to serial port
chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString()); //registers the chosen port
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()) {
connectButton.setText("Disconnect"); //if the port is successfully opened, the connect button now reads disconnect
portList.setEnabled(false); //cant choose another port now
//create a new thread for the speed graph that listens for incomming data
Thread thread1 = new Thread() {
@Override public void run() {
Scanner scanner1 = new Scanner(chosenPort.getInputStream());
while(scanner1.hasNextLine()) {
try {
String line = scanner1.nextLine();
int number = Integer.parseInt(line);
series1.add(x++, 1023 - number);
window.repaint();
} catch (Exception e) {}
} scanner1.close();
}
};
thread1.start();
Thread thread3 = new Thread() {
@Override public void run() {
Scanner scanner3 = new Scanner(chosenPort.getInputStream());
while(scanner3.hasNextLine()) {
try {
} catch (Exception e) {}
} scanner3.close();
}
}; thread3.start();
}
} else {
//disconnect from serial port
chosenPort.closePort();
portList.setEnabled(true); //can access the port list
connectButton.setText("Connect");
series1.clear();
x=0;
}
}
});
}
public static void newGraph(String graphName, String xAxis, String yAxis)
{
//Creates Speed graph
XYSeries series1 = new XYSeries(graphName);
XYSeriesCollection dataset1 = new XYSeriesCollection(series1);
JFreeChart chart1 = ChartFactory.createXYLineChart(
graphName, //name of graph
xAxis, //x axis
yAxis, //y axis
dataset1, //new graph that was just created
PlotOrientation.VERTICAL,
true, //legend
false, //tooltips
false); //url
series1.setKey("Current " + graphName);
dataset1.addSeries(series1);
panel1.add(new ChartPanel(chart1), BorderLayout.NORTH);
tabbedPane.add(panel1, "Graphs");
}
public static void newThermometer(String name, double value)
{
int W = 200;
int H = 2 * W;
DefaultValueDataset dataset = new DefaultValueDataset(value);
ThermometerPlot plot1 = new ThermometerPlot(dataset);
plot1.setUnits(ThermometerPlot.UNITS_CELCIUS);
chart1 = new JFreeChart(
name, // chart title
JFreeChart.DEFAULT_TITLE_FONT,
plot1, // plot
true);
plot1.setMercuryPaint(Color.RED);
plot1.setSubrangePaint(0, Color.GREEN);
plot1.setSubrangePaint(1, Color.orange);
plot1.setSubrangePaint(2, Color.red.darker());
panel2.add(new ChartPanel(chart1, W, H, W, H, W, H, false, true, true, true, true, true));
tabbedPane.add(panel2, "Gauges");
}
}