Ciao a tutti.
Volevo testare due classi Java per scrivere e leggere dati attraverso la porta seriale.
Ho collegato 2 pc (Win XP) con cavo null modem alle porte seriali; su uno lancio la classe "ScriviSuSeriale" e sull'altro "LeggiDaSeriale".
Sembrano funzionare correttamente (non vengono catturate eccezioni), ma la classe che legge, non legge niente (o non riceve perchè e l'altra che non scrive?).
Ho installato sia le javax.comm che le rxtx.
Ecco due classi:
Classe ScriviSuSeriale
codice:
import javax.comm.*;
import java.io.*;
public class ScriviSuSeriale {
public ScriviSuSeriale(String port, String message){
try{
CommPortIdentifier id_porta;
id_porta= CommPortIdentifier.getPortIdentifier(port);
CommPort porta=id_porta.open("ScriviSuSeriale",200);
OutputStream out=porta.getOutputStream();
SerialPort porta_seriale=(SerialPort)porta;
porta_seriale.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
PrintStream ps= new PrintStream(out);
ps.print(message);
ps.close();
porta.close();
}
catch(NoSuchPortException ne){
System.out.println("La porta "+port+" non e' presente");
}catch(PortInUseException pe){
System.out.println("La porta "+port+" e' occupata da "+pe.currentOwner);
}catch(UnsupportedCommOperationException ue){
System.out.println("La porta non supporta le proprietà impostate");
}catch(IOException ioe){
System.out.println("Errore di IO");
}
}
public static void main(String[] args){
String port="COM2";
ScriviSuSeriale scrivi= new ScriviSuSeriale(port,"Pippo");
}
}
Classe LeggiDaSeriale
codice:
import javax.comm.*;
import java.io.*;
public class LeggiDaSeriale extends Thread implements SerialPortEventListener {
private InputStream in;
public void LeggiDaSeriale(String port){
try{
CommPortIdentifier id_porta;
id_porta=CommPortIdentifier.getPortIdentifier(port);
CommPort porta=id_porta.open("LeggiDaSeriale",200);
in=porta.getInputStream();
SerialPort porta_seriale=(SerialPort)porta;
porta_seriale.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
porta_seriale.addEventListener(this);
start();
}
catch(NoSuchPortException ne){
System.out.println("La porta "+port+" non e' presente");
}
catch(PortInUseException pe){
System.out.println("La porta "+port+" e' occupata da "+pe.currentOwner);
}
catch(UnsupportedCommOperationException ue){
System.out.println("La porta non supporta le proprietà impostate");
}
catch(IOException ioe){
System.out.println("Errore di IO");
}
catch(java.util.TooManyListenersException tme){
System.out.println("Si può registrare solamente un ascoltatore");
}
}
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (in.available() > 0){
int numBytes = in.read(readBuffer);
}
System.out.print(new String(readBuffer));
}
catch (IOException e) {}
break;
}
}
public static void main(String[] args){
String port="COM2";
LeggiDaSeriale leggi= new LeggiDaSeriale(port);
}
}
Qualcuno sa dirmi dove sbaglio?