import java.io.*;
import javax.comm.*;
import java.awt.*;
import java.awt.event.*;
public class comframe extends Frame implements Serializable{
static CommPortIdentifier portID;
InputStream ins;
OutputStream out;
static SerialPort serss;
TextArea ausgabe;
TextField tfe;
Button bt;
public static void main(String[] args){
try{portID = CommPortIdentifier.getPortIdentifier("COM1");
serss = (SerialPort) portID.open("mein_programm",2000);
comframe f = new comframe();
f.setSize(330,250);
f.show();
}
catch(Exception exc){System.out.println("Fehler :"+exc);}
}
public comframe() {
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
serss.close();
System.exit(0);
}
});
setLayout(new FlowLayout());
add(ausgabe=new TextArea("Port COM1 geöffnet\n",7,40));
try {ins = new BufferedInputStream(serss.getInputStream());
out = new BufferedOutputStream(serss.getOutputStream());
serss.addEventListener(new commListener());
}
catch (Exception e) { System.out.println("Fehler: "+e);}
serss.notifyOnDataAvailable(true);
try {serss.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1 ,
SerialPort.PARITY_NONE);
serss.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serss.sendBreak(2000); // Reset des Gerätes
PrintWriter send = new PrintWriter(out, true);
byte[] readBuffer = new byte[20];
try{
send.println("INIT ");
Thread.sleep(400);
send.println("PINIT 1");
Thread.sleep(400);
System.out.println("PINIT");
send.println("PENA 1D");
Thread.sleep(400);
System.out.println("PENA");
send.println("BEEP 3");
Thread.sleep(400);
System.out.println("BEEP");
send.println("TSTART:5423");
System.out.println("TSTART");
Thread.sleep(4000);
send.println("TSTOP");
System.out.println("TSTOP");
send.close();
}catch(InterruptedException exe){
exe.printStackTrace();
serss.close();
System.exit(-1);
}
send.close();
}
catch (UnsupportedCommOperationException e) {}
}
public class commListener implements SerialPortEventListener{
public void serialEvent(SerialPortEvent event) {
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
byte[] readBuffer = new byte[20];
try {
while (ins.available() > 0) {int numBytes = ins.read(readBuffer);}
String nachricht = new String(readBuffer);
ausgabe.append(nachricht);
}
catch (IOException e) {System.out.println("Fehler: "+e);}
}
}
}
}