It is super simple to use the XMODEM protocol implementation found on PyPi. A few things to note about the example above is there are some things that are not needed. (Maybe this worked for the author or with a previous version of the module?)
The documentation found here is extremely helpful, so don't let it scare you. You will need a sender and a receiver obviously, and since I do not know which one the Python script will be, here are two examples I have tested and are working below. (basically copied and pasted from the examples on PyPi)
import serial
from xmodem import XMODEM
ser = serial.Serial(port='COM56')
def getc(size, timeout=8):
gbytes = ser.read(size)
print(f'Read Byte: {gbytes}')
return gbytes or None
def putc(data, timeout=8):
pbytes = ser.write(data)
print(f'Put Byte: {pbytes}')
return pbytes or None
if __name__ == '__main__':
modem = XMODEM(getc, putc)
To receive from the serial device:
stream = open('output', 'wb')
modem.recv(stream, crc_mode=0)
To send to the serial device:
stream = open('input', 'rb')
modem.send(stream)
The key here is to be sure the baud rate is set on both sides (defaulting here). Do NOT add any delay or sleep as this is not time based, but transaction based. the prints will allow you to see the transaction real time as the data flows in or out of the file/serial port.