Is there a way to apply sudo to specific serial port? Otherwise can not open

I'm building a Pi Zero hotspot modem with https://www.waveshare.com/wiki/SIM7600G-H_4G_HAT_(B) using RNDIS , which needs AT commands via /dev/ttyUSB2 to get GPS info and all the other settings.

I just rang into a wall that if I use sudo minicom -D /dev/ttyUSB2 everything works well.

But in node red serial nodes, it complains can not open
"[serialconfig:a6eae3b1762db5d3] serial port /dev/ttyUSB2 error: Error: Error: Device or resource busy, cannot open /dev/ttyUSB2"

I tested minicom -D /dev/ttyUSB2 in SSH and got the same error
minicom: cannot open /dev/ttyUSB2: Device or resource busy

so I thought to change permission with chmod but did not solve the problem

pi@zero2:~ $ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Nov  3 21:42 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Nov  3 21:42 /dev/ttyUSB1
crwxrwxrwx 1 root dialout 188, 2 Nov  3 21:49 /dev/ttyUSB2
crw-rw---- 1 root dialout 188, 3 Nov  3 21:42 /dev/ttyUSB3
crw-rw---- 1 root dialout 188, 4 Nov  3 21:41 /dev/ttyUSB4

After some searching I did not find any info on how to elevate a particular node or port within node red, only hint I found was to redirect the serial.

Welcome to the forum.

If the user pi is not already a member of the group dialout you need to add the user pi to the dialout group.

Check with:

cat /etc/group | grep dialout

Change with:

sudo usermod -a -G dialout pi

Also, disconnect any minicom sessions from the port you are trying to access from node-red.

... or vice-versa - stop Node-RED if you want to use minicom instead.

1 Like

Thanks for the guide, but still not finding any progress

pi@zero2:~ $ cat /etc/group | grep dialout
dialout:x:20:pi

I had node red on auto start and after a fresh reboot, I double checked in top that minicom is not even running.

And when I used minicom to debug, I exit with Ctrl A+X rather than A+Q. I'm assuming that with A+X all session would be terminated on exit

Is there anything setting with minicom auto occupy certain port on boot? I don't feel like there is but again I'm not that experienced with linux dev

Can you run
node-red-stop
node-red-start
and post the full output please.

How many serial nodes do you have in your flow?
If you go into the serial node and click on the Serial Port dropdown, how many entries do you see? There should only be one unless you are using multiple serial ports.

There are 7 serial ports total

  • USB0-4 are all the ports of SIM7600 for various functions, of which USB2 is for AT command, but node red can't connect to
  • Then there are standard Pi uart ttyS0 and ttyAMA0, both node red can connect to

Its a fresh imaged SD with only one serial node in the whole setup

here is the full log

pi@raspberrypi:~ $ node-red-stop
Stop Node-RED

Use   node-red-start   to start Node-RED again

pi@raspberrypi:~ $ node-red-start

Start Node-RED

Once Node-RED has started, point a browser at http://10.42.0.1:1880
On Pi Node-RED works better with the Firefox or Chrome browser

Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot

To find more nodes and example flows - go to http://flows.nodered.org

Starting as a systemd service.
4 Nov 09:09:41 - [info]
Welcome to Node-RED
===================
4 Nov 09:09:41 - [info] Node-RED version: v3.0.2
4 Nov 09:09:41 - [info] Node.js  version: v16.18.0
4 Nov 09:09:41 - [info] Linux 5.15.74-v7l+ arm LE
4 Nov 09:09:43 - [info] Loading palette nodes
4 Nov 09:09:44 - [info] Settings file  : /home/pi/.node-red/settings.js
4 Nov 09:09:44 - [info] Context store  : 'default' [module=memory]
4 Nov 09:09:44 - [info] User directory : /home/pi/.node-red
4 Nov 09:09:44 - [info] Projects directory: /home/pi/.node-red/projects
4 Nov 09:09:44 - [warn] No active project : using default flows file
4 Nov 09:09:44 - [info] Flows file     : /home/pi/.node-red/flows.json
4 Nov 09:09:44 - [info] Server now running at http://127.0.0.1:1880/
4 Nov 09:09:44 - [warn]
---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------
4 Nov 09:09:44 - [info] Starting flows
4 Nov 09:09:44 - [info] Started flows
4 Nov 09:09:44 - [error] [serialconfig:3438af58cb61bc39] serial port /dev/ttyUSB2 error: Error: Error: Device or resource busy, cannot open /dev/ttyUSB2

Do you mean you have configured all 7 in node red but are only using one? If so then try removing all of them except the one you want. Make sure you only see one when you click on the port drop-down.

What does this command reveal?

ls -l /proc/[0-9]*/fd/* |grep /dev/ttyUSB2

example output:

lrwx------ 1 root dialout 64 Sep 12 10:30 /proc/14683/fd/3 -> /dev/ttyUSB2

This means the pid 14683 has the /dev/ttyUSB2 open as file descriptor 3


Then you can find out what that process is...

ps -p 14683 -o command

Or you can use top or htop and look for 14683

Hi Steve, thank you so much for detailed direction. I was just about to gave up on this one after so many days.

I did manage some ironic progress, as it turns out, the conflict was between Network Manager and RNDIS driver, both are what I consider as deep deep system level program.

I managed to running node red as root which did allow connection to ttyUSB2 but sending/receiving are worse than hit n miss

As of Sep22 Pi OS update, the Network Manger is built in and will eventually replace dhcpcd

Don't use the tty entries for this otherwise you will occasionally find you are addressing the wrong device. Use the byid entries instead which don't change between reboots.

I think I'm in a confirmed dead end now.... :skull_and_crossbones:

port busy but no process found....

it's a clean Pi OS image with only minicom installed and network manager enabled

pi@raspberrypi:~ $ ls -l /proc/[0-9]*/fd/* |grep /dev/tty
ls: cannot access '/proc/1803/fd/255': No such file or directory
ls: cannot access '/proc/1803/fd/3': No such file or directory
lrwx------ 1 pi pi 64 Nov  5 06:50 /proc/880/fd/0 -> /dev/tty1
lrwx------ 1 pi pi 64 Nov  5 06:50 /proc/880/fd/1 -> /dev/tty1
lrwx------ 1 pi pi 64 Nov  5 06:50 /proc/880/fd/2 -> /dev/tty1
lrwx------ 1 pi pi 64 Nov  5 06:50 /proc/880/fd/255 -> /dev/tty1
pi@raspberrypi:~ $ minicom -D /dev/ttyUSB2
minicom: cannot open /dev/ttyUSB2: Device or resource busy

Simpler way to see what is using a port is:

$ sudo lsof /dev/ttyUSB0
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
node-red 2318 home   55u   CHR  188,0      0t0 12956 /dev/ttyUSB0

$ sudo ps e 2318
  PID TTY      STAT   TIME COMMAND
 2318 ?        Ssl  178:39 node-red LANG=en_GB.UTF-8 LANGUAGE=en_GB:en PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/home LOGNAME=home USER=home SHELL=/bin/bash INVOCATION_ID=136478dcd4954dc68badbdb1b64a86b5 JOURNAL_STREAM=9:11177553 NODE_RED_EXE=/h

But, again, I recommend using the much clearer ls -l /dev/serial/by-id/ and using the id rather than the tty port since the id will not change between reboots.

$ ls -a /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 Oct 12 04:02 usb-1a86_USB_Serial-if00-port0 -> ../../ttyUSB1
lrwxrwxrwx 1 root root 13 Oct 12 04:02 usb-RFXCOM_RFXtrx433_A1Y06XVZ-if00-port0 -> ../../ttyUSB0

Thanks for your help, I actually gave up on network manager and followed the guide to setup dhcpcd hotspot which starts faster in my tests.

Then when I switched back to network manager to test lsof you suggested, the USB2 becomes available, the conflict seems to disappeared.....I'll do more testing on that later

I found the by-id to be usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if04-port0, what's the format to use as substitue of /dev/ttyUSB2

pi@raspberrypi:~ $ ls -l /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 Nov  5 08:18 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if02-port0 -> ../../ttyUSB0
lrwxrwxrwx 1 root root 13 Nov  5 08:18 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if03-port0 -> ../../ttyUSB1
lrwxrwxrwx 1 root root 13 Nov  5 08:18 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if04-port0 -> ../../ttyUSB2
lrwxrwxrwx 1 root root 13 Nov  5 08:18 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if05-port0 -> ../../ttyUSB3
lrwxrwxrwx 1 root root 13 Nov  5 08:18 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if06-port0 -> ../../ttyUSB4

thanks to all your help, I found the process. It's the same process that runs USB0 so I can't just kill it. I guess just have to wait for some pro to patch the bug down the track

Also, if there no sim card inserted and no internet connection, USB2 would be free hence my previous fluke

pi@raspberrypi:~ $ sudo lsof /dev/ttyUSB0
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ModemMana 574 root   12u   CHR  188,0      0t0  454 /dev/ttyUSB0
pi@raspberrypi:~ $ sudo lsof /dev/ttyUSB2
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ModemMana 574 root   10u   CHR  188,2      0t0  464 /dev/ttyUSB2
pi@raspberrypi:~ $ sudo ps e 574
  PID TTY      STAT   TIME COMMAND
  574 ?        Ssl    0:01 /usr/sbin/ModemManager LANG=en_GB.UTF-8 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/root LOGNAME=root U

Try

sudo systemctl stop ModemManager
sudo systemctl disable ModemManager

/dev/serial/by-id/usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if04-port0

Hi everyone, thanks for your amazing help. I got this working by stop and disable Modem Manager completely and rely on RNDIS driver

1 Like

@TotallyInformation Can I trouble you again? I did notice ttyUSBx can change quite often, but I found even ls -l /dev/serial/by-id/ can output different device IDs

Mostly I get this

$ ls -l /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 Nov 14 08:54 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if02-port0 -> ../../ttyUSB0
lrwxrwxrwx 1 root root 13 Nov 14 08:54 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if03-port0 -> ../../ttyUSB1
lrwxrwxrwx 1 root root 13 Nov 14 08:54 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if04-port0 -> ../../ttyUSB3
lrwxrwxrwx 1 root root 13 Nov 14 08:54 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if05-port0 -> ../../ttyUSB4
lrwxrwxrwx 1 root root 13 Nov 14 08:54 usb-SimTech__Incorporated_SimTech__Incorporated_0123456789ABCDEF-if06-port0 -> ../../ttyUSB5

But some time I get this

$ ls -l /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 Nov 21 08:50 usb-SimTech__Incorporated_SimTech__Incorporated-if02-port0 -> ../../ttyUSB0
lrwxrwxrwx 1 root root 13 Nov 21 08:50 usb-SimTech__Incorporated_SimTech__Incorporated-if03-port0 -> ../../ttyUSB2
lrwxrwxrwx 1 root root 13 Nov 21 08:50 usb-SimTech__Incorporated_SimTech__Incorporated-if04-port0 -> ../../ttyUSB3
lrwxrwxrwx 1 root root 13 Nov 21 08:50 usb-SimTech__Incorporated_SimTech__Incorporated-if05-port0 -> ../../ttyUSB4
lrwxrwxrwx 1 root root 13 Nov 21 08:50 usb-SimTech__Incorporated_SimTech__Incorporated-if06-port0 -> ../../ttyUSB5

I did some search that there's no way to force a static device ID in linux, so is my only way out write a bit of script to dynamically get the ID?

if so, how do I go about setting port name dynamically in Node-Red, this post says its impossible

Never seen that issue before, I've always assumed that the by-id entry was fixed.

Your only other hope is that you can find some other unique identifier that does not change. See this example blog post for example that explains how to look for id's and how to use UDEV to create a mapping.

Assign a static USB port on Linux (msadowski.github.io)

If you have to resort to this, just note that the serial port will only ever change after a reboot (well I'm pretty sure that's true anyway). So you can simply set an environment variable after boot and you should be able to use that in the node's settings. Obviously, you need to make sure you set the env variable before node-red starts.