如何在java中清除SerialPort的OutputStream,而不必关闭它? [英] How to clear the OutputStream of a SerialPort in java, without having to close it?

查看:68
本文介绍了如何在java中清除SerialPort的OutputStream,而不必关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SerialPort 类连接到 USB 调制解调器并发送 SMS.我可以成功发送消息.但是当我这样做时,似乎有一些数据来自调制解调器的回复,我需要清除它才能列出 USB 调制解调器上的所有数据.

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.

这是我发送消息的代码.--

This is my code to send the message.--

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;
        }
        } 
       }

对此主题的任何帮助将不胜感激.提前致谢!

Any help on this subject would be highly appreciated. Thanks in advance !

推荐答案

与 C# 不同,Java 中的 SerialPort 类似乎没有定义丢弃所有数据的方法.正如 Edwin Buck 提到的,您必须读入流中的所有数据,然后丢弃您不感兴趣的数据.

Looks like SerialPort Class in Java doesn't have a method defined to discard all it's data, unlike C#. As mentioned by Edwin Buck, you have to read in all data in stream and simply discard the ones that you are not interested.

这篇关于如何在java中清除SerialPort的OutputStream,而不必关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆