Metadata-Version: 2.4
Name: lwpkt
Version: 2.0.0
Summary: Lightweight packet protocol library for communication implementation
Author-email: Tilen Majerle <tilen@majerle.eu>
License-Expression: MIT
Project-URL: Homepage, https://github.com/MaJerle/lwpkt
Project-URL: Repository, https://github.com/MaJerle/lwpkt.git
Project-URL: Documentation, https://docs.majerle.eu/projects/lwpkt/
Project-URL: Issues, https://github.com/MaJerle/lwpkt/issues
Keywords: lwpkt,packet,protocol,manager,communication,com,lightweight,rs-485,rs-422,rs485,rs422,rs-232,rs232,uart,usart,lpuart
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Communications
Classifier: Topic :: Software Development :: Embedded Systems
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# LwPKT (Python)

Pure-Python encoder/decoder for the [LwPKT](https://github.com/MaJerle/lwpkt) lightweight packet
protocol, mirroring the reference C implementation. Read the full protocol documentation at
[docs.majerle.eu/projects/lwpkt](https://docs.majerle.eu/projects/lwpkt/).

## Install

```sh
pip install lwpkt
```

## Usage

```python
from lwpkt import LwPKT, LwPKTError

pkt = LwPKT()
pkt.our_addr = 0x01

# Encode an outgoing packet
data = pkt.generate_packet(b"hello", cmd=0x02, addr_to=0x02)

# Feed received bytes in and process them
pkt.write_rx_data(data)
try:
    if pkt.rx_process():
        received = pkt.rx_get_packet()
        print(received.data, received.cmd, received.pkt_from)
except LwPKTError as e:
    print("Malformed packet, discarded:", e)
```

`rx_process()` raises a typed exception (`LwPKTLengthError`, `LwPKTFieldOverflowError`,
`LwPKTCRCError`, `LwPKTStopByteError` - all subclasses of `LwPKTError`) when it encounters
malformed input. The parser resets itself before raising, so it's always safe to keep calling
`write_rx_data()` / `rx_process()` after catching one.

## License

MIT, see [LICENSE](LICENSE).
