Hi there,
while your “solution” definitely closes serial port connections, it closes all of them. So if you have multiple connections, executing the command task will kill all of them.
For instructions on how to gracefully shut down serial port connections in Royal TSX (and screen in general) please see this KB article: Gracefully closing a serial port connection with the iTerm2 plugin.
cheers,
felix