I don't know anything about Mathematica but I would be very surprised if it is necessary to re-upload the sketch to fix things. How could anything on the PC (except the Arduino IDE) alter the contents of the Arduino?
Try the following ...
Get your code working with the Serial Monitor - as you have already described
Try using Mathematica (I assume you will get the same wrong results)
Go back to the Serial monitor (also I assume wrong results).
Now for the new bits (all the time using the Serial Monitor)
Push the reset button on the Arduino - does that solve the problem ?
Restart the Arduino IDE - ?
Unplug and replug the Arduino - ?
Unplug the Arduino while the Serial Monitor is open and then replug it - which should cause the Arduino to appear on a different serial port - ?
Restart the PC - ?
I wonder if Mathematica may be adding a Carriage Return and / or Line feed after the 'S'. You can simulate that on the Serial Monitor by changing the line-ending box at the bottom of the Monitor screen to see if it causes problems. But I can't see why that would persist after Mathematica is stopped.
Have you a second PC that you could try the Arduino on without changing the Arduino code?
...R