I would like to see someone with a working example in bash reading and writing USB commands to Windows using this gadget and it will answer and help me learn.Unfortunately this Pi Zero device is pretty much useless for reading USB Messages and Writing USB Messages thru script for the following reasons:
As soon as I try to read in any kind of loop even with sleep 2s it "locks up the 1 cpu" and wifi never becomes available which means the device needs a complete sd card wipe and reinstall and redo all the work of the scrpts and retest - there needs to be a dedicated connection to the filesystem outside of ssh like on a mac i can touch the boot system, but not in windows....
HAS Anyone successfully connected a Raspberry Pi Zero in usb Gadget mode to a Windows PC and are able to send and receive USB messages over serial like xg_multi without the device freeze?![]()
![]()
![]()
I have tried examples such as:Code:
#log usb messages to output log sudo cat /dev/ttyGS0 > /usr/bin/xg_multi_output.log
Try tail -f instead of cat.
You also shouldn't need sudo and shouldn't be writing your log file to /usr/bin.also the while loop with sleep:Code:
while IFS= read -r line; do echo "Received: $line" sleep 2s done < /dev/ttyGS0
read -r blocks until it gets a line end. If you never send one it will block forever.I read about python scripts to do this instead of shell, tried to install pip failed to install pyserial library multiple ways
but was going to try to kick this script off after usb gadget for xg_multi was complete rather than inside the xg_multi in the bash script:Code:
import serialimport time# Configure the serial portserial_port = '/dev/ttyGS0' baud_rate = 9600try: # Initialize serial connection ser = serial.Serial(serial_port, baud_rate, timeout=1) time.sleep(2) # Wait for connection to settle print(f"Connected to {serial_port} at {baud_rate} baud.") while True: if ser.in_waiting > 0: # Read line, decode bytes to string, and strip whitespace line = ser.readline().decode('utf-8').rstrip() # --- PARSING LOGIC HERE --- print(f"Received: {line}") # Example: if "TEMP" in line: parse_temp(line) # --------------------------except serial.SerialException as e: print(f"Error: {e}")except KeyboardInterrupt: print("\nExiting...")finally: if 'ser' in locals() and ser.is_open: ser.close() print("Serial port closed.")
The sleep needs to be inside your loop and outside the embedded if block not before it starts. Have another look at my examples, above.
The problem, at least above, is your code.
As for installing things with pip, you need to either use a venv, disable venvs (not recommended), use the --break-system-packages option (also not recommended), or install from apt (prefix the module's name with python3- e.g. python3-pyserial but not all modules are available via apt).
Lastly, and this is not intended to be rude or deprecating, but I think you're in over your head and need to do some studying of Linux basics and basic programming techniques.
Does it need to be a separate shell script launched at boot ? I won’t have access to the device it needs to be all automated at power up.
Appreciate anyone’s advice.
Statistics: Posted by mrkell28 — Thu Jan 29, 2026 10:44 pm