Skip to main content
Arduino

Return to Question

Commonmark migration
Source Link

This is [an exerpt and variant from this library][1] [1]: https://github.com/madsci1016/Arduino-EasyTransfer/tree/master/EasyTransferan exerpt and variant from this library

This is [an exerpt and variant from this library][1] [1]: https://github.com/madsci1016/Arduino-EasyTransfer/tree/master/EasyTransfer

updated to reflect current state of code
Source Link
Womble
  • 199
  • 2
  • 9
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
#define PAYLOAD_SIZE 255
struct SEND_DATA_STRUCTURE
{
 int blinks;
 int pause;
 uint8_t *data;data[PAYLOAD_SIZE];
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup()
{
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 //initalize not so random number generator
 randomSeed(analogRead(0));
 
}
 
void loop()
{
 //mydata.blinks = random(5);
 //mydata.pause = random(5);
 //Create data payload to transfer
 char* payload = "Hello World!";
 //Fill array with data
 strncpy((char *)mydata.data, =payload, PAYLOAD_SIZE);
 //Print sample
 Serial.print(uint8_t*(char*)payload;mydata.data);
 
 //send the entire struct
 TransferObject.sendData();
 
 delay(5000);
}
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
#define PAYLOAD_SIZE 255
struct RECEIVE_DATA_STRUCTURE
{
 int blinks;
 int pause;
 
 uint8_t *data;data[PAYLOAD_SIZE];
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
void setup(){
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 
}
void loop()
{
 //check and see if a data packet has come in. 
 if(TransferObject.receiveData())
 {
 Serial.print("active");
 //print data payload
 Serial.print((char*)mydata.data);
 }
 
 //you should make this delay shorter then your transmit delay or else messages could be lost
 delay(250);
}
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
struct SEND_DATA_STRUCTURE
{
 int blinks;
 int pause;
 uint8_t *data;
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup()
{
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 //initalize not so random number generator
 randomSeed(analogRead(0));
 
}
 
void loop()
{
 //mydata.blinks = random(5);
 //mydata.pause = random(5);
 //Create data payload
 char* payload = "Hello World!";
 mydata.data = (uint8_t*)payload;
 
 //send the entire struct
 TransferObject.sendData();
 
 delay(5000);
}
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
struct RECEIVE_DATA_STRUCTURE
{
 int blinks;
 int pause;
 
 uint8_t *data;
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
void setup(){
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 
}
void loop()
{
 //check and see if a data packet has come in. 
 if(TransferObject.receiveData())
 {
 //print data payload
 Serial.print((char*)mydata.data);
 }
 
 //you should make this delay shorter then your transmit delay or else messages could be lost
 delay(250);
}
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
#define PAYLOAD_SIZE 255
struct SEND_DATA_STRUCTURE
{
 int blinks;
 int pause;
 uint8_t data[PAYLOAD_SIZE];
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup()
{
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 //initalize not so random number generator
 randomSeed(analogRead(0));
 
}
 
void loop()
{
 //mydata.blinks = random(5);
 //mydata.pause = random(5);
 //Create payload to transfer
 char* payload = "Hello World!";
 //Fill array with data
 strncpy((char *)mydata.data, payload, PAYLOAD_SIZE);
 //Print sample
 Serial.print((char*)mydata.data);
 
 //send the entire struct
 TransferObject.sendData();
 
 delay(5000);
}
#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
#define PAYLOAD_SIZE 255
struct RECEIVE_DATA_STRUCTURE
{
 int blinks;
 int pause;
 
 uint8_t data[PAYLOAD_SIZE];
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
void setup(){
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 
}
void loop()
{
 //check and see if a data packet has come in. 
 if(TransferObject.receiveData())
 {
 Serial.print("active");
 //print data payload
 Serial.print((char*)mydata.data);
 }
 
 //you should make this delay shorter then your transmit delay or else messages could be lost
 delay(250);
Source Link
Womble
  • 199
  • 2
  • 9

Casting a struct member between uint8_t and char, with regard to serial transfer

Im simply trying to pass my payload to another device that has the same architecture and struct. I'm using UART to transfer, and modeling the protocol from EasyTransfer.

When I call Serial.print((char*)mydata.data); in Transfer it prints Hello World as expected.

Calling that same line in Receive prints a '?' to console. This may be some background behavior with Arduino, or I'm just not casting/handling the pointers correctly. How can I get this print out to function properly?

Transfer Sketch

#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
struct SEND_DATA_STRUCTURE
{
 int blinks;
 int pause;
 uint8_t *data;
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup()
{
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 //initalize not so random number generator
 randomSeed(analogRead(0));
 
}
 
void loop()
{
 //mydata.blinks = random(5);
 //mydata.pause = random(5);
 //Create data payload
 char* payload = "Hello World!";
 mydata.data = (uint8_t*)payload;
 
 //send the entire struct
 TransferObject.sendData();
 
 delay(5000);
}

Receive Sketch

#include <EasyTransfer.h>
//create object
EasyTransfer TransferObject; 
//Struct contents must be idential to struct from other board
struct RECEIVE_DATA_STRUCTURE
{
 int blinks;
 int pause;
 
 uint8_t *data;
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
void setup(){
 Serial.begin(9600);
 //#define details(name) (byte*)&name,sizeof(name) 
 TransferObject.begin(details(mydata), &Serial);
 
 pinMode(13, OUTPUT);
 
}
void loop()
{
 //check and see if a data packet has come in. 
 if(TransferObject.receiveData())
 {
 //print data payload
 Serial.print((char*)mydata.data);
 }
 
 //you should make this delay shorter then your transmit delay or else messages could be lost
 delay(250);
}

This is [an exerpt and variant from this library][1] [1]: https://github.com/madsci1016/Arduino-EasyTransfer/tree/master/EasyTransfer

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /