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.
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.
uint16_t sendSize = 0;
sendSize = myTransfer.txObj(testStruct, sendSize);
sendSize = myTransfer.txObj(temperature, sendSize);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.
if (myTransfer.available()) {
uint16_t recvSize = 0;
recvSize = myTransfer.rxObj(testStruct, recvSize);
}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.
#include "SerialTransfer.h"
// A 64-byte ring buffer that is both ends of the wire.
class Wire2 : public Stream {
public:
int available() { return (head + 64 - tail) % 64; }
int read() { if (head == tail) return -1; uint8_t b = buf[tail]; tail = (tail + 1) % 64; return b; }
int peek() { return head == tail ? -1 : buf[tail]; }
size_t write(uint8_t b) { buf[head] = b; head = (head + 1) % 64; return 1; }
void flush() {}
void flip(int n) { buf[(tail + n) % 64] ^= 0x10; }
private:
uint8_t buf[64];
int head = 0, tail = 0;
};
Wire2 wire;
SerialTransfer link;
struct __attribute__((packed)) Reading {
char tag;
float value;
int16_t count;
};
void setup() {
Serial.begin(9600);
link.begin(wire, false);
Reading out = {'$', 4.5, -7};
uint16_t size = 0;
size = link.txObj(out, size);
link.sendData(size, 3);
Serial.print("sent ");
Serial.print(size);
Serial.println(" bytes");
if (link.available()) {
Reading in;
link.rxObj(in);
Serial.print("packet ");
Serial.print(link.currentPacketID());
Serial.print(": ");
Serial.print(in.tag);
Serial.print(' ');
Serial.print(in.value, 2);
Serial.print(' ');
Serial.println(in.count);
}
size = link.txObj(out, 0);
link.sendData(size);
wire.flip(6); // one bit of the payload changes on the wire
link.available();
Serial.println(link.status == CRC_ERROR ? "crc error" : "crc missed it");
}
void loop() {}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.
Try SerialTransfer without installing anything
Open the example, change the sketch, and keep your version in a free account.