Java javax.comm同步读取



我有这段代码要写入和读取COM端口。

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
    import javax.comm.CommPortIdentifier;
    import javax.comm.PortInUseException;
    import javax.comm.SerialPort;
    import javax.comm.SerialPortEvent;
    import javax.comm.SerialPortEventListener;
    import javax.comm.UnsupportedCommOperationException;
    public class MainClass implements Runnable, SerialPortEventListener {
      static CommPortIdentifier portId;
      static Enumeration portList;
      InputStream inputStream;
      SerialPort serialPort;
      Thread readThread;
      public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            // if (portId.getName().equals("COM1")) {
            if (portId.getName().equals("/dev/term/a")) {
              MainClass reader = new MainClass();
            }
          }
        }
      }
      public MainClass() {
        try {
          serialPort = (SerialPort) portId.open("MainClassApp", 2000);
        } catch (PortInUseException e) {
        }
        try {
          inputStream = serialPort.getInputStream();
        } catch (IOException e) {
        }
        try {
          serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
        }
        serialPort.notifyOnDataAvailable(true);
        try {
          serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
              SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
        }
        readThread = new Thread(this);
        readThread.start();
/*HERE I NEED TO WAIT THE ANSWER*/
      }
      public void run() {
        try {
          Thread.sleep(20000);
        } catch (InterruptedException e) {
        }
      }
      public void serialEvent(SerialPortEvent event) {
        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:
          byte[] readBuffer = new byte[20];
          try {
            while (inputStream.available() > 0) {
              int numBytes = inputStream.read(readBuffer);
            }
            System.out.print(new String(readBuffer));
          } catch (IOException e) {
          }
          break;
        }
      }
    }

但在退出该方法之前,我需要等待答案(看看我把占位符放在哪里),我需要一个类似syncronous read的东西,怎么做?

对我来说,解决方案是:jssc。串行端口控制而不是rxtx或java.com.m。

在这里我发现了这个方法:

public java.lang.String readString(int byteCount, int timeout)

等待一些字节直到超时。

相关内容

  • 没有找到相关文章

最新更新