0

I've got a main program that starts two threads. First I only had this thread, that executes the following inside the while(true):

loopCounter++;              
outputStream.write(pollBuf);
readResponse();
Thread.sleep(200);
outputStream.write(statusBuf);
readResponse();
logger.info("status executed");

The problem is that when the second readResponse doesn't return because the device listening to the comport simply doesn't answer I'm stuck and the display that gives the status of the machine simply still shows "running" instead of software error or something alike. So I need to know when this thread is stuck and therefore I added another thread that now gets created and started in the main program right before the other thread, the code inside the while(true) of the run() method of this second thread:

public class StatusThread implements Runnable {
  static Logger logger = Logger.getLogger(StatusThread.class);

  private Nv10ToVcdm mainProgram;

  public void initialize(Nv10ToVcdm mProgram, boolean acceptBills) {
    mainProgram = mProgram;
  }

  public void run() {
    int loopCounter = mainProgram.getLoopCounter();
    while (true) {
      try {
        Thread.sleep(1000);
        int currentLoopCounter = mainProgram.getLoopCounter();
        if (loopCounter != currentLoopCounter) {
          loopCounter = currentLoopCounter;
        } else {
          mainProgram.writeToDisplay("SOFTWARE", "ERROR");
        }
      } catch (InterruptedException ie) {
        logger.error("Interrupted exception: " + ie.getMessage());
        mainProgram.errorOnDisplay();
      }
    }
  }
}

Sadly the first thread being stuck on listening to the comport doesn't release the claim it has on the cpu so the second thread doesn't get any CPU time. So: How to show an error on the display when the thread listening to a com port hangs?

The readResponse method that hangs, afaik it hangs on "byte firstByte = (byte) inputStream.read();" because there's nothing to read:

private void readResponse() {
    byte[] bufferLeft = new byte[4];
    byte[] bufferRight = new byte[2];
    byte size = 0;
    boolean responseFound = false;

    try {
      while(!responseFound) {
        byte firstByte = (byte) inputStream.read();
        if (firstByte == -1) {
          logger.error("first byte of response is -1");
          mainProgram.errorOnDisplay();
          break;
        }
        for (int i = 0; i < 4; i++) {
          bufferLeft[i] = (byte) inputStream.read();
        }
        size = bufferLeft[0];
        if (size > 0) {
          bufferRight =  new byte[size];
          int i2 = 0;
          while (i2 < size) {
            bufferRight[i2] = (byte) inputStream.read();
            i2++;
          }
        }

        if (firstByte == 1 && bufferLeft[1] == 40) {
          responseFound = true;
        }
      }

      if (size == 11) {
        // some code
      }
    } catch(IOException ioe) {
      logger.error("IO Exception in readResponse: " + ioe.getMessage());
      mainProgram.errorOnDisplay();
    }
}

Edit (added complete code for second thread & readResponse method)

Inputstream is initialized as follows:

serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
inputStream = serialPort.getInputStream(); 
JeroenV
  • 53
  • 2
  • 10
  • You need to show all the code for this. What are the threads sharing? – MadConan Nov 05 '13 at 13:51
  • We really need the `readResponse()` code to be able to help. – azz Nov 05 '13 at 13:55
  • If the thread is blocked then it most definitely "release the claim it has on the cpu". Even if it is spinning the other thread should get some cycles. Both threads I suspect are blocked. – Gray Nov 05 '13 at 14:09
  • Are you sure there's data to be read? `inputStream.read()` will block until there's data. – dcsohl Nov 05 '13 at 14:12
  • http://stackoverflow.com/questions/804951/is-it-possible-to-read-from-a-inputstream-with-a-timeout might help – Taylor Nov 05 '13 at 14:13
  • No there isn't that's the point, because the program hangs when there's no data to read I want to show an error message – JeroenV Nov 05 '13 at 14:13
  • Thanks Taylor, that looks like a solution. Never heard of a Java Future before ... – JeroenV Nov 05 '13 at 14:15
  • It's newer concurrency stuff. – Taylor Nov 05 '13 at 14:16

1 Answers1

1

Did you tried to cheack data availability before read?

Something like:

if (inputStream.available() > 0) {
  // do your read
} else {
  // wait for some time and retry or trow an error
}