Problem with Java SerialPortEvent: DATA_AVAILABLE calls are faster than incoming data

I am publishing for the first time, so I hope that I will write everything in accordance with the format.

I am currently working on a project using serial communication with RXTX between a Java application and a measuring device. This works fine, but now I want to catch the data that the device sends with events.

Below is the code, but has the following problem: it DATA_AVAILABLEwill be called 4 times before all data is sent. I will catch this in a line called vBuffer, and I can catch the data to get the full line.

Now I want to return this data (full line), but I can not find SerialPortEventwhich will wait until all the data has been sent to return this line.

In the example below, I use OUTPUT_BUFFER_EMPTY, but this is called at the beginning of sending the command. This means that when sending a command for the second time, the event OUTPUT_BUFFER_EMPTYwill return vBuffer with data from the first command and immediately after the second command is run. The third time OUTPUT_BUFFER_EMPTYsends data from the second command and starts the third, etc.

Is there a way to DATA_AVAILABLEwait until all data is sent, or is there another event that will be raised after all data has been sent?

Additional information: the command sends with a Stringbuildercharacter symbol to make sure that the correct format for the device is sent. Team members as follows: <STX><COMMAND><RTX><CR><LF>. Can I catch the end by watching when the team ends? If so, how?

Update: this is the code I'm sending the function to:

  StringBuilder message = new StringBuilder();
  message.append(new Character((char) 2));   // STX (Start of Text)
  message.append("M");                       // Command character
  message.append(new Character((char) 3));   // ETX (End of Text
  message.append(new Character((char) 13));  // CR (Carriage Return)
  message.append(new Character((char) 10));  // LF (Line Feed)
  outputStream.write(message.toString().getBytes());

DATA_AVAILABLE. , .

: , .

serialEvent:

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:
    if (vBuffer != "")
     {
      System.out.println(vBuffer);
     }
     break;  
   case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[40];

    try
    {
      while (inputStream.available() > 0)
      {
        int numBytes = inputStream.read(readBuffer);
      }
      vBuffer += new String(readBuffer);
      System.out.print(new String(readBuffer));
    }
    catch (IOException e)
    {
      System.out.println(e);
    }
    break;
+5
1

STX ETX, , (ETX ). , .

!

public void serialEvent(SerialPortEvent event)
{

 if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE)
 {
  System.out.println("Data available event received");

  try
  {
    int available = inputStream.available();
    byte[] readBuffer = new byte[available];

    if (available > 0)
    {
      inputStream.read(readBuffer);
    }

    messageBuffer = messageBuffer + new String(readBuffer);

    try
    {
      int start = messageBuffer.indexOf(new Character((char) 2));
      int end = messageBuffer.indexOf(new Character((char) 10));

      if (start >= 0 && end >= 0)
      {
        System.out.println("We found 1 complete message!!");
        System.out.println(messageBuffer.substring(start, end));
        _fireBufferEvent();
        messageBuffer = "";
      }
    }
    catch (IndexOutOfBoundsException ex)
    {
      System.out.println("IndexOutOfBoundsException, message not complete yet. Waiting for more data.");
    }
  }
  catch (IOException ex)
  {
    System.out.println("IOException while reading data:");
    System.out.println(ex);
  }
}
+2

All Articles