Hi,
I have connected Arduino to Raspberry Pi and I need read response from USB after send command, but I have problem.
Python code:
#!/usr/bin/python
from __future__ import print_function
###############################
# BeagleBoard
# Decimal VendorID=6790 & ProductID=29987
# Hexadecimal VendorID=0x1a86 & ProductID=0x7523
###############################
import sys
import usb.core
import usb.util
ard = None
arddev = None
ardendpoint= None
def ArduinoConect():
global ard
global arddev
global ardendpoint
try:
#device = usb.core.find(idVendor=0x0483, idProduct=0x5750)
arddev = usb.core.find(idVendor=0x1a86, idProduct=0x7523)
# was it found?
if arddev is None:
print("Arduino Error")
else:
print("Gotowy")
#raise ValueError('Device not found')
c = 1
for config in arddev:
for i in range(config.bNumInterfaces):
if arddev.is_kernel_driver_active(i):
arddev.detach_kernel_driver(i)
#print i
c+=1
# use the first/default configuration
arddev.set_configuration()
# first endpoint
ardendpoint = arddev[0][(0,0)][0]
# read a data packet
data = None
# get an endpoint instance
cfg = arddev.get_active_configuration()
intf = cfg[(0,0)]
ard = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ard is not None
# write the data
#ard.write('0')
except ValueError:
print("blad sprawdzenia")
ArduinoConect();
while 1:
val=raw_input("comenda: ");
ard.write(val + "\r\n")
# read a data packet
#print arddev
try:
data = arddev.read(ardendpoint.bEndpointAddress,ardendpoint.wMaxPacketSize)
RxData = ''.join([chr(x) for x in data])
print(RxData)
except usb.core.USBError as e:
data = None
print( e.args)
I get result:
root@beaglebone:/home/debian/budka# ./arduino-test.py
Gotowy
comenda: 2
(110, u'Operation timed out')
comenda: 2
(110, u'Operation timed out')
comenda: 1
(110, u'Operation timed out')
comenda: 1
LED_OFF
LED_OFF
LED_ON
LED_ON
comenda: 1
(110, u'Operation timed out')
comenda: 1
(110, u'Operation timed out')
comenda: 1
(110, u'Operation timed out')
comenda: 2LED_ON
LED_ON
LED_ON
LED_OF
comenda:
Respons should be after each command, but not working.
Any ideas?
Thanks
Dario