write to serial port inside try block