Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4878

Troubleshooting • Long latency on USB to serial adapter

$
0
0
I'm using one of these https://www.waveshare.com/usb-to-4ch-rs485.htm to interface 3 channels of RS-485 into a Python script on a Raspi 5 with stock up to date Raspberry Pi OS install. Data is sent by 3 x Arduinos in 0x0 delimited 8 byte packets every 100ms. The problem I have is that a huge latency builds up the longer the device is connected. When I first start the Pi up, the data is received in realtime, but as time goes on the delay between the real time data being received is up to 10 seconds.

Here is an example script which receives the packets:

Code:

import serialfrom cobs import cobsimport structser = serial.Serial('/dev/ttyACM0', 57600, timeout=1)  # Adjust the port as neededdef decode_and_print(buffer):    try:        # Remove the final 0x00 delimiter before decoding        print(''.join('{:02x}'.format(x) for x in buffer), len(buffer))        buffer = buffer.rstrip(b'\x00')        # Decode the buffer using COBS        decoded_data = cobs.decode(buffer)        # Unpack the data assuming the same structure as 'gyro_read'        print        if len(decoded_data) == 8:  # gyro_read has 4 int16_t fields, so 8 bytes            x, y, z, wtron = struct.unpack('>hhhh', decoded_data)  # Little-endian            print(f"x: {x}, y: {y}, z: {z}, wtron (potentiometer): {wtron}")        else:            print("Error: Unexpected decoded data length.")    except Exception as e:        print(f"Error decoding data: {e}")while True:    # Read data from serial    data = ser.read_until(b'\x00')  # Read until the 0x00 delimiter    if data:        decode_and_print(data)
This script works perfectly in Windows, but shows the latency on the Pi. I thought it might be a Python serial problem, so I wrote a similar script in C++, which showed the same issues. I've searched around and it seems like various latency problems on the Pi with USB to serial adapters are very common, but all seem to have somewhat different causes. In terms of the latency, it always is receiving messages at 100ms intervals, but it seems to take 10 seconds for events that happen to come through to the script.

This is the dmesg of the device:

Code:

[  370.873763] usb 3-1: new full-speed USB device number 3 using xhci-hcd[  371.085333] usb 3-1: New USB device found, idVendor=1a86, idProduct=55d5, bcdDevice= 1.48[  371.085336] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3[  371.085338] usb 3-1: Product: USB Quad_Serial[  371.085340] usb 3-1: Manufacturer: WCH.CN[  371.085341] usb 3-1: SerialNumber: BC6208ABCD[  371.124476] cdc_acm 3-1:1.0: ttyACM0: USB ACM device[  371.127407] cdc_acm 3-1:1.2: ttyACM1: USB ACM device[  371.130406] cdc_acm 3-1:1.4: ttyACM2: USB ACM device[  371.133372] cdc_acm 3-1:1.6: ttyACM3: USB ACM device
I beleive it's CH34x based. I've tried various things after searching for different solutions:
  • Flushing various buffers on Pi and Arduino, changing baud rates, changing number of messages per second, serial timeouts, different methods of receiving the serial data
  • Changing the device from ttyACM to ttyUSB using modprobe to load different drivers (modeprobe -r cdc_acm/modprobe usbserial vendor=...)
  • Adding stuff to /boot/firmware/config.txt (dwc_otg.nak_holdoff=0 and usbhid.mousepoll=0)
  • Tried different physical USB ports
  • Clearing the buffer before running the script using dd
I also tried a single CH34x based USB to serial converter, which worked fine and didn't display the problem. I've ordered a couple of different USB to RS-485 converters to see if they are any better, but I'd really like to find a solution for this.

I started down the route of using two way communication, where the Python script can stop and start the Arduinos sending, but I'd really prefer a one-way method.

Any help greatly appreciated!

Statistics: Posted by silentlywhat — Tue Sep 17, 2024 9:13 pm



Viewing all articles
Browse latest Browse all 4878

Trending Articles