
34 MAKING THINGS TALK
Serial ports aren’t easily shared between applications.
In fact, only one application can have control of a serial
port at a time. If PuTTY or the screen program has the
serial port open to an Arduino module, for example, the
Arduino programming application can’t download new
code to the module. When an application tries to open
a serial port, it requests exclusive control of it either by
writing to a special file called a lock file or by asking the
operating system to lock the file on its behalf. When it
closes the serial port, it releases the lock on the serial
port. Sometimes when an application crashes while it’s
got a serial port open, it can forget to close the serial
port, with the result that no other application can open
the port. When this happens, the only thing you can
do to fix it is to restart the operating system, which
clears all the locks (alternatively, you could wait for the
operating system to figure out that the lock should be
released). To avoid this problem, make sure that you
close the serial port whenever you switch from one
application to another. Linux and Mac OS X users should
get in the habit of closing down screen with Ctrl- ...