Help writing a library (passing in an unknown structure for serial writing)

Status
Not open for further replies.

KrisKasprzak

Well-known member
All,

I've been using small UART based transcievers (E50-TTL-100) with great success. I can read / write and even program them. RadioHead library will work with and E32, but I want different functions and capability--hence I'm writing my own.

The Transceiver serial is created with something like
#define ESerial Serial1

initialization is then
ESerial.begin(9600);

I send complete data structures (Data.volts, Data.amps, Data.ID, etc..) and this code works fine
ESerial.write((uint8_t*) &Data, sizeof(Data));

My library creates an object using (which is library code leveraged from RadioHead)
EBYTE Transceiver(&ESerial, 4, 5, 6);

library constructor is
EBYTE::EBYTE(Stream *s, uint8_t PIN_M0, uint8_t PIN_M1, uint8_t PIN_AUX)

OK...enough background. In the library I'm writing I want a function like the following so i can pass a data structure but as I wan't to let others have the lib, how do you pass a structure when you don't know anything about it?

void EBYTE::SendTheData( ? *TheStructure) {
_s->write((uint8_t*)&TheStructure, (uint16_t) sizeof(TheStructure));
}

Thanks in advance.
 
Just change your function signature to:
Code:
void EBYTE::SendData(const void *data_, unsigned size_)
{
  s_->write(data_, size_);
}
 
Thanks Jarkkol, I implemented and got the code to compile, but it doesn't work. I can pass in a data structure but the reported sizeof is 2 bytes. I'm told that the size of “void *” is the size of a pointer to memory (2 bytes on this operating system/compiler).

I'm still looking for a way to pass a structure into my function but not requiring the name of the structure. Since this lib can be used by anyone structure names will be different.

MichaelMeissner. I may have to implement your idea but I'm hoping to keep the end user code nice and pretty like: EBYTE.init(); EBYTE.setChannel(); EBYTE.SendData().
 
That's why you need the size as a function parameter as well. Consider memcpy, which is:
Code:
memcpy(void *dest, void *src, size_t size)

In your case, JarkkoL recommended:
Code:
void EBYTE::SendData(const void *data_, unsigned size_)
{
  s_->write(data_, size_);
}

You need to use the size input parameter instead of sizeof.
 
All,

I could really use some more complete help here, a line of code may help an experienced c programmer but it's making me more confused. I'm not a c programmer and have no idea how to implement memcopy. maybe just correct my code below?

My code in the .ion file
// structure created above
// object to Transceiver created above
Transceiver.GetTheData(&TheData, sizeof(TheData));


prototype in .h

void SendTheData(const void *TheStructure, uint16_t *size_);


code in the cpp file

void EBYTE::SendTheData(const void *TheStructure, uint16_t *size_) {

_s->write((uint8_t *) TheStructure, uint16_t size_);


}
 
looks like i got it working

ino file
Transceiver.GetTheData(&TheData, sizeof(TheData));


.h file
void GetTheData(const void *TheStructure, uint16_t size_);

.cpp file
void EBYTE::GetTheData(const void *TheStructure, uint16_t size_) {

_s->readBytes((uint8_t *) TheStructure, size_);


}
 
Status
Not open for further replies.
Back
Top