ReaderWindow

Aus Programmers Guide

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „= ReaderWindow = <source lang="java"> import java.awt.Button; import java.awt.DisplayMode; import java.awt.Frame; import java.awt.GraphicsConfiguration; import…“)
Zeile 13: Zeile 13:
import java.awt.event.WindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowListener;
-
import java.io.InputStream;
+
import java.text.SimpleDateFormat;
-
import java.util.Enumeration;
+
import java.util.Calendar;
import javax.comm.CommPortIdentifier;
import javax.comm.CommPortIdentifier;
-
import javax.comm.SerialPort;
 
 +
//erweitert thread, somit können wir in der klasse einen neuen thread starten
 +
public class ReaderWindow extends Thread{
-
public class ReaderWindow {
+
//variablendeklarationen
-
 
+
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
-
 
+
private static Button b1;
-
private Button b1;
+
private static TextArea ta;
private static TextArea ta;
-
@SuppressWarnings("unchecked")
+
private static Frame frame;
-
static Enumeration ports;
+
private static CommPortIdentifier cid=null;
-
static CommPortIdentifier pID;
+
private static Thread readThread;
-
InputStream inStream;
+
 
-
SerialPort serPort;
+
public ReaderWindow (CommPortIdentifier ci)
-
private Frame f;
+
{
-
public ReaderWindow (CommPortIdentifier ci){
+
//den identifier global zur verfügung stellen
-
f = new Frame("PortReader");
+
cid=ci;
-
f.addWindowListener(new WindowListener(){
+
 
-
public void windowClosing(WindowEvent e){
+
/*
-
f.dispose();
+
* Fensterschnulli
 +
*/
 +
frame = new Frame("PortReader");
 +
//was passiert wenn das Fenster irgendwie geschlossen wird
 +
frame.addWindowListener(new WindowListener(){
 +
public void windowClosing(WindowEvent e)
 +
{
 +
frame.dispose();
try{
try{
-
PortReader.getSerPort().close();
+
SimpleRead.getSerialPort().close();
}catch(Exception ex){}
}catch(Exception ex){}
System.exit(0);
System.exit(0);
Zeile 56: Zeile 62:
});
});
 
 
-
f.setLayout(null); // "Null-Layout" setzen
+
frame.setLayout(null); // "Null-Layout" setzen
ta = new TextArea();
ta = new TextArea();
ta.setBounds(0, 0, 299, 120); // x, y, breite, höhe
ta.setBounds(0, 0, 299, 120); // x, y, breite, höhe
 +
b1 = new Button();
b1 = new Button();
Zeile 67: Zeile 74:
b1.addActionListener(buttonListener);
b1.addActionListener(buttonListener);
 
 
-
f.add(ta);
+
frame.add(ta);
-
f.add(b1);
+
 +
frame.add(b1);
 
 
DisplayMode dMode = null;
DisplayMode dMode = null;
-
GraphicsConfiguration gc = f.getGraphicsConfiguration();
+
 +
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
GraphicsDevice gd = gc.getDevice();
GraphicsDevice gd = gc.getDevice();
dMode = gd.getDisplayMode();
dMode = gd.getDisplayMode();
 
 
-
int width = dMode.getWidth() / 2 - 150;
+
int width = dMode.getWidth()/2-150;
-
int height = dMode.getHeight() / 2 - 77;
+
int height = dMode.getHeight()/2-77;
-
+
frame.setLocation(width,height);
-
f.setLocation(width,height);
+
frame.setSize(300,150);
-
f.setSize(300,150);
+
frame.setResizable(false);
-
f.setResizable(false);
+
frame.setUndecorated(true);
-
f.setUndecorated(true);
+
frame.setVisible(true);
-
f.setVisible(true);
+
/*
-
try {
+
* ENDE Fensterschnulli
-
//new PortReader(ci);
+
*/
-
} catch (Exception e) {
+
//hier wird der thread gestartet (unten bei run() gehts weiter :))
-
e.printStackTrace();
+
readThread = new Thread(this);
-
}
+
readThread.start();
}
}
-
private ActionListener buttonListener = new ActionListener() {
+
//was passiert wenn wir "schließen" drücken
 +
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
public void actionPerformed(ActionEvent event) {
-
f.dispose();
+
//fenster zerstören
 +
frame.dispose();
try{
try{
-
PortReader.getSerPort().close();
+
//versuchen den port zu schließen
 +
SimpleRead.getSerialPort().close();
}catch(Exception ex){}
}catch(Exception ex){}
System.exit(0);
System.exit(0);
}
}
};
};
 +
//wir brauchen einen neuen Thread, sonst reagiert nix mehr. hier wird er gestartet
 +
public void run(){
 +
//Zeiterzeugung
 +
Calendar cal = Calendar.getInstance();
 +
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss\n");
 +
ReaderWindow.getTa().append("Programmstart um: "+sdf.format(cal.getTime()));
 +
//ENDE Zeiterzeugung
 +
try {
 +
//versuchen den reader zu starten
 +
new SimpleRead(cid);
 +
} catch (Exception e) {
 +
e.printStackTrace();
 +
}
 +
}
 +
/*
 +
* GETTERS AND SETTERS
 +
* also wir geben anderen klassen die möglichkeit objekte in der klasse zu verändern
 +
*/
public static TextArea getTa() {
public static TextArea getTa() {
return ta;
return ta;
Zeile 109: Zeile 139:
</source>  
</source>  
-
= PortReader =
+
[[Category:Java]][[Category:Klassen]]
-
 
+
-
<source lang="java">
+
-
 
+
-
import java.io.IOException;
+
-
import java.io.InputStream;
+
-
 
+
-
import javax.comm.CommPortIdentifier;
+
-
import javax.comm.PortInUseException;
+
-
import javax.comm.SerialPort;
+
-
import javax.comm.SerialPortEvent;
+
-
import javax.comm.SerialPortEventListener;
+
-
public class PortReader implements SerialPortEventListener {
+
-
  //@SuppressWarnings("unchecked")
+
-
  private InputStream inStream;
+
-
  private static SerialPort serPort=null;
+
-
  public PortReader(CommPortIdentifier pID) throws Exception{
+
-
  ReaderWindow.getTa().append("Lausche auf Port:"+pID.getName()+"\n");
+
-
  try{
+
-
    serPort = (SerialPort) pID.open("PortReader", 2000);
+
-
    inStream = serPort.getInputStream();
+
-
    serPort.addEventListener(this);
+
-
    serPort.notifyOnDataAvailable(true);
+
-
    serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+
-
  }catch(PortInUseException piu){
+
-
  ReaderWindow.getTa().append("Der Port ist in Benutzung\n");
+
-
  }
+
-
  }
+
-
 
+
-
  public void serialEvent(SerialPortEvent event) {
+
-
    switch (event.getEventType()) {
+
-
    case SerialPortEvent.BI:
+
-
      ReaderWindow.getTa().append("SerialPortEvent.BI occurred\n");
+
-
    case SerialPortEvent.OE:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.OE occurred\n");
+
-
    case SerialPortEvent.FE:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.FE occurred\n");
+
-
    case SerialPortEvent.PE:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.PE occurred\n");
+
-
    case SerialPortEvent.CD:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.CD occurred\n");
+
-
    case SerialPortEvent.CTS:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.CTS occurred\n");
+
-
    case SerialPortEvent.DSR:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.DSR occurred\n");
+
-
    case SerialPortEvent.RI:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.RI occurred\n");
+
-
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.OUTPUT_BUFFER_EMPTY occurred\n");
+
-
      break;
+
-
    case SerialPortEvent.DATA_AVAILABLE:
+
-
    ReaderWindow.getTa().append("SerialPortEvent.DATA_AVAILABLE occurred\n");
+
-
      byte[] readBuffer = new byte[20];
+
-
 
+
-
      try {
+
-
        while (inStream.available() > 0) {
+
-
          int numBytes = inStream.read(readBuffer);
+
-
          ReaderWindow.getTa().append("Bytes: "+numBytes+"\n");
+
-
        }
+
-
        ReaderWindow.getTa().append(new String(readBuffer));
+
-
      } catch (IOException ioe) {
+
-
      ReaderWindow.getTa().append("Exception " + ioe+"\n");
+
-
      }
+
-
      break;
+
-
    }
+
-
  }
+
-
 
+
-
public static SerialPort getSerPort() {
+
-
return serPort;
+
-
}
+
-
 
+
-
public void setSerPort(SerialPort serPort) {
+
-
PortReader.serPort = serPort;
+
-
}
+
-
 
+
-
}
+
-
</source>
+

Version vom 10:27, 25. Aug. 2010

ReaderWindow

import java.awt.Button;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
import javax.comm.CommPortIdentifier;
 
//erweitert thread, somit können wir in der klasse einen neuen thread starten
public class ReaderWindow extends Thread{
 
//variablendeklarationen
private static final long serialVersionUID = 1L;
private static Button b1;
private static TextArea ta;
private static Frame frame;
private static CommPortIdentifier cid=null;
private static Thread readThread;
 
public ReaderWindow (CommPortIdentifier ci)
{
//den identifier global zur verfügung stellen
cid=ci;
 
/*
 * Fensterschnulli
 */
	frame = new Frame("PortReader");
	//was passiert wenn das Fenster irgendwie geschlossen wird
	frame.addWindowListener(new WindowListener(){
		public void windowClosing(WindowEvent e)
		{
			frame.dispose();
			try{
				SimpleRead.getSerialPort().close();
			}catch(Exception ex){}	
			System.exit(0);
		}
		@Override
		public void windowActivated(WindowEvent arg0) {}
		@Override
		public void windowClosed(WindowEvent arg0) {}
		@Override
		public void windowDeactivated(WindowEvent arg0) {}
		@Override
		public void windowDeiconified(WindowEvent arg0) {}
		@Override
		public void windowIconified(WindowEvent arg0) {}
		@Override
		public void windowOpened(WindowEvent arg0) {}
	});
 
	frame.setLayout(null); // "Null-Layout" setzen
 
	ta = new TextArea();
	ta.setBounds(0, 0, 299, 120); // x, y, breite, höhe
 
 
	b1 = new Button();
	b1.setBounds(110,125,80,20);
	b1.setLabel("schliessen");
	b1.setName("Fenster schliessen");
	b1.addActionListener(buttonListener);
 
	frame.add(ta);
 
	frame.add(b1);
 
	DisplayMode dMode = null;
 
	GraphicsConfiguration gc = frame.getGraphicsConfiguration();
	GraphicsDevice gd = gc.getDevice();
	dMode = gd.getDisplayMode();
 
	int width = dMode.getWidth()/2-150;
	int height = dMode.getHeight()/2-77;
	frame.setLocation(width,height);
	frame.setSize(300,150);
	frame.setResizable(false);
	frame.setUndecorated(true);
	frame.setVisible(true);
	/*
	 * ENDE Fensterschnulli
	 */
	//hier wird der thread gestartet (unten bei run() gehts weiter :))
	readThread = new Thread(this);
	readThread.start();
}
//was passiert wenn wir "schließen" drücken
ActionListener buttonListener = new ActionListener() {
	public void actionPerformed(ActionEvent event) {
		//fenster zerstören
		frame.dispose();
		try{
			//versuchen den port zu schließen
			SimpleRead.getSerialPort().close();
		}catch(Exception ex){}
		System.exit(0);
	}
};
//wir brauchen einen neuen Thread, sonst reagiert nix mehr. hier wird er gestartet
public void run(){
	//Zeiterzeugung
	Calendar cal = Calendar.getInstance();
	SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss\n");
	ReaderWindow.getTa().append("Programmstart um: "+sdf.format(cal.getTime()));
	//ENDE Zeiterzeugung
	try {
		//versuchen den reader zu starten
		new SimpleRead(cid);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
/*
 * GETTERS AND SETTERS
 * also wir geben anderen klassen die möglichkeit objekte in der klasse zu verändern
 */
public static TextArea getTa() {
	return ta;
}
public static void setTa(TextArea ta) {
	ReaderWindow.ta = ta;
}
 
}
Persönliche Werkzeuge