JujuZA
JujuZA

Reputation: 157

How to find and stop what has locked a serial port in Linux

I'm trying to connect to a makerbot 3d printer using pyserial. The part of the code for connecting that's giving me trouble is this:

import serial
file = serial.Serial('/dev/ttyACM1', 115200, timeout=1)

When I run it as is (ie. just python), then I get:

[Errno 13] Permission denied: '/dev/ttyACM1'

So I run it with super user privelleges (sudo python) and I get this back:

serial.serialutil.SerialException: pid 3516 could not open port /var/lock/LCK..ttyACM1: locked by PID 1054

So I guess the device is locked by something. But how can I find out what is locking it, and how to stop it?

Upvotes: 2

Views: 4273

Answers (1)

Lynn Crumbling
Lynn Crumbling

Reputation: 13367

I would try:

lsof /dev/ttyACM1

You may need to grab lsof, as it may not be installed by default on your dist.

Per lsof's man page:

Lsof lists on its standard output file information about files opened by processes [...] An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file (Internet socket, NFS file or UNIX domain socket.) A specific file or all the files in a file system may be selected by path.

Upvotes: 2

Related Questions