Error handling in openPort function