# lwpkt2 **Repository Path**: camplus/lwpkt2 ## Basic Information - **Project Name**: lwpkt2 - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: develop - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-12-03 - **Last Updated**: 2026-12-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LwPKT — Lightweight packet protocol for embedded systems [![Build & Test](https://github.com/MaJerle/lwpkt/actions/workflows/build-and-test.yml/badge.svg?branch=develop)](https://github.com/MaJerle/lwpkt/actions/workflows/build-and-test.yml) [![Release](https://img.shields.io/github/v/release/MaJerle/lwpkt)](https://github.com/MaJerle/lwpkt/releases) [![PlatformIO Registry](https://badges.registry.platformio.org/packages/majerle/library/LwPKT.svg)](https://registry.platformio.org/libraries/majerle/LwPKT) [![PyPI](https://img.shields.io/pypi/v/lwpkt)](https://pypi.org/project/lwpkt/) [![License: MIT](https://img.shields.io/github/license/MaJerle/lwpkt)](LICENSE) [![Documentation](https://img.shields.io/badge/docs-docs.majerle.eu-blue)](https://docs.majerle.eu/projects/lwpkt/) LwPKT is a generic, platform independent packet protocol library optimized for embedded systems, written in C11. It wraps application data into packets with optional addressing, command, flags and CRC, and parses incoming bytes back into validated packets. Data goes in and out through [LwRB](https://github.com/MaJerle/lwrb) ring buffers, so it plugs directly into UART, RS-485, USB or any other byte stream. A matching Python implementation is available on PyPI, for PC-side tools and tests. **[📖 Documentation](https://docs.majerle.eu/projects/lwpkt/)** · **[Changelog](CHANGELOG.md)** · **[Releases](https://github.com/MaJerle/lwpkt/releases)** ## Features - Written in C (C11), compatible with `stdint.h` data types - Platform independent, no architecture specific code - Uses [LwRB](https://github.com/MaJerle/lwrb) library for data read/write operations - Support for events on packet-ready, read and write operations - Optimized for embedded systems, allows high optimization for data transfer - Configurable packet structure with support for variable-length data, theoretically unlimited size - Allows multiple nodes in network with `from` and `to` addresses - Separate optional field for _command_ data type - CRC-8 or CRC-32 check to handle data transmission errors, selectable at compile time - Runtime toggle of address, command, flags and CRC features per packet instance - Optional extended (variable-length) address and command encoding for larger identifier ranges - Hardened parsing with bounded variable-length fields, rejecting malformed or oversized packets - Python implementation available, published to PyPI as `lwpkt` - User friendly MIT license ### Applications To name a few: - Communication in RS-485 network between various devices - Low-level point to point packet communication (UART, USB, ethernet, ...) ## Quick start ```c #include #include #include "lwpkt/lwpkt.h" /* LwPKT instance and its TX and RX ring buffers */ static lwpkt_t pkt; static lwrb_t pkt_tx_rb, pkt_rx_rb; static uint8_t pkt_tx_rb_data[64], pkt_rx_rb_data[64]; int main(void) { const char* data = "Hello World"; /* Initialize ring buffers and link them to LwPKT */ lwrb_init(&pkt_tx_rb, pkt_tx_rb_data, sizeof(pkt_tx_rb_data)); lwrb_init(&pkt_rx_rb, pkt_rx_rb_data, sizeof(pkt_rx_rb_data)); lwpkt_init(&pkt, &pkt_tx_rb, &pkt_rx_rb); lwpkt_set_addr(&pkt, 0x12); /* Our device address */ /* Write packet to address 0x11 with command 0x85, it is placed in pkt_tx_rb. Transmit the content of pkt_tx_rb over your interface (UART, ...) */ lwpkt_write(&pkt, 0x11, 0x85, data, strlen(data)); /* Put received bytes into pkt_rx_rb (for example from UART interrupt), then process them */ if (lwpkt_read(&pkt) == lwpktVALID) { printf("Packet from 0x%02X, cmd 0x%02X, %u bytes of data\r\n", (unsigned)lwpkt_get_from_addr(&pkt), (unsigned)lwpkt_get_cmd(&pkt), (unsigned)lwpkt_get_data_len(&pkt)); if (lwpkt_is_for_me(&pkt)) { /* Process data with lwpkt_get_data(&pkt) */ } } return 0; } ``` The arguments of `lwpkt_write` follow the enabled packet fields. The example uses the default configuration, with address and command enabled and flags disabled. More examples, including event-driven processing with `lwpkt_process`, are in [examples](examples). ### Python ```sh pip install lwpkt ``` See [python/README.md](python/README.md) for usage. ## Configuration Packet fields (address, flags, command, CRC), maximum data length and events are configured in the `lwpkt_opts.h` file. Copy [lwpkt/src/include/lwpkt/lwpkt_opts_template.h](lwpkt/src/include/lwpkt/lwpkt_opts_template.h) to your project, rename it to `lwpkt_opts.h`, and override the defaults listed in [lwpkt_opt.h](lwpkt/src/include/lwpkt/lwpkt_opt.h). See _Getting started_ in the [documentation](https://docs.majerle.eu/projects/lwpkt/) for details. ## Add to your project > **Important:** LwPKT requires the [LwRB](https://github.com/MaJerle/lwrb) ring buffer library, which is not pulled in automatically. > Add LwRB to your project as well, and make it available to LwPKT, as shown for each build system below. ### CMake ```cmake set(LWPKT_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/lwpkt_opts.h) # Optional, default options are used otherwise add_subdirectory(path/to/lwrb/lwrb) add_subdirectory(path/to/lwpkt/lwpkt) target_link_libraries(lwpkt PUBLIC lwrb) # Required, lwpkt target does not link LwRB on its own target_link_libraries(${PROJECT_NAME} PRIVATE lwpkt) ``` Optional compiler options and definitions can be set with `LWPKT_COMPILE_OPTIONS` and `LWPKT_COMPILE_DEFINITIONS`, see [lwpkt/library.cmake](lwpkt/library.cmake). ### Any other build system 1. Copy the `lwpkt` folder to your project 2. Add `lwpkt/src/include` to the include path 3. Add the source files from `lwpkt/src/lwpkt/` to the build 4. Add LwRB to the project the same way: `lwrb/src/include` to the include path and `lwrb/src/lwrb/lwrb.c` to the build 5. Create `lwpkt_opts.h` in a folder on the include path (see [Configuration](#configuration)) ### PlatformIO LwPKT is available in the [PlatformIO Registry](https://registry.platformio.org/libraries/majerle/LwPKT). LwRB is not installed as a dependency automatically, so add both libraries to your `platformio.ini`: ```ini lib_deps = majerle/LwPKT majerle/LwRB ``` ## Contribute Fresh contributions are always welcome. Simple instructions to proceed: 1. Fork Github repository 2. Follow [C style & coding rules](https://github.com/MaJerle/c-code-style) and use `clang-format` to format the code 3. Create a pull request to `develop` branch with new features or bug fixes Alternatively you may: 1. Report a bug 2. Ask for a feature request ## License LwPKT is released under the [MIT license](LICENSE).