Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to clear the OutputStream of a SerialPort in java, without having to close it?
    text
    copied!<p>I'm using the SerialPort class to connect to a USB modem and send an SMS. I can succesfully send a message. But when I do, there seems to be some data that comes as a reply from the modem and I need to clear it to list all the data on the USB modem.</p> <p>This is my code to send the message.--</p> <pre><code>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; } } } </code></pre> <p>Any help on this subject would be highly appreciated. Thanks in advance !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload