Go Down

Topic: Read from USB after write to USB (Read 111 times) previous topic - next topic

raczkowski1

Hi,

I have connected Arduino to Raspberry Pi and I need read response from USB after send command, but I have problem.

Python code:
Code: [Select]

#!/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:

Quote
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: 2

LED_ON
LED_ON
LED_ON
LED_OF
comenda:

Respons should be after each command,  but not working.
Any ideas?

Thanks
Dario

spycatcher2k

I have an idea - go to a python / Raspberry pi forum! This forum is for Arduino, not for code you write on another platform that happens to connect to an Arduino.
-=-
http://www.uk-pcb.co.uk - UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details. Arduino & C# PC programming.

Robin2

#2
Aug 13, 2017, 11:29 am Last Edit: Aug 13, 2017, 11:31 am by Robin2
Why are you not using pySerial ?

This Python - Arduino demo may be of interest.
 
I was recently fiddling around with direct writing to a USB device with Python. I believe a USB device has separate endpoints for sending and receiving.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up