You might also want to add a short delay after restarting the serial port, because reconfiguring the output level could cause a brief output pulse during the time it was set to the unintended level. Waiting 10 bit times of 31250 baud ought to be enough.
Then when you use the MIDI library, the TX2 output becomes inverted. It's not the MIDI library doing this. MIDI still works the same way, but you've altered the serial port settings.