In this article, we will learn how to detect USB ports or devices in Pi automatically. So, you do not need to search from the command box. This simple line of code helps a lot while working on projects. Anyway, let’s start.

Disclaimer:

Electricity poses inherent risks. Adequate skills are crucial for handling it safely. Undertake tasks at your own risk; the author disclaims responsibility for misuse, harm, or errors. Website content is unique and copyrighted; refrain from unauthorized copying. Most articles are open-source for your benefit. Feel free to use the knowledge freely, and consider purchasing available resources. For assistance or guidance, comment below; the author aims to help. Some articles may contain affiliate links, having no impact on you but supporting the author with a commission. Thank you for your understanding.

Advertisements

Table of Contents

Manual way:

There is a manual way to detect the port from ‘sudo‘ command. Write ‘ls -m /dev/tty*‘ then the cmd pad will give you something like this:

Detecting USB Port or devices in Pi

From this, you can identify your port manually. How? Simply plug the device and write the command, and then unplug the device and do the same as before. From the difference, you can identify the device. But doing this each time while you are writing a code in Py, will be painful. So?

There is a way to find the port automatically and let the code find the exact port you need to detect.

You can check my other articles that can help you:

Automated way:

When wringing a code, just keep this part to detect the port.

import serial
import time
import serial.tools.list_ports
GSM_port_name = 'CP2102'
port_range = 20

def find_port(cnt_p,name):
    port=''
    print('Finding port for GSM')
    while(cnt_p):
        cnt_p-=1
        p=serial.tools.list_ports.comports()
        n=len(p)
        for i in range(0,n):
            des=p[i][1]
            if name in des:
                port = p[i][0]
                cnt_p=0
                print('Found Port for GSM:'+str(port))
                break
            else:
                print('.\n')
        time.sleep(0.5)
    return port

Now call this ‘find_port()’ later on your main code.

p = find_port(port_range,GSM_port_name)
ser = serial.Serial(p, 9600, timeout = 1)#open USB serial

I had a GSM hat device to connect with the Pi through the USB port. And with this code, I can detect that port very easily.

Test Result:

Detecting USB Port or devices in Pi

End:

In the end, is this helps you? If yes, then visit more again to find more helpful articles. Thanks.

For Professional Designs or Help:

Loading


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *