-1

I have this code to write and read to COM port.

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

But I need to wait the answer before exiting the method (see where I put a placeholder), I need something like a syncronous read, how to do it?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • Code is a bit whacked. No need to do threading and events. Why don't you just write to the serial port output stream and read from the input stream? This will be synchronous. If you really need async, then you are in a worl – DomV Jul 01 '13 at 22:03
  • But I don't know when the inputstream will receive data... how to stop the main thread until printer answers? – Tobia Jul 02 '13 at 06:07

1 Answers1

0

For me the solution was: jssc.SerialPort instead of rxtx or java.comm.

Here I found this method:

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

To wait some bytes until a timeout.

Tobia
  • 9,165
  • 28
  • 114
  • 219