OK, so your problem is not with the Arduino or your sketch. So I am no help.
But it seems like messing with the com port on the PC side as you are doing makes something go wrong.
The only thing I can suggest is to place the output you wish to
hoover up collect on another serial port, independent of the one used to uplaod code and talk to the serial monitor in the IDE.
Some Arduinos have extra hardware serial ports. If you have a hardware serial port then try it.
If you Arduino has no additional hardware support for serial communication (UARTs) you could try the Arduino SoftwareSerial library.