YMODEM (For cluster communication port) written by Python - 用Python实现的YMODEM串口通讯协议

alexwoo1900 alexwoo1900 Last update: Aug 26, 2022

ymodem-logo

The YMODEM project is based on XMODEM implementation written by tehmaze. It is also compatible with XMODEM mode.

Build Statuslicense

README: ENGLISH | 简体中文

YMODEM for Python Demo

Test the sending and receiving functions

If you want to run the test sample, please do the following:

  1. use virtual serial port tool to generate COM1 and COM2 that can communicate
  2. run the FileReceiver.py and FileSender.py on the command line

The specific transmission process is shown in the following figure:SenderAndReceiver

Interact with SecureCRT

Interact with SecureCRT as senderSecureCRT1

Interact with SecureCRT as FinderSecureCRT2

How to use YMODEM for Python

  1. Import MODEM module
from Modem import Modem
  1. Define the reader and writer (or read() and write()), then create MODEM object
def sender_read(size, timeout=3):    serial_io.timeout = timeout    return serial_io.read(size) or Nonedef sender_write(data, timeout=3):    serial_io.writeTimeout = timeout    return serial_io.write(data)sender = Modem(sender_read, sender_write)
  1. Send file
sender.send(stream, info=file_info)
  1. Receive file
receiver.recv(stream, info=file_info)

YMODEM for Python API

Create MODEM Object

def __init__(self, reader, writer, mode='ymodem1k', program="rzsz")
  • reader, reader(object) or read(function)
  • writer, writer(object) or write(function)
  • mode, support xmodem, xmodem1k, ymodem, ymodem1k(by default)
  • program, YMODEM of different program have different features

Send file (stream)

def send(self, stream, retry=10, timeout=10, callback=None, info=None):
  • stream, data stream.
  • retry, max retry count.
  • timeout, timeout of reader or writer in second.
  • callback, callback function. see below.
  • info, file information dictionary. see below.

callback parameters:

ParameterDescription
total packetsnumber of packets plan to send
success packetsnumber of packets successfully sent
failed packetsnumber of packets failed to send

info properties:

FieldDescription
namefile name
lengthfile length
mtimefile modification date (GMT)
sourceoperation system the file original from

Receive file (stream)

def recv(self, stream, crc_mode=1, retry=10, timeout=10, delay=1, callback=None, info=None)
  • stream, data stream.
  • crc_mode, checksum or crc mode.
  • retry, max retry count.
  • timeout, timeout of reader or writer in second.
  • delay, delay in second.
  • callback, callback function. see below.
  • info, file information dictionary. see below.

callback parameters:

ParameterDescription
received lengthreceived file bytes
remaining lengthremaining file bytes

info properties:

FieldDescription
save_pathfolder path where the file are saved

Changelog

v1.2 (2022/8/10 14:00 +00:00)

  • Fixed receiver bug

License

MIT License

Tags:

Subscribe to our newsletter