简体   繁体   English

Java RXTX 无法读取 InputStream

[英]Java RXTX Unable to read InputStream

I am having an issue with the Java serial reader I am trying to write, the issue seems to lie with the inputstream method...我正在尝试编写的 Java 串行阅读器有问题,问题似乎出在 inputstream 方法上...

I get the error with my readSerial() method, it gives me the error "Cannot make a static reference to the non-static method available() from the type InputStream"我的 readSerial() 方法出现错误,它给了我错误“无法从 InputStream 类型对非静态方法 available() 进行静态引用”

    import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.io.*;
import java.util.*;
import gnu.io.*;
 // Our RXTX package

public class basicinterface implements Runnable, SerialPortEventListener {
    private SerialPort serialPort ;         //defining serial port object
   private CommPortIdentifier portId  = null;       //my COM port
   private static final int TIME_OUT = 2000;    //time in milliseconds
   private static final int BAUD_RATE = 9600; //baud rate to 9600bps
   private BufferedReader input;               //declaring my input buffer
   private OutputStream output;                //declaring output stream
   private String name;        //user input name string
   Scanner inputName;          //user input name

   //method initialize
   private void initialize(){
       CommPortIdentifier ports = null;      //to browse through each port identified
       Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); //store all available ports
       while(portEnum.hasMoreElements()){  //browse through available ports
               ports = (CommPortIdentifier)portEnum.nextElement();
            //following line checks whether there is the port i am looking for and whether it is serial
              if(ports.getPortType() == CommPortIdentifier.PORT_SERIAL&&ports.getName().equals("COM7")){ 

               System.out.println("COM port found:COM6");
               portId = ports;                  //initialize my port
               break;                                                                                     }

           }
      //if serial port am looking for is not found
       if(portId==null){
           System.out.println("COM port not found");
           System.exit(1);
       }

                           }

   //end of initialize method

   //connect method

   private void portConnect(){
       //connect to port
       try{
        serialPort = (SerialPort)portId.open(this.getClass().getName(),TIME_OUT);   //down cast the comm port to serial port
                                                                                    //give the name of the application
                                                                                    //time to wait
       System.out.println("Port open succesful: COM6"); 

       //set serial port parameters
serialPort.setSerialPortParams(BAUD_RATE,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);



       }
       catch(PortInUseException e){
           System.out.println("Port already in use");
           System.exit(1);
       }
       catch(NullPointerException e2){
           System.out.println("COM port maybe disconnected");
       }
       catch(UnsupportedCommOperationException e3){
           System.out.println(e3.toString());
       }

       //input and output channels
       try{
     //defining reader and output stream
      input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
       output =  serialPort.getOutputStream();
       //adding listeners to input and output streams
       serialPort.addEventListener(this);
       serialPort.notifyOnDataAvailable(true);
       serialPort.notifyOnOutputEmpty(true);
       }
       catch(Exception e){
           System.out.println(e.toString());
                           }

   }
   //end of portConnect method

   private byte[] readBuffer = new byte[400];

   private void readSerial() {
       try {
           int availableBytes = InputStream.available();
           if (availableBytes > 0) {
               // Read the serial port
               InputStream.read(readBuffer, 0, availableBytes);

               // Print it out
               System.out.println(
                       new String(readBuffer, 0, availableBytes));
           }
       } catch (IOException e) {
       }
   }
   //readWrite method

   public void serialEvent(SerialPortEvent evt) { 
    byte[] readBuffer = new byte[400];
      if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) { //if data available on serial port
          readSerial();
            }

 }


   //end of serialEvent method

   //closePort method
   private void close(){
       if(serialPort!=null){
           serialPort.close(); //close serial port
       }
       input = null;        //close input and output streams
       output = null;
   }
   //main method
   public static void main(String[] args) {
       basicinterface myTest = new basicinterface();  //creates an object of the class
       myTest.initialize();
       myTest.portConnect();
      System.out.println("Started");
      while(1>0);       //wait till any activity



                                           }
//end of main method
//end of  SerialTest class

@Override
public void run() {
    // TODO Auto-generated method stub

}
}

I am still learning to code in Java and all help is appreciated!我仍在学习用 Java 编写代码,感谢所有帮助! My understanding is that the reading from serial input is possible but I seem to be missing something.我的理解是从串行输入读取是可能的,但我似乎遗漏了一些东西。

InputStream is a class; InputStream是一个类; you can't call instance methods on it.你不能在它上面调用实例方法。 The input field in your class is an instance and should be used to retrieve the data!类中的输入字段是一个实例,应该用于检索数据!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM