Python - Low Level USB Port Control - python

I want to make a simple python program, which controls my laptop's usb hubs. Nothing extra, just put first usb port's DATA+ channel into HIGH (aka 5V) or LOW (aka 0 V) state.

Python is way to highlevel for this problem, this behavior would require you to rewrite the USB Driver of your OS.

I don't think you can do this. Even if you knew how to write low-level drivers for your operating system, this is probably not possible because the data pins of USB ports are only designed to output voltages between 0 and 3.3 V. They are also designed to send and receive USB packets, not arbitrary voltages.
Instead, you should get a small programmable microcontroller with a normal USB interface and use its output pins.

Related

Pymodbus with Raspberry 4 PL011 uart4 and TTL/RS485 transreceiver

I'm trying to act as a master with my Pi 4 B in an rs485 Modbus communication.
I'll ask for register values.
All ok with pyModbus and a USB-RS485 (like ftdi) using its virtual com (/dev/ttyUSB0)
Now...
I summoned the uart4 with the standard procedure.
dtoverlay=uart4,ctsrts
So I'll be working with /dev/ttyAMA1, TX=GPIO8, RX=GPIO9, RTS=GPIO11, CTS=GPIO10
I bought one of those cheap TTL/RS-485.
DE and RE are connected to uart RTS (CTS enabled but not used).
TX to DI, RX to RO
I ask for 5 registers one at a time (with 3s timeout for the response each) and a final pause of 3 sec.
I connected some LEDs and slowed the baud rate to 1200 to see how LEDs act.
From the other side, I'm monitoring Modbus activity with ftdi USB-rs485 and dock light programmed to respond with a certain message (acts as a 'fake' slave)
No activity of the ftdi at all.
If I de-attach DE and leave only RE with RTS, I see the TTL TX message on the RX pin (like a looping echo).
What's wrong??
I bought one of those cheap TTL/RS-485. DE and RE together and connected to uart RTS (CTS enabled but not used).
That is not right. With Modbus only one device is allowed to use the bus at a time, so you need one of those GPOx (it can also be RTS or CTS) to go low right before the device is ready to write something on the bus and stay low until the whole frame is transmitted. After a determined amount of time elapses, the signal has to go high to free the bus and allow other devices to talk.
For the practical part, since this is a very common problem, you will find all you need to fix your problem in this Q&As. A very short summary: you need to tweak whatever library you use to add the signaling to feed the drive enable/read enable pin on your TTL converter.
If you are wondering why the USB converter works and the TTL won't, the answer is very simple: your USB converter has this signal implemented in its hardware (you can read more details on the link).
The problem is not difficult to fix, but if you are short on patience or don't have time to waste, just take the fast track and buy a decent transceiver. There are many options, this is just one (I'm not affiliated with the manufacturer nor I make any profit off it). You can also stick to those connected through USB; most of them will work (not all though).
If you decide to implement the software solution be aware that you will be asking for real-time performance on a non-real-time platform, meaning that even though the solution will probably work almost 100% of the time, it is not guaranteed to always work. If your RPi is loaded with too much work, there might be instances where the signaling will not be properly timed and some frames might not get to the other side. You are warned: if your application is mission-critical in any way, just forget about it and buy a proper transceiver.

Directly send signals to USB using Python

How can I set HIGH or LOW to a usb port connections using Python.
This could be used in come custom usb device.
For Example,
Consider I have a LED connected to the usb port(DATA Line) .
Now through the code I want to blink it or control it.
Now this can be easily achieved by using any micro controller, Arduino, Raspberry Pi
But I want to achieve this with with a normal computer and python.
[EDIT]
Can I achieve this by making a C or C++ API and make a wrapper to use it in Python. Is yes then what will be the best way to achieve it?
NOTE :
My main objective isn't just blinking some LED. I just gave it as an example.
I want to be able to directly control the USB ports.
Quoting : https://www.cmd-ltd.com/advice-centre/usb-chargers-and-power-modules/usb-and-power-module-product-help/usb-data-transfer-guide/#:~:text=How%20is%20data%20sent%20across,amounts%20known%20as%20'packets'.
Within the standard USB 2.0 connector you can see four metal strips. The outer two strips are the positive and ground of the power supply. The two central strips are dedicated to carrying data.
With the newer USB 3.0 connector, the data transfer speed is increased by the addition of extra data-carrying strips; four extra signalling wires help USB 3.0 achieve its super speed.
I want to set the values of the Data pins.
By my saying HIGH LOW please don't misunderstand that I want to set the value to +5V and GND. But I mean to control its value directly via my code without any external driver present in the computer.
I mentioned HIGH LOW to just make the language simple and so that it is easier to understand.
Controlling components like LEDs from devices like Arduino and Raspberry Pi are done using GPIO pins. In contrast, USB is designed for data transfer and not for maintaining more constant high or low signals like GPIO. It also consumes different voltage and current levels than GPIO pins and could potentially damage components meant for GPIO.
However, you can get USB to GPIO adapters for this purpose (see here for an example discussion on these options).
In terms of Python, you can use packages such as PyUSB or the libusb Python wrapper to control/transfer data/communicate with USB devices (such as the USB to GPIO adapters). The companies providing the adapters might also have designed their own easy-to-use Python package to wrap around a lower-level driver (probably written in C). See this USB controlled LED device as an example with its own Python driver. The drivers are just software programs that take in relatively simple commands from the user for what they want a device to do. They then encapsulate the lower-level complexities required for following a protocol to communicate the user's intention to the USB device and controlling the USB port at the lowest possible software level.
You cannot achieve that for several reasons:
The USB port has its own protocol of connection. Data is transmitted in packets with starting and ending bits. The negotiation and handshake process is done in the hardware layer between microchips. This process also selects the communication speed in the bidirectional data line. You have to direct access to the pin (like GPIO) to turn LEDs ON and Off or create your own connection protocol. This cannot be done in USB.
There are also voltage and current limitations. The data line is not +5 and GND. The data line is 2.8v for D+ and 0.3v for D- and both with respect to the GND. The data is transmitted and received differentially (D+ with respect to D-) and they are not compared with the GND for 1s and 0s.
The button line is you have no direct control over USB.

Raspberry Pi Keyboard Splitter

I would like to program a Raspberry Pi to take a single keyboards inputs in one USB port and then output through the Pi's two other USB ports in order to control two Macs at once with one keyboard.
I'm very new to Python. Which functions and commands do I need in order to program this function to my Pi?
I've been searching for the proper command, but I came here for help.
This isn't exactly something easily done with Python. This is because you're delving into developing device drivers on your Macs' side. Please allow me to explain:
Assuming you're using Raspbian on the Raspberry Pi (a flavor of Linux, and thus a Posix system onto itself), you would need to read the appropriate /dev/tty* file which maps to your keyboard first, and then appropriately convert and write out to the appropriate /dev/usb* files. Do note, most computers also send handshakes back and forth to the USB devices (for example, to register them on their own USB busses).
To be frank, you'd probably have better luck splicing wires from your keyboard, and connecting it to another USB male-socket, and then plugging both male-socket ends into your Macs.

Can a device have two UMDF Drivers?

This may seem like a bit of an odd question, but I was wondering if it is possible to associate two drivers with a peripheral device?
The reason I ask is that I am building an input device for Maya using an Arduino microcontroller. The Arduino already has its own device driver, but I was thinking of developing a UMDF driver to take the data that comes in from Arduino over the serial port and pre-process it ready to go into Maya.
Right now, I have two Python programs running - a 32-bit Python program running outside of Maya which listens to the serial port and converts the data to a form which a second 64-bit program inside the 64-bit version of Maya can understand and use in the Maya scene. This works fine, but it is a bit annoying having to start that external server program every time I want to use this device in Maya. If I can have a UMDF driver ready to jump into action when the appropriate type of data comes in off the Arduino then this would help immensely. Will this approach work?
It's more a comment/suggestion than an answer, but maybe it would be worth to invest some time and check if the filter driver would do the job for you. In WDM you can put it above the kernel device driver on the driver stack for that device, and use it to pre-process your device data. I think it is also possible in UMDF.
See Creating a New Filter Driver (MSDN).
You may always try to use Teensy/Teensyduino instead of Arduino to implement a virtual keyboard, mouse, joystick or other HID device. This does not require Windows drivers, and accessing the keyboard or joystick from Maya may be easier that the serial port.

Controlling electronic components with USB?

I want to try something that I imagine is somewhat simple. I want to have a USB cable with an LED on it, and a python script that would have 2 possible arguments: "on" and "off". This would toggle the LED on and off (obviously). So, anyone who knows software know which libraries (if not libusb) to use and how to use them, and hardware people, know how I might make said circuit? I don't want to turn the USB's power supply off, I just want to change something in the circuit to disable the LED.
USB is a serial protocol; you can't just set it "on" or "off", the way you used to be able to set a parallel port pin.
You need something (ie an embedded microprocessor) on the far end; the computer talks to the microprocessor which drives the LED.
Take a look at an Arduino; they are cheap, readily available, and easy to use.
Edit: try the Teensy - completely Arduino-compatible, $16, just over an inch square, has a USB port and 25 I/O pins.

Categories