Я использую класс SerialPort для подключения к USB-модему и отправки SMS.
Я могу успешно отправить сообщение.
Но когда я делаю, кажется, что есть некоторые данные, которые приходят в ответ от модема, и мне нужно очистить их, чтобы вывести все данные на USB-модем.
Это мой код для отправки сообщения .--
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.comm.*;
import java.net.*;
import java.sql.*;
import java.lang.String.*;
public class recving implements SerialPortEventListener
{
static CommPortIdentifier id;
public static SerialPort sp;
static OutputStream os;
static InputStream is;
static Enumeration pl;
static PrintStream ps;
public static OutputStream f1;
public static void main(String[] args)
{
String line1 = "AT+CMGS=\"phonenumber\"\r\n"+"a simple message"+((char)26);
pl = CommPortIdentifier.getPortIdentifiers();
while (pl.hasMoreElements() )
{
try{
id= CommPortIdentifier.getPortIdentifier("COM4");
}
catch(Exception e)
{
System.out.println(e);
}
id = (CommPortIdentifier) pl.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if ( id.getName().equals("COM4"))
{
try
{
if(id.isCurrentlyOwned())
{
sp.close();
}
else
sp = (SerialPort) id.open("id",1000);
recving wr = new recving(sp);
}
catch (PortInUseException e)
{
System.out.println("Port In Use " + e);
}
try
{
os = sp.getOutputStream();
}
catch (IOException e)
{
System.out.println("Error writing to output stream " + e);
}
try
{
ps = new PrintStream(sp.getOutputStream());
}
catch (Exception e1)
{
System.out.println("Error printing to output stream " + e1);
}
try
{
sp.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException ex)
{
System.out.println("exception " + ex);
}
try
{
os.write(line1.getBytes());
os.flush();
}
catch (Exception exe)
{
System.out.println("Error writing message " + exe);
}
}
}
}
}
public recving(SerialPort serial)
{
try
{
is = serial.getInputStream();
try
{
serial.addEventListener(this);
}
catch (TooManyListenersException e)
{
System.out.println("Exception in Adding Listener" + e);
}
serial.notifyOnDataAvailable(true);
}
catch (Exception ex)
{
System.out.println("Exception in getting InputStream" + ex);
}
}
public void serialEvent(SerialPortEvent event)
{
CharSequence key="+CMTI:";
String line3="AT+CMGL=0\r\n";
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:
{
/*here i check the stream and data comes up !!
I dont want to parse any data when I send a message. and this interferes with the reading of data from the modem because there is data in the stream and I'm unable to pull up the required data by executing a command, So I need to be able to clear this stream up*/
is = sp.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String n = new String();
while((n= br.readLine())!= null){
System.out.println(n);
}
break;
}
}
}
Любая помощь по этому вопросу будет принята с благодарностью. Заранее спасибо!