PySerial Input/Output Error排查:SSC-32舵机控制器四足机器人串口问题
Alright, let's tackle this serial port access headache with your SSC-32 quadruped robot on a Linux VM running on Mac—this setup has a few predictable gotchas, but we can work through them step by step.
The biggest issue with VM serial access is making sure your USB-to-Serial adapter is actually being passed through to the Linux guest, not stuck on the Mac host. Here's what to check:
- If you're using VMware Fusion/Parallels, look for the USB icon in your VM's toolbar. When you plug in the adapter, it should pop up as an option—select it to connect it to the Linux VM.
- Some VMs don't auto-reconnect devices after unplugging, so you'll need to manually re-select the adapter from the USB menu every time you plug it back in.
Linux locks down serial ports by default, so your user account probably doesn't have permission to access /dev/ttyUSB* or /dev/ttyACM* (the typical device names for USB-to-Serial adapters). Fix this by adding yourself to the dialout group:
sudo usermod -aG dialout $USER
You'll need to log out and back in for this change to take effect. To verify, run ls -l /dev/ttyUSB0 (replace with your actual device name) and check that the group is listed as dialout.
When you unplug and re-plug the adapter, Linux might assign it a different device name (e.g., /dev/ttyUSB0 becomes /dev/ttyUSB1). Stop hardcoding the port path in your code—instead:
- Use
dmesg | grep ttyright after plugging in to see the exact device name the system assigns. - In Python, use PySerial's built-in port enumeration to auto-find the adapter:
Look for entries mentioning "USB Serial" or similar to identify your adapter.from serial.tools import list_ports ports = list_ports.comports() for port in ports: print(f"Found port: {port.device} - {port.description}")
Here's a robust code snippet that handles port detection, error handling, and proper serial setup (adjust baud rate to match your SSC-32's default—usually 9600):
import serial from serial.tools import list_ports def find_ssc32_port(): # Scan for USB-to-Serial ports for port in list_ports.comports(): if "USB" in port.description or "Serial" in port.description: return port.device return None try: port_path = find_ssc32_port() if not port_path: raise Exception("Could not find USB-to-Serial adapter") # Initialize serial connection with SSC-32 settings ser = serial.Serial( port=port_path, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_1, bytesize=serial.EIGHTBITS, timeout=2 ) print(f"Connected to SSC-32 on {port_path}") # Test with a simple command (move servo 1 to neutral position) ser.write(b"#1 P1500 T1000\r\n") # Always close the port when done ser.close() except serial.SerialException as e: print(f"Serial communication error: {str(e)}") except Exception as e: print(f"Error: {str(e)}")
Key notes:
- Always handle exceptions to get meaningful error messages (e.g., "port already open" or "permission denied").
- Explicitly close the serial port when finished to avoid locking it up for future attempts.
- If you're using VMware Fusion, go to VM Settings > USB & Bluetooth and make sure "Share Bluetooth devices with Linux" is disabled (it can interfere with USB serial devices).
- For older USB-to-Serial adapters, set your VM's USB compatibility to USB 2.0 instead of 3.0—some adapters don't play nice with USB 3.0 passthrough.
- If the port still won't connect after unplugging, try restarting the serial getty service in Linux:
Replacesudo systemctl restart serial-getty@ttyUSB0.servicettyUSB0with your actual device name.
Before blaming PySerial, verify the port works with a simple Linux tool like screen:
screen /dev/ttyUSB0 9600
Once connected, type a test SSC-32 command (e.g., #1 P1500 T1000\r) and hit enter. If the servo moves, your system setup is good and the issue is in your Python code. If not, double-check permissions and VM passthrough.
内容的提问来源于stack exchange,提问作者Christopher C

