0

Situation: I'm trying to make the incoming data from SerialPort usefull for my purposes. In one class Processor.java I've implemented several methods - one of them (serialEvent) implements gnu.io.SerialPortEventListener. It stores the information read from inputStream in a buffer which is a byte array. There is also a method, which writes data to outputStream.

Problem: I want to implement a method (in the same class) which will write something to outputStream depending on the messages read from the inputStream.

Pseudo code:

@Override
public void serialEvent(SerialPortEvent event) {
// get data
}

public void writeData(String dataToWrite) {
// write data
}

public void respond() {
// write data
// wait for appropriate response (read data)
// write data
// ...
}

How can I do this?

Rubid
  • 59
  • 1
  • 1
  • 5

1 Answers1

0

Only thing that comes to mind is a background thread that waits for input-buffer-full condition to process the received message and responds to it.

If you are communicating in fixed length packets or start-stop marked packets you should create a thread that would monitor the serial port, buffer the received data and once a "packet/message complete" condition is met to fire an event to a registered listener (in another thread if possible). That listener would then process the message and respond (in its own thread).

Cebence
  • 2,406
  • 2
  • 19
  • 20