Ola, estou tentando estabelecer uma conexao com o meu modem usando a API javax.comm
A conexão deveria ser semelhante ao hyper terminal, aonde eu posso enviar comandos AT e esperar a resposta do modem aos comando que eu envio.
Eu consegui estabelecer conexao mas nao consigo enviar os comando AT e verificar a resposta.
Segue abaixo o codigo que obtive no proprio site da sun
Desde ja muito obrigado
public class SimpleWrite {
private static String messageString = "ATD 50841134"; //Liga para este numero
public static void main(String[] args) {
SerialPort serialPort = null;
OutputStream outputStream = null;
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM5");
serialPort = (SerialPort) portId.open("SimpleWrite",2000);
outputStream = serialPort.getOutputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.notifyOnOutputEmpty(true);
outputStream.write(messageString.getBytes());
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (PortInUseException e) {
} catch (IOException e) {
} catch (UnsupportedCommOperationException e) {
} catch (Exception e) {
}
serialPort.close();
System.exit(1);
}
}
public class SimpleRead implements Runnable, SerialPortEventListener {
private static final Logger logger = Util.startLogger(SimpleRead.class,"simpleReadLog");
static CommPortIdentifier portId;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
try {
portId = CommPortIdentifier.getPortIdentifier("COM5");
SimpleRead reader = new SimpleRead();
} catch (NoSuchPortException e) {
e.printStackTrace();
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
inputStream = serialPort.getInputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (PortInUseException e) {
} catch (IOException e) {
} catch (TooManyListenersException e) {
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} 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 (inputStream.available() > 0)
inputStream.read(readBuffer);
logger.info(new String(readBuffer));
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}