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...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9). - Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array. - Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values...
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...
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
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.
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT