You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Raspbian环境下实现ttyUSB0连接时自动执行指定命令

解决Raspbian下插拔Arduino后自动配置波特率并重启依赖程序的方案

Got it, let's work through this problem properly. The most reliable way to handle automatic actions when a USB device is plugged/unplugged on Raspbian is using udev rules—it's Linux's native tool for responding to device events, perfect for your use case.

Step 1: Identify Your Arduino's Unique Device Attributes

First, we need to target your specific Arduino (not just any /dev/ttyUSB0, which can shift if you plug in other serial devices). Here's how to get its unique IDs:

  • Plug in your Arduino, then run this command in the terminal:

    lsusb
    

    Look for a line like this (yours might have a different ID, depending on your Arduino model):

    Bus 001 Device 005: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)
    The two hex numbers here (2341 and 0043) are your Arduino's Vendor ID (VID) and Product ID (PID)—we'll use these to create a precise udev rule.

  • To confirm, you can also run this command to get more detailed udev attributes:

    udevadm info -a -n /dev/ttyUSB0 | grep -E "(SUBSYSTEM|ATTRS{idVendor}|ATTRS{idProduct})"
    

    This will show you the exact attributes udev uses to recognize your device, so we can match them perfectly.

Step 2: Create a Udev Rule File

Udev rules live in /etc/udev/rules.d/. Let's create a custom rule for your Arduino:

  1. Open a text editor with root privileges (e.g., nano):

    sudo nano /etc/udev/rules.d/99-arduino-auto-config.rules
    
  2. Paste in the following rule (replace 2341 and 0043 with your Arduino's VID/PID from Step 1):

    SUBSYSTEM=="tty", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0043", \
    SYMLINK+="arduino", \
    RUN+="/usr/local/bin/arduino_post_connect.sh"
    
    • SYMLINK+="arduino" creates a fixed /dev/arduino link that points to your Arduino's serial port (even if /dev/ttyUSB0 changes to ttyUSB1 later—super useful for your program to reference a stable path).
    • RUN+="/usr/local/bin/arduino_post_connect.sh" tells udev to run our custom script whenever the device connects.
  3. Save and exit nano (Ctrl+O, Enter, Ctrl+X).

Step 3: Write the Post-Connect Script

Now we need to create the script that resets the baud rate and restarts your program. Let's put it in /usr/local/bin/ (a standard spot for custom scripts):

  1. Create the script file:

    sudo nano /usr/local/bin/arduino_post_connect.sh
    
  2. Paste in this template (adjust the values to match your setup):

    #!/bin/bash
    
    # Wait a second to make sure the device is fully initialized
    sleep 1
    
    # Set the desired baud rate (replace 115200 with your needed rate)
    stty -F /dev/arduino 115200 cs8 -cstopb -parenb
    
    # Restart your dependent program—choose one of the options below:
    
    # Option 1: If your program runs as a systemd service (recommended for persistent programs)
    # systemctl restart my_arduino_program.service
    
    # Option 2: If your program runs as a regular user process (replace "pi" with your username, and the path/command)
    # su - pi -c "pkill -f 'my_program_executable' && /home/pi/path/to/my_program_executable &"
    
    # Option 3: If you just need to start it (no need to kill first)
    # su - pi -c "/home/pi/path/to/my_program_executable &"
    
    • The sleep 1 is important—sometimes the device isn't fully ready immediately after plugging in, so adding a short delay prevents errors.
    • The stty command sets the serial port parameters: adjust 115200 to your required baud rate, and tweak other flags if needed (cs8 = 8 data bits, -cstopb = 1 stop bit, -parenb = no parity).
    • Choose the right restart method for your program: systemd is the most robust way to manage persistent programs on Raspbian.
  3. Save and exit, then make the script executable:

    sudo chmod +x /usr/local/bin/arduino_post_connect.sh
    

Step 4: Test the Setup

Now let's activate the udev rule and test it:

  1. Reload the udev rules to apply changes:
    sudo udevadm control --reload-rules && sudo udevadm trigger
    
  2. Unplug your Arduino, then plug it back in. Check:
    • Does /dev/arduino exist? Run ls -l /dev/arduino to confirm it links to your ttyUSB device.
    • Is your program restarted? Check its process status (e.g., systemctl status my_arduino_program.service if using systemd).
    • Did the baud rate get set correctly? Run stty -F /dev/arduino to verify.

Troubleshooting Tips

  • If the script doesn't run, check udev logs with dmesg | grep udev to see error messages.
  • Make sure all paths in the script are absolute (e.g., /home/pi/program instead of program)—udev runs with a minimal environment, so relative paths might not work.
  • If your program runs as a non-root user, use su - [username] -c "command" to avoid permission issues.

内容的提问来源于stack exchange,提问作者tofgau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:57:42