I have a class, as such:
private class verifierListener implements SerialPortEventListener {
String outBuffer;
char charBuffer;
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) {//If data is available
timeOut = 1000;
lastReadTimer = System.currentTimeMillis();
if (event.getEventValue() > 0) {//Check bytes count in the input buffer
try {
byte[] buffer = verifierPort.readBytes(event.getEventValue());
//INSERT CODE HERE
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
With two possible methods of implementation(in the INSERT CODE HERE area)
Case A:
outBuffer = new String(buffer);
bfrFile.print(outBuffer);
sysOutWriter.append(outBuffer);
Case B:
for(byte bt : buffer) {
charBuffer = (char) bt;
bfrFile.print(charBuffer);
sysOutWriter.append(charBuffer);
}
Both compile and run, and do what they are supposed to do. But I’m trying to make this code perform as seamlessly as possible, so I don’t possibly risk a loss of transmitted data on a lower end PC.
I’m assuming that case A will have more overhead due to the String initialization, but I wanted to make certain before I remove it.
Can y’all tell which one would be cleaner, and/or how to determine the processing cost for each?
You shouldn’t be loosing any data, even on a lower-end PC. That’s because there are (several) buffers in between your code and the actual data that’s coming over the serial port(operating system buffers, Java buffers, etc..). Speed-wise, unless you’re running this code a lot(as in, several thousand times per second) you shouldn’t notice a difference.
Assuming this is a standard serial connection, and you were running at 115200 bits per second, that means that you’re getting at most 14,400 characters per second. Even if you were to read those one character at a time, you shouldn’t see a big speed hit.