So I have 2 different Maxbotix USB sensors connected to a USB hub. They enumerate as /dev/ttyUSB0 and /dev/ttyUSB1. I'm trying to cycle through both sensors using this code:
If I manually put in /dev/ttyUSB0 and /dev/ttyUSB1 then it works but I'm trying to cycle through several ports. I plan to add up to 6 sensors through the USB ports.
#!/usr/bin/python3 # sample script to read range values from Maxbotix ultrasonic rangefinder from time import sleep import maxSonarTTY serialPort = "dev/ttyUSB" maxRange = 5000 # change for 5m vs 10m sensor sleepTime = 1 minMM = 9999 maxMM = 0 port = 0 while port <= 1: mm = maxSonarTTY.measure(serialPort + str(port)) print(str(port) + " Value: ", mm) port = port + 1 sleep(sleepTime)I get this error:
Traceback (most recent call last): File "/home/pi/Documents/Python Projects/PyMaxBotix-master/rangeBox2.py", line 21, in <module> mm = maxSonarTTY.measure(serialPort1) NameError: name 'serialPort1' is not definedIf I try this:
#!/usr/bin/python3 # sample script to read range values from Maxbotix ultrasonic rangefinder from time import sleep import maxSonarTTY serialPort = "dev/ttyUSB" maxRange = 5000 # change for 5m vs 10m sensor sleepTime = 1 minMM = 9999 maxMM = 0 port = 0 while port <= 1: print(serialPort + str(port)) port = port + 1 sleep(sleepTime)I get this which appears to work:
>>> ================================ RESTART ================================ >>> dev/ttyUSB0 dev/ttyUSB1 >>>
If I manually put in /dev/ttyUSB0 and /dev/ttyUSB1 then it works but I'm trying to cycle through several ports. I plan to add up to 6 sensors through the USB ports.