Serial: Reading a struct sent by a C# program

OK. I’m trying to read settings sent over by a .NET program i made to update my settings in the arduino nano. The idea is to read the settings when a jumper (or pin) becomes HIGH and store the settings in the EEPROM. The function i’m having problems with is the last on in this code. Deserialize. I’m trying to get a Struct out of the bytes read by the serial stream.

template <class T> int WriteVals(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int ReadVals(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}


template <class T> int Deserialize(const byte buff[], T& value)
{
     byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = buff[i];
    return i;
}

I keep getting gibberish when i try to deserialize.

What are you trying to send? What do you receive (bytes)? And is data in .NET on a PC the same as data in C/C++ on an AVR MPU?

Cheers!

A struct with some "double" typed variables serialized into bytes from .NET

--- wait a minute, i just remember i read somewhere that arduino doesn't really support double, but float mimicked as double. Wonder if that's where the mismatch occured??

Not sure if the data types would be treated the same between the two platforms.

If your PC program sends a series of bytes to the Arduino it seems to me you have two simple options.

If the data represents numbers as ascii characters (eg '100.73') you can use atof() etc to convert them.

If the data is binary data you could create a union in the Arduino that overlays a byte array onto the struct. Receive the bytes into the byte array and use the values from the struct. You will need to check the endian system used on the PC. The Arduino uses little-endian.

In either case the examples in Serial Input Basics are simple reliable ways to receive the data.

...R

The idea is to read the settings when a jumper (or pin) becomes HIGH

That's dumb. The idea is to read the settings when the PC sends them.

Requesting the PC to send them when a switch becomes pressed is a different story.

I got it working last night. I used strings and readuntil() a delimiter that separates the values

The jumper only indicates to the arduino that setting will be updated so it should open serial port and wait for data. It’s one less unnecessary routine if update is not being done. The whole sketch is a time sensitive PWM routine.

donperry: I got it working last night. I used strings and readuntil() a delimiter that separates the values

The jumper only indicates to the arduino that setting will be updated so it should open serial port and wait for data. It's one less unnecessary routine if update is not being done. The whole sketch is a time sensitive PWM routine.

readUntil() and time sensitive are incompatible bedfellows.

...R

Right. And it wouldn't matter at that point, as the Arduino would be in "update" mode, as per my programming for the jumper pin.