# Help sending memcpyed buffer over XBee's S2C in AT Mode in one pass

**URL:** <https://forums.digi.com/t/help-sending-memcpyed-buffer-over-xbees-s2c-in-at-mode-in-one-pass/23313>\
**Category:** XBee & XBee Gateway\
**Tags:** can, s2c, arduino, xbee\
**Created:** [December 5, 2022, 8:05am UTC](https://forums.digi.com/t/help-sending-memcpyed-buffer-over-xbees-s2c-in-at-mode-in-one-pass/23313 "2022-12-05T08:05:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegreekman2](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@thegreekman2](https://forums.digi.com/u/thegreekman2)\
**Post date:** [December 5, 2022, 8:05am UTC](https://forums.digi.com/t/help-sending-memcpyed-buffer-over-xbees-s2c-in-at-mode-in-one-pass/23313/1 "2022-12-05T08:05:04Z")

</div>

Hello all. The goal of my following code snippets is to send a byte\_buffer over two XBees (S2C) in AT mode in one go, or at the very least successfully, even if not in one go.

In total there are 3 teensys. Two of them are connected to each other through a CAN transceiver (sn65hvd230). The code I have for these two has been thoroughly tested and is working. Essentially, I am hard coding the data of one can\_message\_t (from the FLEXCAN\_T4 library on github) and sending it through one CAN IC to another and reading it on the second Teensy which is connected to the XBee. THe last teensy is simply plugged into another computer and the serial is read through XCTU/Arduino Serial monitor after being transmitted over teensy. (have also tested just sending with XBees and had no problem, although that was serial input, not a hard coded byte/char array, ie string)

The can\_message\_t is read by code on the second teensy and populates a byte buffer of length 24 (size of can\_msg\_t) using memcpy. I have tested this and found memcpy works fine and populates the buffer array (and on the other side a can\_msg\_t) struct just fine. The problem, however, is that the data is not coming through in one go, or is being jumbled up. This is where I get lost so pardon if my understanding is lacking.

I have tried to only send when XBee.availableForWrite (and also read) are \> 23, implying the entire can frame (byte buffer) can be sent, however I am not getting the byte buffer sent correctly. The data differs (partly because of the timestamp member of Can\_message\_t as that is the only variable updated) however other members are also changed which is where my confusion begins.

I understand XBee can send bytes in little packets until a size is reached and also spent time trying to use readBytesUntil using a delimiter and increasing my buffer array by one, however that was unsuccessful.

So in short, I am trying to send an entire 24 bytes in one go, or at the very least read them in one go and somewhere along the transmission or reading, there are errors that misplace or do not send all of the data resulting in erroneous data.

I have tried many things, minus changing the baud rate of CAN IC, and at some point got the message to get through, but it does not happen consistently.

If you are taking the time to read this and assist, thank you in advance!

----- this is the last teensy that just displays what is received on the Serial monitor  
#include  
#define XBeeSerial Serial1

CAN\_message\_t msg;  
unsigned long baud = 19200;  
const int led\_pin = 13;  
const int reset\_pin = 4;  
const int buffer\_len = 24;  
byte buffer[buffer\_len];  
unsigned char dtr;  
int read\_var, write\_var, data;  
unsigned char prev\_dtr;  
int count;

void setup()  
{  
pinMode(led\_pin, OUTPUT);  
digitalWrite(led\_pin, LOW);  
digitalWrite(reset\_pin, HIGH);  
pinMode(reset\_pin, OUTPUT);  
Serial.begin(baud);  
XBeeSerial.begin(baud);  
}

void dtr\_method() {  
dtr = Serial.dtr();  
if (dtr && !prev\_dtr) {  
digitalWrite(reset\_pin, LOW);  
delayMicroseconds(250);  
digitalWrite(reset\_pin, HIGH);  
}  
prev\_dtr = dtr;  
}  
void print\_buf(){  
for(int i=0; i \< buffer\_len; i++){  
Serial.print(buffer[i]);  
}  
Serial.println();  
}

void print\_can(){  
Serial.print("CAN1 “);  
Serial.print(“MB: “); Serial.print(msg.mb);  
Serial.print(” ID: 0x”); Serial.print(msg.id, HEX );  
Serial.print(” EXT: “); Serial.print(msg.flags.extended );  
Serial.print(” LEN: “); Serial.print(msg.len);  
Serial.print(” DATA: “);  
for ( uint8\_t i = 0; i \< 8; i++ ) {  
Serial.print(msg.buf[i]); Serial.print(” “);  
}  
Serial.print(” TS: "); Serial.println(msg.timestamp);  
}  
// done  
void baud\_update(){  
if (Serial.baud() != baud) {  
baud = Serial.baud();  
if (baud == 57600) {  
XBeeSerial.begin(58824);  
} else {  
XBeeSerial.begin(baud);  
}  
}  
}

void loop() {  
unsigned char prev\_dtr = 0;  
// first check if there is data coming from the Serial Monitor (transmitter)  
// Note: this is not used in this demonstration but here to work with XCTU for debugging pps  
read\_var = Serial.available();  
if (read\_var \> 0) {  
write\_var = XBeeSerial.availableForWrite();  
if (write\_var \> 0) {  
if (read\_var \> write\_var) read\_var = write\_var;  
if (read\_var \> buffer\_len) read\_var = buffer\_len;  
data = Serial.readBytes((char \*)buffer, read\_var);  
XBeeSerial.write(buffer, data);  
}  
}

// check if there is data going to receiver  
read\_var = XBeeSerial.available();  
if (read\_var \> 23) {  
write\_var = Serial.availableForWrite();  
if (write\_var \> 0) {  
if (read\_var \> write\_var) read\_var = write\_var;  
if (read\_var \> buffer\_len) read\_var = buffer\_len;  
memset(buffer, 0, buffer\_len);  
data = XBeeSerial.readBytes((char \*)buffer, read\_var);  
memcpy(&msg, buffer, data);  
//print\_buf();  
print\_can();  
digitalWrite(led\_pin, HIGH);  
}  
}  
//method that keeps teensy awake  
dtr\_method();  
// shown above  
baud\_update();  
}

—this is the code that receives the can message and then sends it over xbee  
NOTE: i originally had Can1.read(msg) in an if statement with availablewritebytes \> 23, but that resulted in the same three numbers being sent over and over. Not sure if this is because of baud rate, blocking functions or what. I get lost here.

#include  
#define XBeeSerial Serial1

FlexCAN\_T4 Can1;  
CAN\_message\_t msg;  
unsigned long baud = 19200;  
const int led\_pin = 13;  
const int reset\_pin = 4;  
const int buffer\_len = 24;  
byte buffer[buffer\_len];  
int count;

void setup()  
{  
// turn led  
pinMode(led\_pin, OUTPUT);  
digitalWrite(led\_pin, LOW);  
digitalWrite(reset\_pin, HIGH);  
pinMode(reset\_pin, OUTPUT);  
// being serial communication  
Serial.begin(baud);  
XBeeSerial.begin(baud);  
Can1.begin();  
Can1.setBaudRate(250000);  
}

void print\_can(){  
Serial.print("CAN1 “);  
Serial.print(“MB: “); Serial.print(msg.mb);  
Serial.print(” ID: 0x”); Serial.print(msg.id, HEX );  
Serial.print(” EXT: “); Serial.print(msg.flags.extended );  
Serial.print(” LEN: “); Serial.print(msg.len);  
Serial.print(” DATA: “);  
for ( uint8\_t i = 0; i \< 8; i++ ) {  
Serial.print(msg.buf[i]); Serial.print(” “);  
}  
Serial.print(” TS: "); Serial.println(msg.timestamp);  
}

void loop()  
{  
unsigned char dtr;  
int read\_var = 0, write\_var, data;  
unsigned char prev\_dtr = 0;

```
     Can1.read(msg);

```

// LOOK here ignore next xbeeserial  
write\_var = XBeeSerial.availableForWrite();  
if (write\_var \> 23) {  
if (read\_var \> write\_var) read\_var = write\_var;  
if (read\_var \> buffer\_len) read\_var = buffer\_len;  
memset(buffer, 0, buffer\_len);  
memcpy(buffer, &msg, buffer\_len);  
XBeeSerial.write(buffer, buffer\_len);

```
}

```

//serial and baud rate functions omitted for char count, same as previous code

EDIT – added the hardcoded can\_message\_t and example of what is being received

-----hard code can\_msg struct  
msg.flags.ext = 0;  
msg.id = 0x100;  
msg.len = 8;  
msg.buf[0] = 10;  
msg.buf[1] = 20;  
msg.buf[2] = 0;  
msg.buf[3] = 100;  
msg.buf[4] = 128;  
msg.buf[5] = 64;  
msg.buf[6] = 32;  
msg.buf[7] = 16;

– what should be received -----  
CAN1 MB: 0 ID: 0x100 EXT: 0 LEN: 8 DATA: 10 20 0 100 128 64 32 16 TS: 1088 (ts will differ)

– example what is being received –  
CAN1 MB: 8 ID: 0x4000008 EXT: 72 LEN: 4 DATA: 0 0 72 4 4 4 0 0 TS: 1088  
CAN1 MB: 8 ID: 0x48 EXT: 0 LEN: 0 DATA: 4 0 0 0 156 2 0 4 TS: 8  
CAN1 MB: 8 ID: 0x4840401 EXT: 0 LEN: 1 DATA: 0 0 4 0 10 8 156 0 TS: 1028  
CAN1 MB: 0 ID: 0x400 EXT: 72 LEN: 0 DATA: 4 4 128 4 4 4 10 0 TS: 72  
CAN1 MB: 2 ID: 0x1A1C0000 EXT: 0 LEN: 0 DATA: 74 1 10 10 70 66 38 66 TS: 4

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [December 7, 2022, 1:19pm UTC](https://forums.digi.com/t/help-sending-memcpyed-buffer-over-xbees-s2c-in-at-mode-in-one-pass/23313/2 "2022-12-07T13:19:23Z")

</div>

I don’t think this is something that can be addressed by Digi. But you might want to try using API mode. API mode is a framed based interface on the XBee that would allow what ever data you want to be sent as one packet.

If you are looking for help in your code for the 3rd party processor, you really should be contacting the 3rd party processor manufacture instead.

---

<div class="post-metadata">

**Author:** ![mb11](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@mb11](https://forums.digi.com/u/mb11)\
**Post date:** [December 21, 2022, 3:35pm UTC](https://forums.digi.com/t/help-sending-memcpyed-buffer-over-xbees-s2c-in-at-mode-in-one-pass/23313/3 "2022-12-21T15:35:48Z")

</div>

My guess is that you are sending data to the Xbee faster that it can be transmitted and therefore the Tx buffer gets corrupted. In transparent mode you either need to use CTS flow control or limit the data rate so that the Tx buffer never overflows.
