如何在java中清除SerialPort的OutputStream,而不必关闭它?
我正在使用SerialPort类连接到USB调制解调器并发送SMS。 我可以成功地发送消息。 但是当我这样做的时候,似乎有一些数据是作为调制解调器的应答而来的,我需要清除它以列出USB调制解调器上的所有数据 这是我发送消息的代码--如何在java中清除SerialPort的OutputStream,而不必关闭它?,java,serial-port,Java,Serial Port,我正在使用SerialPort类连接到USB调制解调器并发送SMS。 我可以成功地发送消息。 但是当我这样做的时候,似乎有一些数据是作为调制解调器的应答而来的,我需要清除它以列出USB调制解调器上的所有数据 这是我发送消息的代码-- 在此问题上的任何帮助都将不胜感激。提前谢谢 当您第一次抓取流时,将其数据排出。看起来Java中的SerialPort类没有定义一个方法来丢弃它的所有数据,这与C#不同。 正如Edwin Buck所提到的,您必须读入流中的所有数据,并简单地丢弃您不感兴趣的数据。请看这
在此问题上的任何帮助都将不胜感激。提前谢谢 当您第一次抓取流时,将其数据排出。看起来Java中的SerialPort类没有定义一个方法来丢弃它的所有数据,这与C#不同。
正如Edwin Buck所提到的,您必须读入流中的所有数据,并简单地丢弃您不感兴趣的数据。请看这个问题:我想我必须对流进行清晰编码。无论如何,谢谢:)
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;
}
}
}