RS232 Problem mit javax.comm

ram2000

Grünschnabel
Hallo Zusammen,
ich schreibe derzeit ein Programm, dass über eine serielle Schnittstelle Cisco-Router konfigurieren soll. Der DataoutputStream funktioniert ohne Probleme. Beim auslesen der Schnittstelle endet mein Programm immer in einer Endlosschleife. [ public void write2Router(String[] commandList) ]. Der Actionlistener reagiert nicht richtig. Wäre super dankbar für eure Hilfe. Verzweifel langsam.

public class NConsole implements SerialPortEventListener {

public DataOutputStream dataOutputStream;
public BufferedInputStream bufferedInputStream;
public SerialPort serialPort;

public NConsole ()
{
try {
CommPortIdentifier comport = CommPortIdentifier.getPortIdentifier("COM1");
if ((comport != null) && ( comport.getPortType() == CommPortIdentifier.PORT_SERIAL) ) {
System.out.println("öffne "+comport.getName());
serialPort = (SerialPort) comport.open("Meine RS232", 30000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

//serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);

dataOutputStream = new DataOutputStream(serialPort.getOutputStream());
bufferedInputStream = new BufferedInputStream(serialPort.getInputStream());
}


} catch (Exception e) {
System.err.println(e);
}
}


public void serialEvent(SerialPortEvent serialPortEvent)
{ // reagiert nicht zuverlässig
System.out.println("da");
if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int len = bufferedInputStream.available();
if ( len > 0) {
byte [] buf = new byte [len];
System.out.println(new String(buf));
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

}

public void write2Router(String[] commandList) {
char c;
try {

for(String str : commandList){
dataOutputStream.writeBytes (str);
System.out.println("aktueller Routerbefehl: "+str);
dataOutputStream.flush();
}
/* for (int i=0; i<15; i++) {
System.out.println(String.valueOf(bufferedInputStream.);
} */
while ( (c=(char) bufferedInputStream.read())>0) {

System.out.print(c);
}

dataOutputStream.close();
bufferedInputStream.close();
serialPort.close();

} catch (IOException e) {
e.printStackTrace();
}

}
}
 
ich benutze javax.comm und habe rtx auch ausprobiert. beides endet in einer endlosschleife...
muss wohl irgendeinen bug im code haben:confused:
 
Zurück