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.
Related
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.
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.
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.
I am trying to connect the Black Swift with an display from Parallalax[1]. I have been reading lots of tutorials about that but with the displays being used on the tutorials there is always a difference to the pins on my display. The displays that are being used usually have lots of pins where you can choose the ones you need, but the display I have just has 3 pins (5V, GND, RX). Now I assume that with this RX pin I can control the display (like when I use lots of pins on other displays). But I dont know where to connect that RX pin on my Black Swift. The next thing is,
I have also read the manual on that display I have (the link I provided) but the examples they use there are all built up on some boards that are being sold on the website (for example nothing about how to connect a raspberry or arduino). When reading a tutorial everything seems pretty easy, but I guess I didnt choose my hardware wisely :(
Now my question, since I am new to this, is it possible to connect that display to some random microcontroller (in my case Black Swift) ? If yes, where should I connect that RX pin? What is this RX pin doing, (didnt find anything in the manual about that)? Since they are using some special way (PBasic?) to control the display (is inside the manual), is it be possible to control the display with pythong?
Thanks for the help
EDIT:
So far I have managed to turn on/off the UART mode, and it seems that this is not the issue bcz I am getting same results (weired characters on te display).
[1] https://www.parallax.com/sites/default/files/downloads/27979-Parallax-Serial-LCDs-Product-Guide-v3.1.pdf
The linked PDF tells the story pretty conclusively. You need a UART from your controller with the matching bps-rate as set up by the DIP-switches.
Following http://www.black-swift.com/wiki/index.php/File:BlackSwift-1.0-pinout.png you should connect GPIO10/UART_TX to the RX, and then program your uC in whatever language it supports.
This is the same question as in
Change keyboard locks in Python
or
How to change caps lock status without key press.
But there is a difference!!
I don't want to change the lights on /dev/console or /dev/tty but on a human interface device that is connected via USB and recognised as /dev/hidraw0.
To make it even harder, this is being used on a Raspberry Pi.
The method suggested in the other questions doesn't work.
Using library at Jakub Wilk's library at http://jwilk.net/software/python-keyboardleds, it is pretty simple:
lk = LedKit('/dev/input/event1')
lk.num_lock.reset()
lk.caps_lock.reset()
The trick is to determine which input device your keyboard is. To figure that out, you look at /proc/bus/input/devices. (Thanks to Jakub for this solution)
I tested this on two a keyboards:
PS1 keyboard connected via PS1 to USB adaptor with Holtek chipset (04d9:1400)
A cheap numeric keypad, also with Holtek chipset (04d9:1603), HT82M99E.
Result: It works for the former but not the latter.
Perhaps someone knows whether that's a driver issue or a problem of the hardware itself (not responding to requests from the driver).