So, now your serialEvent() method is only called when there is a line feed in the serial buffer. This should be the last character of the packet.
In the serialEvent() method, you then read everything in the buffer, including the carriage return and line feed, and use them as part of the file name.
You need to replace the readBytes() method with readBytesUntil(), which will let you NOT read the carriage return and line feed.
byte[] inBuffer = new byte[7];
while (myPort.available() > 0) {
myPort.readBytesUntil(cr, inBuffer);
Be sure to add a call to readBytes() to strip the CR and LF from the buffer, or they will simply remain there, and be in front of the data next time.
How do I assign the output of myString to val?
if (inBuffer != null) {
String myString = new String(inBuffer);
println(myString);
val = myString;
}
could, and should, be
if (inBuffer != null) {
val = new String(inBuffer);
print("val = [");
print(val);
println("]");
}
This should show
val = [47]
val = [1]
val = [2]
in the window, without all the blank lines.