0
votes

I'm trying to send 9 bytes through a serial port (tested with RS232, RS485) with Python 2.7 pySerial package. If I write out the bytes to the serial port, some of the bytes randomly get lost (do not arrive on the receiving end). If I use a 1 millisec wait between every write of a single byte, all bytes arrive to the receiving end.

I tested the functionality between 2 serial terminals on the same OS.

Here is the code fragment which causes packet (byte) losses:

import serial
import struct

ser = serial.Serial()
ser.baudrate = 9600
ser.parity = "N"
ser.rtscts = False
ser.xonxoff = False
ser.write(struct.pack('B', 0x61))
ser.write(struct.pack('B', 0x62))
ser.write(struct.pack('B', 0x63))
...
ser.close()

The fragment which is working:

import serial
import struct
from time import sleep

ser = serial.Serial()
ser.baudrate = 9600
ser.parity = "N"
ser.rtscts = False
ser.xonxoff = False
ser.write(struct.pack('B', 0x61))
sleep(0.001)
ser.write(struct.pack('B', 0x62))
sleep(0.001)
ser.write(struct.pack('B', 0x63))
sleep(0.001)
...
ser.close()

What can be the root cause for the random packet losses?

System details:

  • OSX 10.9.4
  • Python 2.7
  • Minicom or screen was used for terminal emulation

Test environment:

  • ATC USB/RS485 converters
  • ATC Serial/RS485 converters with USB-Serial adapter
1
You can use: '\x61'. And also write accept multiple bytes: ser.write('\x61\x62\x63'). I believe the receiving end is unable to read those multiple writes consistently.JBernardo
the read buffer on your rs485 converter is filling up if you dont wait ... it is on the device end not the python end... you have a near infinite buffer on the python end (exaggeration but still) ... this isnt Jon is it? (We are working through an rs485 issue of our own)Joran Beasley
I also tried using the syntax ser.write('\x61\x62\x63\x64\x65\x66\x67') but had the same result. Additionally I have concerns on the theory that the receiving end is unable to handle multiple bytes - I'm using a terminal emulator (minicom or just screen) on the same PC.balas
I checked the same setup on a Linux box, same Python version, same RS485 converters. It works without any issues. Only difference is the MacBook and the USB hub I used with the Mac ... Now I will test without the hub on the Mac.balas
Ok, this looks like an issue only on the Mac. Even without the USB hub it fails to send through all bytes. However I'm not sure if this is related to the Python serial implementation or if it has another root cause.balas

1 Answers

0
votes

Typically USB serial converters have to be configured to not flush the buffers on close. As you're seeing if you provide a sleep to wait for the data to complete it works. But if you just dump a bunch of characters then close the device it's buffer may still have data which gets trashed on the close. I'd simply recommend either configuring your device to not flush the buffers (if possible) or wait the character time before performing your close.