Python Forum
Help with code to access USB port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with code to access USB port
#1
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 defined
If 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.
Reply
#2
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.
Reply
#3
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?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 3,450 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,500 Sep-09-2021, 03:15 PM
Last Post: Creepy
  Erreur Code access violation reading 0x00000004 yan_mhb 0 2,348 Jul-10-2020, 02:28 PM
Last Post: yan_mhb
  Multiple process access to a serial port mkonnov 0 3,075 Apr-14-2019, 12:42 PM
Last Post: mkonnov
  Cant find S3 Read or Write access from Python code tpc 0 2,392 Apr-19-2018, 04:00 AM
Last Post: tpc
  Error in running MS Access Database connection code pyuser1 4 7,722 Feb-01-2018, 08:28 PM
Last Post: pyuser1
  Python code with serial port and global undefined marciokoko 13 14,556 Jan-17-2017, 06:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020