https://github.com/alexwoo1900/ymodem
YMODEM written by Python - 用Python实现的YMODEM通讯协议
https://github.com/alexwoo1900/ymodem
python ymodem
Last synced: 5 months ago
JSON representation
YMODEM written by Python - 用Python实现的YMODEM通讯协议
- Host: GitHub
- URL: https://github.com/alexwoo1900/ymodem
- Owner: alexwoo1900
- License: mit
- Created: 2018-03-29T07:46:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-19T01:09:54.000Z (almost 2 years ago)
- Last Synced: 2025-11-29T04:18:56.789Z (7 months ago)
- Topics: python, ymodem
- Language: Python
- Homepage:
- Size: 41.1 MB
- Stars: 98
- Watchers: 6
- Forks: 55
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

The YMODEM project is based on XMODEM implementation written by tehmaze. It is also compatible with XMODEM mode.
[](https://opensource.org/licenses/MIT)
README: [ENGLISH](https://github.com/alexwoo1900/ymodem/blob/master/README.md) | [简体中文](https://github.com/alexwoo1900/ymodem/blob/master/README_CN.md)
- [**Installation**](#installation)
- [**Usage**](#usage)
- [CLI TOOL](#cli-tool)
- [Sending a batch of files](#sending-a-batch-of-files)
- [Receive a file](#receive-a-file)
- [Source Code](#source-code)
- [API](#api)
- [Create MODEM Object](#create-modem-object)
- [Send files](#send-files)
- [Receive files](#receive-files)
- [Debug](#debug)
- [Changelog](#changelog)
- [License](#license)
## Demo
### Test the sending and receiving functions

### Interact with SecureCRT
Interact with SecureCRT as sender

Interact with SecureCRT as Finder

## Installation
```Bash
pip install ymodem
```
## Usage
### CLI TOOL
```Bash
# To get help
ymodem -h
# or
python -m ymodem -h
```
#### Sending a batch of files
```Bash
ymodem send ./file.bin ./file2.bin -p COM4 -b 115200
# or
python -m ymodem send ./file.bin ./file2.bin -p COM4 -b 115200
```
#### Receive a file
```Bash
ymodem recv ./ -p COM4 -b 115200
# or
python -m ymodem recv ./ -p COM4 -b 115200
```
### Source Code
```python
from ymodem.Socket import ModemSocket
# define read
def read(size, timeout = 3):
# implementation
# define write
def write(data, timeout = 3):
# implementation
# create socket
cli = ModemSocket(read, write)
# send multi files
cli.send([file_path1, file_path2, file_path3 ...])
# receive multi files
cli.recv(folder_path)
```
For more detailed usage, please refer to __main__.py.
### API
#### Create MODEM Object
```python
def __init__(self,
read: Callable[[int, Optional[float]], Any],
write: Callable[[Union[bytes, bytearray], Optional[float]], Any],
protocol_type: int = ProtocolType.YMODEM,
protocol_type_options: List[str] = [],
packet_size: int = 1024,
style_id: int = _psm.get_available_styles()[0]):
```
- protocol_type: Protocol type, see Protocol.py
- protocol_type_options: such as g representing the YMODEM-G in the YMODEM protocol.
- packet_size: The size of a single packet, 128/1024 bytes, may be adjusted depending on the protocol style
- style_id: Protocol style, different styles have different support for functional features
#### Send files
```python
def send(self,
paths: List[str],
callback: Optional[Callable[[int, str, int, int], None]] = None
) -> bool:
```
- callback: callback function. see below.
Parameter | Description
-|-
task index | index of current task
task (file) name | name of the file
total packets | number of packets plan to send
success packets | number of packets successfully sent
#### Receive files
```python
def recv(self,
path: str,
callback: Optional[Callable[[int, str, int, int], None]] = None
) -> bool:
```
- path: folder path for storing the target file
- callback: callback function. Same as the callback of send().
#### ATTENTION
Depending on different communication environments, developers may need to manually adjust timeout parameters in _read_and_wait or _write_and_wait.
## Debug
If you want to output debugging information, set the log level to DEBUG.
```python
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
```
## Changelog
### v1.5 (2024/02/03)
- Added cli tool to iteract with YMODEM via Serial bus
### v1.5 (2023/05/20 11:00 +00:00)
- Rewritten send() and recv()
- Support YMODEM-G.
The success rate of YMODEM-G based on pyserial depends on the user's OS, and after testing, the success rate is very low without any delay.
## License
[MIT License](https://opensource.org/licenses/MIT)