Files
sinqepicsapp/testEuroMoveUsb.py
2020-03-18 15:53:44 +01:00

89 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
"""
Created on Thur Feb 06 10:22:42 2020
@author: gaston emmanuel exil
"""
#pip install pyusb
#sudo cp 10-llbDevices.rules /etc/udev/rules.d/
#sudo udevadm control --reload-rules && udevadm trigger
#reboot
import sys
import usb.core
import usb.util
from time import sleep
# 1. Device
llbVendorID=0x04B4
llbProductID=0x1002
# 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
# find our device
device = usb.core.find(idVendor=llbVendorID, idProduct=llbProductID)
# was it found?
if device is None:
raise ValueError('Device not found')
sys.exit(1)
else:
if device._manufacturer is None:
device._manufacturer = usb.util.get_string(device, device.iManufacturer)
print("manufacturer: ", str(device._manufacturer))
if device._product is None:
device._product = usb.util.get_string(device, device.iProduct)
print("product: ", str(device._product))
# 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)
# claim the device
usb.util.claim_interface(device, INTERFACE_EV3)
# write endpoint
endpoint_BulkOut = device[0][(0,0)][0]
# Read endpoint
endpoint_BulkIn = device[0][(0,0)][2]
try:
command = "L"+chr(13)
assert device.write(endpoint_BulkOut, command.encode('utf-8'), 100) == len(command)
ret = device.read(endpoint_BulkIn, endpoint_BulkIn.wMaxPacketSize, timeout=30000)
print(ret)
#byte_str = "".join(chr(n) for n in ret)
print(ret.tostring())
command = "A1,4"+chr(13)
assert device.write(endpoint_BulkOut, command.encode('utf-8'), 100) == len(command)
ret = device.read(endpoint_BulkIn, endpoint_BulkIn.wMaxPacketSize, timeout=30000)
print(ret)
byte_str = "".join(chr(n) for n in ret)
print(ret.tostring())
except Exception as e:
print(e)
# release the device
usb.util.release_interface(device, INTERFACE_EV3)
# reattach the device to the OS kernel
#device.attach_kernel_driver(INTERFACE_EV3)