I'm working on a project with Raspberry Pi. I have two identical keyboard emulator devices as inputs. In my program, I need to know which one gave the input. Is there a way to do this in Python? Thank you!
Do you have control over these devices? Could you change the USB protocol to something more reasonable, like a USB CDC ACM virtual serial port?
Do they have to by identical? If not, I would do something simple like have one of the devices only send capital letters and have the other device only send lower-case, but I guess that doesn't extend so well if you need to send a number.
With two keyboard emulators, you have to worry about what happens if the messages overlap. For example, if device 1 tries to type "banana" and device 2 tried to type apple, there is nothing to prevent your python program from reading something like "applbaneana".
Related
I am using Raspberry Pi 4 which comes with 4 USB ports. For my project, I have two mics and both will be connected to Rsp via a USB port. I need to be able to choose which Mic needs to be ON. So basically I need to be able to switch/choose which USB port I want the sound to come in.
I need to be able to choose Mic1 or Mic2. I am writing my code in Python. Is there any library that gives me access to choose a specific UBS port? I really can't find anything.
Appreciate your help.
Regards
So I fear this is going to be a no, but is there a way to have python 3 on windows 10, discover, connect and get subscription updates from a bluetooth Low Energy device? Bleak and other similar libraries do not seem to work.
If that is not a solution, is it possible that something like C++ would work? My end goal is to have something connect to a bluetooth device, the script/program to read what the device is sending it, then translate that into keyboard inputs.
I am willing to pick up another language to make this happen, just not really sure where to start.
I may be confusing a few concepts here so any help is appreciated.
Q1: Is it possible to attach any sensor in the world to the USB on my computer as long as it gives me analog data, and read its output? (e.g. pH, temperature, oxygen sensor etc as long as it gives me analog data)
Q2: If so, then what is the simplest way in python for me read such data.
Comment: I am trying to bypass using PLC's, and trying to see if I can get the output from the sensor directly to the PC. (I do not have drivers for these sensors)
Actual Need: I have an oxygen sensor connected to my computer via a USB. The oxygen sensor is able to send out analog data. The obvious way is to go through a PLC. However, I would like a solution which by-passes PLC's so I can connect the sensor directly to my PC via USB.
The USB port on your computer cannot read analog data because USBs work with digital signals. You would need an analog-to-digital converter (ADC).
as #digitaLink answered, it is not possible directly via USB and yes, the obvious way is to use a PLC.
I would go the PLC way - in fact, I did it a few times in the past - and start with an Arduino and later develop a custom PCB, put it in a box and done.
Another possibility is to use a raspberry pi (or similar SBC), which has the GPIOs you can use for analog read.
Edit: there is another possibility.
The sensor you use now is _very_likely_ a PLC in itself, that is, the sensor is attached to a microcontroller that uses the USB port for serial communication. Now, the drivers you are missing do nothing else but decode the data coming through the serial port. Take a look inside your harware and try to find out what components there are in.
So what you could do is to try to find out how to communicate with the sensor via a serial terminal. It is probably possible to monitor serial communication (although I must admit, I don't know how to do that), reverse engineer the code and write your own driver in python. You could learn a lot from this, even if you don't succeed.
The solution to this problem is probably pretty simple, but I am new to interfacing with a device dynamically. What I'm doing is I am making a python executable code, so the user doesn't have to have Idle on their computer or any kind of python interpreter, which means I don't know which USB port the device will be plugged in to. The program needs to be able to open a connection to a device that is connected through a serial to usb converter. How can I determine which connected device is the correct device to open a port to? I am using pySerial to interact with the device. Any help would be greatly appreciated.
No matter how you configure your device, at some point you're probably going to have to ask the user where the port is, or poll all serial devices for a known response. (Polling has it's pitfalls though, so read on!). Unlike USB devices, there is no vendor/device ID that is made known to the OS when you attach a plain-old serial device.
First you need to find the serial ports. Here's a question that might help: What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?.
Once you have a list of serial ports, you could ask the user whether they know which one to use. If they do, problem solved!
If they don't, you could offer to poll ALL serial devices with some data that you know will yield a certain response from your device. Keep in mind though that if the user has other serial devices attached, your string of "hello" bytes might actually be the self-destruct sequence for some other device! Hence, you should warn the user that polling may interfere with other devices, and always prompt them before you do so.
Without knowing more about your code (eg. what comms framework, if any, you're using; are you doing this in the console or are you using a GUI toolkit, etc), it's impossible to say what the best way to code this might be. In the simplest case, you could just loop over all of your serial devices, send the greeting and check for a response. (You could also do this in parallel: loop once for the greeting, and loop again to check what's in the buffer. If you're going to get more fancy than that, use a proper library.)
Side note: You might be able to get around this if you have a built-in converter that you can set the vendor/device ID for, but the converter will still be automatically detected by any modern OS and enumerated as a serial port; you won't get to talk to it directly as a USB device. It could be possible to figure out which port goes with which ID, but I've never tried to do that. But this approach is useless if you're not the one who gets to pick the converter (eg. if it's a user-supplied cable).
This is the way that I used for identify Serial-USB conveter device like Arduino.
It checks 'USB' String of device description.
import serial as ser
import serial.tools.list_ports as prtlst
global COMs
COMs=[]
def getCOMs():
global COMs
pts= prtlst.comports()
for pt in pts:
if 'USB' in pt[1]: #check 'USB' string in device description
COMs.append(pt[0])
The only way I can think to get around the problem with probing unknown devices is to have the device send unsolicited "hello" responses continually. That was you can just connect to all serial devices and listen for the "hellos". Connecting and listening to a serial device shouldn't ever mess it up.
The downside is you have these messages cluttering up your serial stream. You could have a "I'm here now, stfu" command but then you can only connect to it once.
FTDI chips have a method of identification but you have to use their library to access the data.
I would like to know if the following is possible:
I want to connect my computer to either another computer, or a some consumer electronics (like a ps3 or xbox or something), via a double sided USB cable. I want to run a program on the first computer that will run a constant data stream through the USB cable, to trick the second computer into believing it is a usb flash drive, and it can read data from it. The first computer can change the data stream accordingly to the files that are supposed to be on the emulated flash drive.
Essentially, I want to use a program on a computer to mimic USB hardware in another device.
I don't know if I am wording this is a proper way or not, but is this possible?
Diagram:
| My Computer running this program | >-----emulated USB data stream-----> | Target |
This isn't possible without special hardware because USB doesn't support peer-to-peer networks. You might do better with Firewire (IEEE-1394), which can at least be used for TCP/IP with appropriate drivers.