I need to disable and then enable a serial port for testing purposes on a Java program.

Since the man page of setserial is stating: "Using UART type none will disable the port.", I use sudo setserial /dev/ttyS4 uart none to disable the port. This successfully disables the serial port. To enable the serial port again, I use sudo setserial /dev/ttyS4 -a autoconfig and the port is up as expected.

After I've disabled the port, I do try to open the port using the Java gnu.io.CommPortIdentifier function. When I try to enable the port after the executing the gnu.io.CommPortIdentifier function, I get this output:

$ sudo setserial /dev/ttyS4 -a autoconfig
Cannot autoconfigure port: Device or resource busy

How can I work around this problem? Is there a better way to enable/disable the serial port? Or can I clear the list of requests made to the port?

I obviously can't just kill the process, since my test is running in it.

2 Answers 2


I suppose that Java gnu.io.CommPortIdentifier will have bigger problems if you delete /dev/ttySx aftes disabling the serial port.

When yoou need to enable it again you must first create the node with MAKEDEV script or mknod command. Here is some info about creating serial devices.

  • I don't think this is the best approach... I need to emulate a situation where "the cable connecting in the port has fallen", and deleting the port wouldn't be the same, would it?
    – MarcioB
    Aug 8, 2014 at 16:41
  • Always glad to learn new info. Well, disabling a port is not the best approach also. Why don't you just unplug the cable and see what is happening?
    – LHristov
    Aug 8, 2014 at 18:04
  • Because it's supposed to be an automated test. And I did said what I was trying to do at the beginning of the question :)
    – MarcioB
    Aug 8, 2014 at 18:16
  • For me disabled port is much different from enabled, but not connected port. I've worked with serial ports under Windows and there disabled port is something you cannot use; enabled port with broken link is something you can open from the app and can send data, and can wait forever for answer (forever = timeout in my case). If you want to do automated test better think about hardware disabling/enabling e.g. to connect 2 serial ports through 3wired cable, put a relay on TX wire and disable and enable it programmatically, through timer or by any other way.
    – LHristov
    Aug 8, 2014 at 18:23
  • I thought that a disabled port was the closest I could get from an unconnected one, thanks for the info. Anyway, the hardware option you described is not available to me, do you see any other alternative?
    – MarcioB
    Aug 8, 2014 at 18:29

You can try to delete /var/lock/LCK..ttyS4, using rm. As I understand, you need to unlock the device in a such way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .