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));
message.append("M");
message.append(new Character((char) 3));
message.append(new Character((char) 13));
message.append(new Character((char) 10));
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;