Question

In: Computer Science

Develop a program using python to demonstrate data exchange using USB protocol.

Develop a program using python to demonstrate data exchange using USB protocol.

Solutions

Expert Solution

To do such communication , we have to use Python library.
We will use pyusb for this. Below is the commnad to install it.

pip install --user pyusb

usb.core is the main part of pyusb. Most of the programs starts by importing it. 

Below is the working command as question doesn't describe with which product USB should be workign. 


#!/usr/bin/env python2

# Send a BEEP command via USB to a Lego Mindstorms EV3 brick

import sys
import usb.core                 # https://walac.github.io/pyusb/

def ev3_write(command):
    # To send commands, we need an Endpoint.

    # To get to the endpoint we need to descend down the hierarchy of
    # 1. Device
    VENDOR_LEGO = 0x0694
    PRODUCT_EV3 = 5
    # 2. Configuration 
    CONFIGURATION_EV3 = 1       # 1-based
    # 3. Interface
    INTERFACE_EV3 = 0           # 0-based
    # 4. Alternate setting
    SETTING_EV3 = 0             # 0-based
    # 5. Endpoint
    ENDPOINT_EV3 = 1            # 0-based

    # 1. Device
    device = usb.core.find(idVendor=VENDOR_LEGO, idProduct=PRODUCT_EV3)
    if device is None:
        print("Is the brick connected and turned on?")
        sys.exit(1)

    # By default, the kernel will claim the device and make it available via
    # /dev/usb/hiddevN and /dev/hidrawN which also prevents us
    # from communicating otherwise. This removes these kernel devices.
    # Yes, it is weird to specify an interface before we get to a configuration.
    if device.is_kernel_driver_active(INTERFACE_EV3):
        print("Detaching kernel driver")
        device.detach_kernel_driver(INTERFACE_EV3)

    # 2. Configuration
    # A Device may have multiple Configurations, and only one can be active at
    # a time. Most devices have only one. Supporting multiple Configurations
    # is reportedly useful for offering more/less features when more/less
    # power is available.
    ## Because multiple configs are rare, the library allows to omit this:
    ## device.set_configuration(CONFIGURATION_EV3)
    configuration = device.get_active_configuration()

    # 3. Interface
    # A physical Device may have multiple Interfaces active at a time.
    # A typical example is a scanner-printer combo.
    #
    # 4. Alternate setting
    # I don't quite understand this, but they say that if you need Isochronous
    # Endpoints (read: audio or video), you must go to a non-primary
    # Alternate Setting.
    interface = configuration[(INTERFACE_EV3, SETTING_EV3)]

    # 5. Endpoint
    # The Endpoint 0 is reserved for control functions
    # so we use Endpoint 1 here.
    # If an Interface uses multiple Endpoints, they will differ
    # in transfer modes:
    # - Interrupt transfers (keyboard): data arrives soon, with error detection
    # - Isochronous transfers (camera): data arrives on time, or gets lost
    # - Bulk transfers (printer): all data arrives, sooner or later
    endpoint = interface[ENDPOINT_EV3]

    # Finally!
    endpoint.write(command)

beep_command = \
    '\x0F\x00\x01\x00\x80\x00\x00\x94\x01\x81\x02\x82\xE8\x03\x82\xE8\x03'
ev3_write(beep_command)

Above program is taken from : http://mvidner.blogspot.com/2017/01/usb-communication-with-python-and-pyusb.html


Related Solutions

How to use Bluetooth to exchange Data between 2 computers. Develop a program with Python to...
How to use Bluetooth to exchange Data between 2 computers. Develop a program with Python to demonstrate this data exchange.
Develop a C++/Python program using visual studio connected to mysql workbench to show all vendor's name...
Develop a C++/Python program using visual studio connected to mysql workbench to show all vendor's name and phone (vendor_name and vendor_phone) in the state "CA" and the city "Los Angeles". here is the database tables CREATE TABLE general_ledger_accounts ( account_number INT PRIMARY KEY, account_description VARCHAR(50) UNIQUE ); CREATE TABLE terms ( terms_id INT PRIMARY KEY AUTO_INCREMENT, terms_description VARCHAR(50) NOT NULL, terms_due_days INT NOT NULL ); CREATE TABLE vendors ( vendor_id INT PRIMARY KEY AUTO_INCREMENT, vendor_name VARCHAR(50) NOT NULL UNIQUE, vendor_address1...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
TheMathGame Develop a program in PYTHON that will teach children the basic math facts of addition....
TheMathGame Develop a program in PYTHON that will teach children the basic math facts of addition. The program generates random math problems for students to answer. User statistics need to be kept in RAM as the user is playing the game and recorded in a text file so that they may be loaded back into the program when the student returns to play the game again. In addition, the youngster should be allowed to see how (s)he is doing at...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and screenshot of running the program quiz could be of anything like , what's the sum of 2&2. There should be number of attempts(limited) suppose if the answer is wrong.
This program is in Python ### simulates packets of data arriving at irregular times. The data...
This program is in Python ### simulates packets of data arriving at irregular times. The data gets ### read from the file and then you must place it in a queue where it will wait ### its turn to get processed. The "processing" happens in a threaded subroutine ### called "process_input_data". That function should pull data from the queue, ### hash the student ID number, and then store the data in a hash table which ### you have to implement....
What are the pros and cons of data exchange using pipes/sockets vs data exchange using shared...
What are the pros and cons of data exchange using pipes/sockets vs data exchange using shared memory.
What are the pros and cons of data exchange using pipes/sockets vs data exchange using shared...
What are the pros and cons of data exchange using pipes/sockets vs data exchange using shared memory.
Develop a python program that prompts the user to take up the quiz (which has choices)...
Develop a python program that prompts the user to take up the quiz (which has choices) with limited attempts and limited time. Suppose if the user answered wrong then they have to get numer of attempts(limited) Quiz should have questions on General knowldge & multiple choices for those questions
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data...
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data from a CSV file, which has no names, just numbers. All I did was to read a .csv file. How do I pull data from three columns which contains about 1500 rows with just numbers and make a scatter plot with two in the x-axis and the third in the y-axis?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT