I\'m beginner in Java. I\'m reading data from device through serial port. I\'m getting data for every one minute, but first reading is coming half, after that data is coming
Try flushing the input buffer of the port before doing your read. Otherwise, if the sending end has sent data during your program's startup (or closely before, that might be up to the operating system), you will get old buffered data.
Also, if possible, consider adding message framing to the protocol, so you can detect when you have read something that is not, in fact, a complete message, and discard it. This is often very helpful with these kinds of issues.
Use the following:
while (inputStream.available()>0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer));
}
You are printing the result out of the while loop. However the code inside the loop may run more than once, so chunk of data will be lost.
This looks as if you were reading the rest of some message which was sent before you started.
Try to read as much data as possible as you start the program to clear any hardware buffers. After that, start your processing.