Delays in serial communication with pyserial - python-3.x

I'm seeing ~15ms of overhead when communicating with a microcontroller over RS-485 with a USB-to-serial converter, is there any way to minimize this overhead?
I have some debug and data collection python3.10 scripts that connect to a microcontroller via either USB or half-duplex RS-485 serial. I'm using PySerial in both cases and I am either passing it the address for the USB-to-serial converter to the controller directly (running on MacOS). Today I logged how long a command and response takes to execute at different RS485 baud rates (median of 1000 command/response cycles, min time is similar):
9600 baud - 32 ms
38400 baud - 16 ms
115200 baud - 16 ms
230400 baud - 16 ms
500000 baud - 16 ms
USB - 1 ms
I have scoped the signal # 115200 baud and the command + response takes 3ms max, so I while I would like this micro to burn in a pit I don’t think I can blame this particular problem on it. Since all my code is shared between the serial and usb tests, I'm left to believe that the majority of the overhead is coming from pyserial or my d-tech usb-to-serial converter.
From what I’ve seen a large number of the usb-to-serial adapters use the same FTDI chipset, so I’m not sure if I would actually get any improved performance by blindly choosing another converter. If someone knows better here please chime in.

Related

How to get a UART communication at 12 Mbps in linux (Raspbian)?

I am currently working on a Raspberry pi (Jessie Stretch), the issue is that I want to connect two FTDI FT2232H serially at 12 Mbps, but because 12Mbps is not a standard speed Raspbian does not allow me to add that baud rate. I would like to know if someone has transmitted at that speed or if someone knows how to achieve the Bit rate of 12 Mbps with the maximum baud rate in Raspbian (4,000,000) .
PS: I changed the UART clock to 64,000,000, modified the "termbits.h" library and created termios structures, but nothing worked.
Thanks.
The data sheet for the FT2232H does advertise it supports 12 Mbaud (not 12Mbps). But it looks like it comes in different modules with support for RS232, RS422, and RS485. The most typical being RS232.
I've never heard of anyone operating a RS232 connection at 120000000 baud. The typical maximum that almost everything supports is 115200. The highest I've seen is 921600. Typical RS232 cables started running into interference issues at the higher baud rates.
I suspect the 12Mbaud spec is for RS422/RS485 operation which requires different cabling and is designed for higher speeds.
If you're using an FT2232H with RS232, the speeds you're looking for are likely unrealistic. If you're using it with RS422/RS485 you can probably get there, but it will be a much more specialized endeavor. It does look like Linux does support RS485. But there's not nearly as much documentation out there as for RS232.
Can you provide any more information about the USB adapters you're using?

I2C and Serial communication between devices

I am using python sockets to connect to a bluetooth HC-05 module with my PC. I want to send music to the HC-05 by converting a wav file to a string array which will later by converted to integers ranging from 0-65535 on an arduino. The arduino and HC-05 communicate via serial at 9600 baud. Then those ints will be passed into a DAC via I2C. I am wondering if there could be a memory issue sending an enormous number of strings from my PC. Is it possible the original quality of the sound will be distorted as a result of the different rates of sending/receiving data across the devices? Or will the sound signal just be delayed on the DAC?
The arduino and HC-05 communicate via serial at 9600 baud.
This is going to be too low to be usable for audio.
9600 baud gives you 7680 bits of data per second. At 16 bits per sample, you're looking at a sample rate of 481 Hz, which is too low for intelligible audio. It's barely even high enough to reproduce sound at all.
You need to:
Increase the baud rate. Ideally you'll want at least 57600 baud, for 46 kbit/sec of data. If higher baud rates are available, use them.
Use fewer bits for each sample. Using 4 bits for each sample at 56 kbaud will give you a respectable sample rate of 11.5 kHz. The audio will sound a little tinny at 4 bits, but it'll be intelligible.

How to modify timing in readyRead of QextSeriaport [duplicate]

I'm implementing a protocol over serial ports on Linux. The protocol is based on a request answer scheme so the throughput is limited by the time it takes to send a packet to a device and get an answer. The devices are mostly arm based and run Linux >= 3.0. I'm having troubles reducing the round trip time below 10ms (115200 baud, 8 data bit, no parity, 7 byte per message).
What IO interfaces will give me the lowest latency: select, poll, epoll or polling by hand with ioctl? Does blocking or non blocking IO impact latency?
I tried setting the low_latency flag with setserial. But it seemed like it had no effect.
Are there any other things I can try to reduce latency? Since I control all devices it would even be possible to patch the kernel, but its preferred not to.
---- Edit ----
The serial controller uses is an 16550A.
Request / answer schemes tends to be inefficient, and it shows up quickly on serial port. If you are interested in throughtput, look at windowed protocol, like kermit file sending protocol.
Now if you want to stick with your protocol and reduce latency, select, poll, read will all give you roughly the same latency, because as Andy Ross indicated, the real latency is in the hardware FIFO handling.
If you are lucky, you can tweak the driver behaviour without patching, but you still need to look at the driver code. However, having the ARM handle a 10 kHz interrupt rate will certainly not be good for the overall system performance...
Another options is to pad your packet so that you hit the FIFO threshold every time. It will also confirm that if it is or not a FIFO threshold problem.
10 msec # 115200 is enough to transmit 100 bytes (assuming 8N1), so what you are seeing is probably because the low_latency flag is not set. Try
setserial /dev/<tty_name> low_latency
It will set the low_latency flag, which is used by the kernel when moving data up in the tty layer:
void tty_flip_buffer_push(struct tty_struct *tty)
{
unsigned long flags;
spin_lock_irqsave(&tty->buf.lock, flags);
if (tty->buf.tail != NULL)
tty->buf.tail->commit = tty->buf.tail->used;
spin_unlock_irqrestore(&tty->buf.lock, flags);
if (tty->low_latency)
flush_to_ldisc(&tty->buf.work);
else
schedule_work(&tty->buf.work);
}
The schedule_work call might be responsible for the 10 msec latency you observe.
Having talked to to some more engineers about the topic I came to the conclusion that this problem is not solvable in user space. Since we need to cross the bridge into kernel land, we plan to implement an kernel module which talks our protocol and gives us latencies < 1ms.
--- edit ---
Turns out I was completely wrong. All that was necessary was to increase the kernel tick rate. The default 100 ticks added the 10ms delay. 1000Hz and a negative nice value for the serial process gives me the time behavior I wanted to reach.
Serial ports on linux are "wrapped" into unix-style terminal constructs, which hits you with 1 tick lag, i.e. 10ms. Try if stty -F /dev/ttySx raw low_latency helps, no guarantees though.
On a PC, you can go hardcore and talk to standard serial ports directly, issue setserial /dev/ttySx uart none to unbind linux driver from serial port hw and control the port via inb/outb to port registers. I've tried that, it works great.
The downside is you don't get interrupts when data arrives and you have to poll the register. often.
You should be able to do same on the arm device side, may be much harder on exotic serial port hw.
Here's what setserial does to set low latency on a file descriptor of a port:
ioctl(fd, TIOCGSERIAL, &serial);
serial.flags |= ASYNC_LOW_LATENCY;
ioctl(fd, TIOCSSERIAL, &serial);
In short: Use a USB adapter and ASYNC_LOW_LATENCY.
I've used a FT232RL based USB adapter on Modbus at 115.2 kbs.
I get about 5 transactions (to 4 devices) in about 20 mS total with ASYNC_LOW_LATENCY. This includes two transactions to a slow-poke device (4 mS response time).
Without ASYNC_LOW_LATENCY the total time is about 60 mS.
With FTDI USB adapters ASYNC_LOW_LATENCY sets the inter-character timer on the chip itself to 1 mS (instead of the default 16 mS).
I'm currently using a home-brewed USB adapter and I can set the latency for the adapter itself to whatever value I want. Setting it at 200 µS shaves another mS off that 20 mS.
None of those system calls have an effect on latency. If you want to read and write one byte as fast as possible from userspace, you really aren't going to do better than a simple read()/write() pair. Try replacing the serial stream with a socket from another userspace process and see if the latencies improve. If they don't, then your problems are CPU speed and hardware limitations.
Are you sure your hardware can do this at all? It's not uncommon to find UARTs with a buffer design that introduces many bytes worth of latency.
At those line speeds you should not be seeing latencies that large, regardless of how you check for readiness.
You need to make sure the serial port is in raw mode (so you do "noncanonical reads") and that VMIN and VTIME are set correctly. You want to make sure that VTIME is zero so that an inter-character timer never kicks in. I would probably start with setting VMIN to 1 and tune from there.
The syscall overhead is nothing compared to the time on the wire, so select() vs. poll(), etc. is unlikely to make a difference.

A better way to read serial device in Linux

I have a serial device connected to USB of my Linux PC and i need constantly read data from it and process it.
When i use open(fd, xxx) while(1) {read(fd, xx)} it works but i miss more than 60% of data.
Is there a better technique to have less data loss?
Addendum
The baud rate is 115200. we must receive at least 10 packets a second, but we get barely two.. The packet size is 22 bytes. The code is exactly the code from the second comment.

Setting Linux serial baud rate higher than 4 megabaud

Super long story short: is it possible to read from a UART at something close to 12 megabaud?
I have a high speed connection that I am trying to read from on a Raspberry Pi. At present, the Raspberry Pi (and as far as I can tell, the world of serial programming) has a maximum baud rate of 4 Mbaud, which I achieved by setting the Raspberry Pi's UART clock to 160 MHz.
However, I need a UART running at 12+ Mbaud to capture the data streaming in from my external source. The Raspberry Pi's terminos.h file specifies 4 Mbaud as the max baud rate for serial communication. Is there a way to get higher baud rates? Is the key ioctl from How can I set the baud rate to 307,200 on Linux??
Raspbian 3.6.11, Raspberry Pi rev2
Each UART has a hardware limit... If your hardware supports this rate, it's allowed... Verify it before.

Resources