c++ - Float data to uint8_t and memcpy , returning null -


i have float variable , needs passed uint8_t function .

how convert float original value .

code:

bool setanalog(uint8_t number, float voltage, messagepriority priority, callback clbck) {     uint8_t args[2];     args[0] = number;     memcpy(&(args[1]),&voltage,sizeof(float));      ptcloopoutmessage* message = parent()->getloopoutmessage(this,                                                              this->address(),                                                              _outputvoltage,                                                              "dacvoltage",                                                              args,                                                              sizeof(args),                                                              device,                                                              priority);     message->setcallback(clbck);      return processoutmessage(message); } 

i try float original value uint8_t array , need assign probe[channel]

uint8_t channel = message->getoutmessage()->getdata()->data[0]; unsigned char* value = &message->getoutmessage()->getdata()->data[1]; memcpy(&__output.output.probe[channel].dac, value, sizeof(float)); 

i value 0,

please help

try this:

struct myargs {   uint8_t channel;   float voltage; } *args = new myargs(); args->channel = number; args->voltage = voltage; 

then pass this:

....getloopoutmessage(....., (uint8_t *)args, sizeof(*args), ....) 

and read data out this:

struct myargs {   uint8_t channel;   float voltage; } *args = (struct myargs *)message->getoutmessage()->getdata()->data;  uint8_t channel = args->channel; __output.output.probe[channel].dac = args->voltage;  delete args; 

note i've used new/delete here, not local stack storage, because appear using data in callback, i'm guessing data has been overwritten (with zero, happens) before read it.


if new/delete inappropriate (getloopoutmessage makes copy of data), this:

struct {   uint8_t channel;   float voltage; } args = {number, voltage}; 

then pass this:

....getloopoutmessage(....., (uint8_t *)&args, sizeof(args), ....) 

and read data out this:

struct mydata {   uint8_t channel;   float voltage; } *args = (struct mydata *)message->getoutmessage()->getdata()->data;  uint8_t channel = args->channel; __output.output.probe[channel].dac = args->voltage; 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -