PortReader

Aus Programmers Guide

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „= PortReader = <source lang="java"> import java.io.IOException; import java.io.InputStream; import javax.comm.CommPortIdentifier; import javax.comm.PortInUseExc…“)
Zeile 1: Zeile 1:
-
= PortReader =
+
= PortReader =
 +
 
<source lang="java">
<source lang="java">
Zeile 84: Zeile 85:
}
}
</source>
</source>
 +
 +
[[Category:Com]][[Category:Java]][[Category:Klassen]][[Category:Snippet]]

Version vom 10:47, 25. Aug. 2010

PortReader

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 static Enumeration ports;
 
  //static CommPortIdentifier pID;
 
  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;
}
 
}
Persönliche Werkzeuge