Actual Python 3.10 async implementation of serial port/uart communication? - python-3.x

folks.
I'm writing some sort of scheduler that will send commands and receive responses to a hardware controller that is connected to the Linux-based system via a serial (UART) port. Currently it is FT232RL USB <-> UART bridge.
The scheduler itself is asynchronous and written using asyncio "in-code" Python module. There is no code that may block main loop. But here I'm facing the need of actually sending data to the controller via UART and receiving data from it. The "controller" will actually execute commands and perform actions with physical devices. The problem is that there is no "in core" standard Python module to communicate with external devices using serial ports. Especially the way asyncio does.
Is there is any actual maintained one that is supported by Python 3.10 that will allow me to make use of asyncio and make non-blocking data transmission via UART?
The pyserial-asyncio seems to be a good candidate. Any other modules to take a look at?

Related

Hardware communication with Python

Is it possible to write an API with Python so you can connect a physical ON and OFF switch via USB to a PC and when user presses the switch to ON or OFF, the python program detects it and send a signal to a web app and shows ON or OFF message on the website?
I am sorry if what I am asking its not clear enough!
Yes, it is possible. Reading USB devices can be done with Python. In linux USB device inputs can be found in some files(e.g. /dev/ttyUSB0). By reading those files you can get the information that you need. Putting here link that will be helpful
similar post
Firstly, you can't write an API to interact with hardware in python. You would have to use the pre-existing windows API(or the API provided by the Operating system that you are using) in order to interact with hardware in such a high-level language.
If you want to interact with hardware in python, and detect switch presses, releases etc, I would recommend you used a microcontroller such as a raspberry pi(for python) or an arduino(for C++). The respberry pi provides a very easy way to interact with hardware in python. If you still want to interact with a USB stick in python(but not acting as a switch) you can use the pyusb library.

Linux : how to transfer data through USB bulk endpoint of cdc-adm driver from userspace

I am a beginner to Linux drivers and I started with writing an application for a cdc-acm based USB device in linux. Therefore, I have used the cdc_acm driver. The USB device that I am using has 2 Bulk endpoints (read and write) and one interrupts endpoint.
Now, my question is whether all these endpoints operate on the same /dev/ttyACM0 file, and how does the write call on this tty file get convert into acm_write_bulk fops call?
If I write a data to trigger a USB functionality into the ttyACM0 file, will the data get sent through bulk out endpoint? Or how should I send the data to the bulk endpoint directly from user space. Should I write any supporting driver in kernel space?
Similarly, how do I read the data from interrupt endpoint in the userspace?
I appreciate your help in advance.
There is no need to write a kernel space driver. You can open /dev/ttyACM0 with the open system call, set parameters for it using termios (optional), and then use the read and write system calls to read and write bulk data from your device. These system calls are easiest to access from C and C++, but most languages have a library you can use to access serial ports.
I don't know of a great way to read data from the interrupt endpoint in Linux but you can at least look into the TIOCMGET, TIOCGICOUNT, and TIOCMIWAIT ioctls in you really need to do that.
The Linux serial port interface abstracts away all details about USB, endpoints, and bulk transfers, so you can use a simpler, more abstract API to communicate with the serial port. In fact, you can use the same code on any type of serial port, regardless of which kernel driver implements the serial port. It might help you to search the Internet for things like "linux serial port programming" or "posix serial port programming" to understand more about how to do this.
If you really are curious about how the Linux CDC ACM driver works, and how it converts a write system call into the corresponding USB transfer, you can read the source of the cdc-acm driver. However, that is way beyond what you need to do to simply use a serial port.

BCM socket cyclic reception in python

I'm trying to monitor a can bus through socketcan with Python. I'm taking as a reference the can4python package.
Since I want to continuously acquire data from the can socket, I'm thinking of using BCM sockets since it handles this on the Kernel level. In the can4python package I can only find periodic CAN transmission but no periodic can frame reception.
Is it possible to do this with can4python? If not is it possible to do it with BCM sockets in general?
Thank you for your help.
Just create a thread in Python that continuously reads CAN frames from the socket. If there are CAN frames you're not interested in just setup a CAN filter, so that SocketCAN subsystem would deliver only the required frames.
can4python project seems to be abandoned. Take a look at python-can project that is actively maintained.

Simulating Serial Port with node.js without using names pipes

I´m working on my own smart home system using some arduinos and Xbees, receiving them on a raspberry pi / mac os X with node.js via serial line connected xbee.
I was thinking about a small simulation so that i can develop without need of running hardware. The only way i know to simulate a serial line is using named pipes which would need two files, one for the input and one for the output. This would need heavy changes within my node.js code (using xbee-api module) which i would like to avoid.
Does anyone know another way to simulate a serial line communication without splitting the communication into two files? My aim would be to simply change the name for the serial port in the configuration of my main code which should than send and receive frames.
I now decided to write my own simulator because there seems to be no other solution. I work corrently on a simulation of the serialport module for nodejs.

Looking for some mutex-like mechanism for exclusive USB access

I'm working on various USB hardware devices, each of which implementing a serial port. I'd like to access such serial ports by multiple (Chrome and non-Chrome) applications running on the system in a pseudo-parallel fashion.
Basically I'd like to use some mutex-like atomic primitive so that I could make sure that one application has access to the serial port at the same time and the others are temporarily blocked, waiting for the mutex to be released.
I'm afraid that the Chrome API doesn't offer any such low-level primitives but please disprove me. Also, I'm open to any suggestions.
I've just finished reading the full Chrome packaged apps API and mutexes are definitely not supported but mutexes only work inside a process anyways so it's not what I'm looking for to begin with.
I could create a native app that could bind to multiple ports on the local loopback interface and proxy those connections towards the /dev/ttyACM* serial interface.
The other solution is implementing multiple serial ports for my USB device in the firmware so that multiple clients could connect to my device without interference.
I think I'll go with the latter solution because I don't want to have a daemon proxy running in the background all the time.

Resources