Ciao a tutti, specialmen te a pippo_75.
Sto usando le javacomm per realizzare un programma che attraverso la seriale del mio computer, mandi dei messaggi attraverso un modem gsm.
Per il momento sto facendo delle prove con un modem normale.
Il problema sta che quando mando il comando AT, mi aspetto la risposta OK dal modem, ma questa non mi viene visualizzata, mi viene visualizzato l' echo del comando da me mandato.
Qualcuno può darmi una mano? Posto anche il codice, in modo tale che si capisca meglio il problema.
Ciao,
Spero che pippo_75 mi sappia dare una mano.
Cristian
CLASSE ApriSeriale.java
CLASSE Logger.javacodice:import java.io.*; import javax.comm.*; import java.util.*; public class ApriSeriale { //COSTRUTTORE public ApriSeriale() throws IOException,NoSuchPortException,UnsupportedCommOperationException,PortInUseException { OutputStream os; InputStream is; SerialPort sp=null; String str; Enumeration en=CommPortIdentifier.getPortIdentifiers(); while(en.hasMoreElements()){ CommPortIdentifier cpi=(CommPortIdentifier)en.nextElement(); if(cpi.getPortType()==CommPortIdentifier.PORT_SERIAL){ System.out.println("Sto usando la seguente porta seriale: "+cpi.getName()); str=cpi.getName(); try{ sp=(SerialPort)cpi.open("ApriSeriale",500); } catch(PortInUseException ex){ System.out.println("Port Already In Use By "+ex); } //Acquisisco input e output dalla seriale is=sp.getInputStream(); os=sp.getOutputStream(); //Setto i parametri della seriale sp.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); //Stampo i parametri appena settati System.out.print("\n\n"); System.out.println("****************************************"); System.out.println("PORTA SELEZIONATA: "+sp.getName()); System.out.println("BAUD RATE: "+sp.getBaudRate()); System.out.println("DATABITS: "+sp.getDataBits()); System.out.println("STOPBITS: "+sp.getStopBits()); System.out.println("PARITY: "+sp.getParity()); System.out.println("****************************************"); System.out.println("\n\n"); try{ System.out.println("Sto scrivendo ATD"); os.write("AT".getBytes()); System.out.println("Scritto"); } catch(IOException ex){ System.out.println("Ecezione di tipo IOException"); } sp.notifyOnDataAvailable(true); /* try{ System.out.println("Sto scrivendo ATD"); os.write("ATD".getBytes()); System.out.println("Scritto"); } catch(IOException ex){ System.out.println("Ecezione di tipo IOException"); }*/ try{ System.out.println("Ora sto entrando in Logger"); sp.addEventListener(new Logger(str,sp)); } catch(TooManyListenersException ex){ System.out.println("Eccezione di tipo TooManyListenersException"); } } } } }
Classe Test.javacodice:import java.io.*; import javax.comm.*; import java.util.*; public class Logger implements SerialPortEventListener{ String portName; SerialPort thePort; BufferedReader br; InputStream in; public Logger(String name,SerialPort port)throws IOException{ portName= name; thePort= port; br=new BufferedReader(new InputStreamReader(thePort.getInputStream())); in=thePort.getInputStream(); } //Gestore Eventi Dati Disponibili public void serialEvent (SerialPortEvent spe){ if(spe.getEventType()==SerialPortEvent.DATA_AVAILABLE){ System.out.println("DATI DISPONIBILI"); System.out.println(); byte[] readBuffer = new byte[20]; try { while (in.available() > 0) { int numBytes = in.read(readBuffer); char strng =(char)numBytes; System.out.println("STRNG: "+strng); } System.out.print("BUFFER: "+new String(readBuffer)); } catch (IOException e) {} //thePort.close(); //System.exit(0); } } }
codice:import java.io.*; import javax.comm.*; public class Test{ public static void main(String args[])throws IOException,NoSuchPortException,UnsupportedCommOperationException,PortInUseException{ ApriSeriale as =new ApriSeriale(); } }
Grazie ancora, Taurus

Rispondi quotando