![]() |
Help with code to access USB port - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with code to access USB port (/thread-3811.html) |
Help with code to access USB port - donmerch - Jun-27-2017 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: #!/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. RE: Help with code to access USB port - Larz60+ - Jun-27-2017 You may want to take a look at this package: https://pypi.python.org/pypi/libusb1/1.6.4 Even if you don't use it, you may get some ideas from looking at the code. RE: Help with code to access USB port - Ofnuts - Jun-27-2017 I don't understand how this: serialPort = "dev/ttyUSB" port = 0 while port <= 1: mm = maxSonarTTY.measure(serialPort + str(port))Can make your code reference a variable named serialPort1 (and I understand even less why in that case it doesn't complain first about serialPort0 ). Is there a serialPort1 variable hiding somewhere?
|