import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Dataset;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.awt.*;
import java.awt.event.*;
public class TempBefehl extends ApplicationFrame implements ActionListener //SerialPortEventListener
{
static CommPortIdentifier portID;
static SerialPort serialport;
static InputStream ins;
static OutputStream out;
static double temp = 0;
static double winkel = 0;
static PrintWriter aus ;
String nachricht= null;
final JButton button1 = new JButton("Add To Series 1");
final static XYSeries series = new XYSeries("");
static XYSeriesCollection dataset = new XYSeriesCollection(series);;
Thread t;
public TempBefehl(String title)//(String port, String title)
{
super(title);
final JFreeChart chart = ChartFactory.createXYLineChart(
"Diagramm",
"Winkel",
"Temperatur",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
chartPanel.setMouseZoomable(true, false);
setContentPane(chartPanel);
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);// x axe wird gefüllt
axis.setRange(0,90);
final NumberAxis rangeAxis2 = (NumberAxis) plot.getRangeAxis();
// rangeAxis2.setAutoRangeIncludesZero(false);
rangeAxis2.setRange(100,200);
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
content.add(chartPanel);
button1.setActionCommand("ADD_DATA_1");
button1.addActionListener(this);
final JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(button1);
content.add(buttonPanel, BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(content);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
t = new Thread(new Nebenlaufen());
while(true)
{
synchronized(t)
{
t.run();
try
{
t.sleep(1000);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}
//}
/*int x = 0X02;
char stx = (char)x;
System.out.println(stx);
aus = new PrintWriter(out);
aus.println(stx);
aus.flush();
aus.close();*/
/* synchronized(t)
{
try
{
t.sleep(2000);//1000 millisekunde = 1 sekunde
//t.notify();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
//close();
}
// t.stop();
}*/
//}
}
//}
/* public class commListener implements SerialPortEventListener
{
public void serialEvent(SerialPortEvent event)
{
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
byte[] readBuffer = new byte[256];
try
{
//while (ins.available() > 0)
if (ins.read(readBuffer)!= readBuffer.length)
{
int numBytes = ins.read(readBuffer);
byte zeichen;
for (int i=2 ;i<readBuffer.length; i++)
{
zeichen = (readBuffer[i]);
//series.toArray();
//double Arr [][] = new double[256][2];
winkel = (90/255.0)*(i-2);
temp = byteToInt(zeichen)/255.0 * (200-100) + 100;
//Arr[j][0]=temp;
//Arr[j][1]=winkel;
System.out.println(winkel+" "+temp+"\n");
series.add(winkel,temp);
//}
}
series.clear();
}
}
catch (IOException e)
{
System.out.println("Fehler: " + e);
}
}
}
}
public static int byteToInt(byte b)
{
return (b & 0x00ff);
}
public void close()
{
try
{
ins.close();
out.close();
}
catch (IOException e)
{}
serialport.close();
}
/* public static void zeichnen()
{
TempBefehl demo = new TempBefehl("");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}*/
}