PJON read character array from receiving struct

Good day everyone. My very first post here. I hope I place all the needed information right, and please correct me if I’m wrong.

I’m trying to setup a simple PJON network. In this network I’m using an ESP-32 as a command module, and several Arduino nano’s as worker modules that control single parts of the escape box I’m building. The Arduinos only have to send their state to the ESP-32, and the ESP-32 is telling the nano’s what to do. To make this possible I make use of the PJON SerialBitBang library. The PJON documentation is really good and I love the single wire communication.

To make the communication possible, I want to make use of a simple struct consisting of an integer and a small character array.

struct payLoad {
  uint8_t cmd;
  char msgLine[20];
};

When I let the receiver print the struct, the integer gets printed fine. However, when I want to print the msgLine char array, I see the following:
Serial Monitor output:

10
1{

The integer (10) shows as sent.
The character array “12345” however is not. Only the 1 is shown.

So my question: Can someone please point out to me what I am doing wrong here?
I'm near 100% sure that this is not a PJON related issue, but rather me missing a detail here.

Code that can be used to reproduce the issue:
Arduino worker:

#include <PJONSoftwareBitBang.h>
PJONSoftwareBitBang bus(19);

struct payLoad {
  uint8_t cmd;
  char msgLine[20];
};

payLoad pl;

void send_command(uint8_t id, uint8_t cmd, char msgLine[20]) {
  payLoad pl;
  strcpy(pl.msgLine , msgLine);
  pl.cmd = cmd;
  bus.send(id, &pl, sizeof(&pl));
};

void setup() {
  bus.strategy.set_pin(8);
  bus.begin();
}

void loop() {
  send_command(20,10,"12345");
  bus.update();
  delay(2000);
}

Code for the receiving ESP-32:

// Command module running on an ESP32.
#include <PJONSoftwareBitBang.h>
PJONSoftwareBitBang bus(20);

// STRUCTS
struct payLoad {
  uint8_t cmd;
  char msgLine[20];
};

payLoad pl;

void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
  memcpy(&pl, payload, sizeof(pl));
  Serial.println(pl.cmd);
  Serial.println(pl.msgLine);
};

void setup() {
  Serial.begin(9600);
  bus.strategy.set_pin(25);
  bus.set_receiver(receiver_function);
  bus.begin();
}

void loop() {
  bus.receive(10000);
}

Cursieve tekst

Where is the the msgLine char array declared and defined in the first sketch ? Is it zero terminated to turn it into a C style string ?

Hello @UKHeliBob.

The very first sketch was a cut and paste of the transmitter and receiver code. So the msgLine is declared within the struct.

Should I declare it as a char array first and then add it to the struct?

My apologies. I see now that you have used msgLine as a variable name for one of the function parameters

I found out what the issue is. Classic development example of how one character can ruin your day (e.g. read the **** manual). At the moment of sending I used the size of the pointer as payload size, instead of the actual struct.

So in the sender code I had:

bus.send(id, &pl, sizeof(&pl));

But it should have been:

bus.send(id, &pl, sizeof(pl));

By removing the & in the size turns the payload size to 21 byte instead of 2.

This question can be closed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.