How to Detect event generated by USB device into raspberry pi - python

Hello geniuses i want to only detect the event generated by a usb device attached to it like barcode scanner.Now want that a barcode scanner is plugged in to a usb port of a raspberry pi.now when it scan then how can my rpi detect that a usb device has generated a event.

Hopefully PyUSB works, and in that case you can follow this guide
If for whatever reason that doesn't work, you could open the /dev/tty device direclty with a standard:
with open('/dev/tty4', 'rb') as fh:
for event in fh.read(8)
Note that /dev/tty is just a out-of-the-hat example, your device might end up somewhere else. Check dmsg and lsusb to determine where your device got mounted, if at all mounted or discovered. You might need specific drivers for your scanner.

Related

How to connect to USB TTY from BeagleBone Black?

I would like to establish serial communication from a Windows PC to the BeagleBone Black. The purpose is to have a Python application on the BeagleBone interact with a terminal running on the PC. I would like to use the same USB cable I use for the SSH connection (from PC to micro-usb on the BB), but instead of SSH, I want to open a serial connection via something like TeraTerm, and have a running python application connect to the TTY, something like /dev/ttyUSB. However, this TTY does not exist.
Note that I'm not trying to do serial debugging, as this article suggests. I'm also not trying to do this connection over UART. I specifically want to communicate with a python application via a terminal running on the PC. The Python application would use a library like pyserial.
Since the TTY is not showing on the BeagleBone, how can I proceed?
A TTL to USB converter will work. I got an older model that is still produced from some company. Here is one I found easily online at Adafruit: https://www.adafruit.com/product/954.
That will give you TX, RX, GND, and PWR. Please make sure to get a 3.3v type if you are using it for the BBB.
Here is some write up w/ questions/answers: https://askubuntu.com/questions/40959/how-do-i-connect-to-tty-com-dev-ttyusb0
Sometimes when you use PuTTY w/ Win 10 COM ports, it is necessary to make sure you get the COM port correct. Actually, every time! Click on the connection ICON on the bottom, right of the screen under the "^", click on the Open Devices and Printers, and then go to the BBB on the photo screen. Once you right click and go to Properties, finally click on the Hardware tab to find your COM Port. Then, one can use a COM Port on Win 10 w/ the BBB along w/ PuTTY.
So, w/ PuTTY, you can click the serial connection, type your COM Port (COM6 or whatever), and finally click Open.
...

How can I capture events from Flirc USB (HID Device) using Python (on Windows)?

My goal is simple. It is to use a FLIRC USB device connected to my windows laptop. I then want to point an Infrared remote control to it and press each button and have a Python script listen for each time a button is pressed on the remote i.e listen for any event on the FLIRC USB.
So far I understand that that the FLIRC USB device is a HID device. I am able to grab the vendor ID and product ID of the HID device using the following script using the pywinusb module:
import pywinusb.hid as hid
all_devices = hid.find_all_hid_devices()
flirc_hid_devices = []
for device in all_devices:
if "flirc.tv" in str(device):
flirc_hid_devices.append(str(device))
print(flirc_hid_devices[0])
The good thing is that I am able to identify the FLIRC USB's vendor and product ID (as it is a HID device).
Where I am stuck is I don't know how to proceed from here. Any ideas would be appreciated.

How to read file input from a text file in pc to arduino

So, basically I have a file on my pc which contains some statements (string). I want to send those strings character by character to arduino uno. fopen() doesn't work in arduino (if that's what i read is correct).
I don't know if serial port will work as taking input from a file on my pc.
Is there a way to send character by character data to arduino?If yess, then please guide me.
If data can be sent through python and/or command prompt(terminal) please tell me how to do it.
Thanks in advance.
There are countless ways to communicate with an Arduino, given you have the respective shields. Wifi, Ethernet, Bluetooth, ...
You can use a USB to Serial converter, connected to the Arduinos Rx/Tx pins. Most if not all Arduinos come with an integrated USB to serial converter onboard. Connect the Arduino to your PC with a USB cable. Make sure you have the drivers for that USB-serial converter installed and you're ready to go.
All you need to know is how to send data from your PC through the virtual COM port of that USB-serial device and how to read that data within your Arduino sketch.
Then you open a file on your computer and send it to the Arduino byte by byte.
There are countless tutorials available. Just search the web for "Arduino serial" and "Python serial" in case you want to do the PC part in Python.

How to send commands from Python 3.4 to a Motorized XY Microscope Stage with db15 input socket

I have a Motorized XY Microscope Stage which is currently controlled by a joystick, the joystick signals are transformed into data and sent to the motors in the XY Microscope Stage through a cable which is a db15. I need to move it with commands from my computer, replacing the joystick.
I also have a USB to DB15 converter "USB Game Port Adapter Rockfire RM-203" that I got from here
http://www.rockfire.com.tw/en/products/accessory/usb-accessory/7-rm-203-usb-nest-converter.html
which creates in Human Interface Devices a HID-compliant game controller and a new USB input device
I am having some issues sending a command (lets say moving a motor from position 0 to position 100 in X axis) from my laptop to the XY Microscope Stage, I do not know how to send the command from Python 3.4 to the motors.
As I have this HID, I can use pywinusb 0.4.1 from here https://pypi.python.org/pypi/pywinusb/, Docs here https://github.com/rene-aguirre/pywinusb, but I cannot see how to sent a coomand to the HID
I think I have to identify and open the HID port, then write the commands and send them through, so the motors will move, like I did with a serial port which was using instead of HID a COM port, and It was way much easier than this
Can somebody help me
Thanks
Edit
I have check some information online but there is little or no examples about it. Here is an approach that I tryed
How to send hid data to device using python / pywinusb?
then it did not work so I breaked into pieces and tryed this
from pywinusb import hid
filter = hid.HidDeviceFilter()
print (filter)
hid_device = filter.get_devices()
print (hid_device)
if hid_device:
print("Found %d matching hid devices" + str(len(all_devices)))
else:
print("no hid devices")
hid_device.open()
And I got this
<pywinusb.hid.core.HidDeviceFilter object at 0x0000000003FC0240>
{}
no hid devices
Traceback (most recent call last):
File "C:/Users/me/Desktop/other.py", line 15, in <module>
hid_device.open()
AttributeError: 'dict' object has no attribute 'open'
So sadly, even though the keyboard, mouse and USB to DB15 cable are connected, and I can see they are connected on the Device manager, this program does not recognise them.
Here other examples
https://stackoverflow.com/search?page=1&tab=relevance&q=pywinusb
https://stackoverflow.com/questions/12802401/simple-reading-writing-from-to-a-usb-hid-device-in-python
http://cooder.org/questions/18709253
https://github.com/jrowberg/pywinusb/commit/8a0c7ac0aca6ac66eb944520a2b5e4c538f06a8c
http://www.developerfusion.com/project/31693/pywinusb/
http://nullege.com/codes/show/src#p#y#pywinusb-0.3.3#examples#pnp_qt.py
http://www.developerfusion.com/project/31693/pywinusb/
https://developer.mbed.org/cookbook/USBHID-bindings-
Still, I do not know how to even recognize the cable in Python 3.4
please help
Nevermind, I was trying to control the MAC5000 from this link
http://ludl.com/?portfolio=mac-6000
I have emailed the manufacturer, and they said that probably after all the test performed I have to use the "control box" of the device and not trying to control it directly from the db15 port. although such db port has 15 pins I can not use USB to GPIB or USB to DB15 adapters, python packets pyvisa and pywinusb respectively. this control box has a serial port so I will use pyserial to send commands and get reply messages from the MAC5000.

Linux USB Mapping Question

I'm working on a utility that will auto mount an inserted USB stick on linux. I have tied into D-Bus to receive notification of when a device is inserted, and that works great. However, I need to determine which device in /dev is mapped to the inserted USB stick. I am getting the D-Bus notification and then scanning the USB system with pyUSB ( 0.4 ). I filter for USB_MASS_STORAGE_DEVICE classes, and I can see the device that's been added or removed. I need to mount this device so I can query it for available space and report that to our app so we can determine if enough free space exists so we can write our data.
I'm using python for this task. I'm not sure what our target distro will be, only that it will be at least 2.6
edit: My question is: How do I determine which device in /dev maps to the buss-device number I get from pyUSB.
You should probably ask HAL about that. You say you already get notifications from HAL by D-Bus... It maintains list of USB devices, together with their IDs and device names (block.device property).
Here's a nice example of how to get device file name together with the notification of new USB device: How can I listen for 'usb device inserted' events in Linux, in Python?
Why not use "os" module to mount the file system:
os.system ("mount ... ")
Or if you want to examine output use "popen":
l = op.popen ("mount ....").readlines()
what about using dmesg output to find out the device name (sdc1 etc...)
use it right after dbus tells you something is was inserted in USB. you could do tail dmesg for example

Categories