Source: https://mokxi.com/libraries/serialtransfer
Updated: 2026-11-23

Libraries / Communication

Arduino library · Communication

# SerialTransfer: send structs between boards over serial

Yes. You can use the SerialTransfer library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "A struct through SerialTransfer, and a frame that fails its CRC", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <SerialTransfer.h> and it builds, with nothing to install. Free, and no account needed.

## What SerialTransfer does

Sending a number over serial with Serial.print() works until you need two numbers, a float and a byte, or to know that what arrived is what was sent. SerialTransfer packages bytes into a frame: a start byte, a packet ID, a length, your payload and a CRC-8 checksum. The receiver finds the start byte, collects the payload and only hands it over if the checksum matches.

You copy a struct into the packet with txObj(), send it with sendData(), and on the other board call available() and copy it back out with rxObj(). Because it copies raw bytes, the struct has to look the same on both ends: the same field order and the same sizes. A packed struct made of fixed-size types such as uint16_t and float is the safe choice between an Uno and an ESP32.

It works on any Arduino Stream, so the same code runs over Serial1, SoftwareSerial or a Bluetooth serial link. Each packet has an ID, so one wire can carry several kinds of message, and begin() can take a table of callbacks that tick() runs by ID.

Library Manager name | SerialTransfer

Author | PowerBroker2

Include | #include <SerialTransfer.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <SerialTransfer.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "SerialTransfer" by PowerBroker2 from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add SerialTransfer to lib_deps in PlatformIO. Mokxi follows the calls of release 4.0.0, so a sketch written here builds there too.

What to know in the simulator: Pure software on any Stream, so Serial1, SoftwareSerial or a board-to-board serial wire speak the real frame. The I2C and SPI transports are not here.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### SerialTransfer myTransfer

Creates a transfer object with its own 255-byte send and receive buffers.

### myTransfer.begin(Serial1)

Attaches it to a serial port, or any other Stream. Start that port yourself first with its own begin(baud).

### myTransfer.txObj(testStruct, sendSize)

Copies a struct, float, array or any variable into the outgoing payload at the offset you give and returns the offset after it, so the next call can append.

### myTransfer.sendData(sendSize)

Sends the first sendSize bytes of the payload as one packet. A second argument sets the packet ID, 0 to 255.

### myTransfer.available()

Reads what has arrived and returns the payload length when a whole good packet is in, otherwise 0. The status member says why: NEW_DATA, CRC_ERROR or STALE_PACKET_ERROR.

### myTransfer.rxObj(testStruct)

Copies the received payload into a variable, starting at an optional offset, and returns the offset after it.

## Example: A struct through SerialTransfer, and a frame that fails its CRC

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### ERROR: CRC_ERROR

A byte changed on the way, or the two boards disagree on the baud rate. Check that both ends call begin() with the same speed, that the grounds are joined, and that nothing else prints on the same serial port.

### The values arrive wrong, but no error shows

The two structs do not match. Use the same field order on both boards, fixed-size types such as int16_t and not int, and mark the struct packed so the compiler adds no padding that differs between an AVR and an ESP32.

### ERROR: STALE_PACKET_ERROR

A packet started and then stopped for longer than the timeout. The sender was cut off, or a long delay() in the receiver let the buffer overflow. Call available() often, and avoid long delays in loop().

## Questions and answers

### How big can a packet be?

The payload is up to 255 bytes. On an Uno that is a lot of the 2 KB of RAM, because the library keeps one buffer for sending and another for receiving.

## Keep going

### More communication libraries

- OneWire
- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard
- RF24Mesh
- SoftWire
- MicroNMEA
- NeoGPS
- RadioHead (RH_NRF24 driver)

Every built-in library · Open the editor

## Try SerialTransfer without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
