SerialPort 클래스를 사용하여 USB 모뎀에 연결하고 SMS를 보냅니다. 메시지를 성공적으로 보낼 수 있습니다. 하지만 내가 할 때, 모뎀으로부터 답장으로 오는 일부 데이터가있는 것 같아서 USB 모뎀의 모든 데이터를 나열하려면이 데이터를 지울 필요가 있습니다. 자바에서 SerialPort의 OutputStream을 지우지 않고 어떻게 지울 수 있습니까?
이
메시지 -이 주제에 어떤 도움이 높게 평가 될 것이다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;
}
}
}
를 보낼 수있는 내 코드입니다. 미리 감사드립니다!
이 질문보기 http://stackoverflow.com/questions/4454961/how-do-you-flush-a-java-serial-inputstream –