❔ Deserialize serial data to struct (port from C)

Hello, I'm not sure how you do this in C#: I have this code I'm porting to C#. It's basically a stream from a serial port that needs to be de-serialized into a struct. This is the C code for the struct:
#define AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT 512
#define AETHER_X6100CTRL_BB_FRAME_MAGIC 0xAA5555AA

typedef struct
{
bool resync : 1;
bool tx : 1;
bool chg : 1;
bool vext : 1;
uint32_t reserved : 28;
} aether_x6100ctrl_bb_frame_flags_t;

typedef struct AETHER_X6100CTRL_PACKED
{
uint32_t magic; /*!< Every frame starts with AETHER_X6100CTRL_BB_FRAME_MAGIC */
aether_x6100ctrl_fcomplex_t bb_iq_samples[AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT];
aether_x6100ctrl_bb_frame_flags_t flags;
uint8_t reserved_1;
uint8_t tx_power;
uint8_t vswr;
uint8_t alc_level;
uint8_t vext;
uint8_t vbat;
uint8_t batcap;
uint8_t reserved_2;
uint32_t atu_params;
uint32_t reserved_3;
uint32_t reserved_4;
uint32_t reserved_5;
uint32_t reserved_6;
uint32_t crc;
} aether_x6100ctrl_bb_frame_t;
#define AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT 512
#define AETHER_X6100CTRL_BB_FRAME_MAGIC 0xAA5555AA

typedef struct
{
bool resync : 1;
bool tx : 1;
bool chg : 1;
bool vext : 1;
uint32_t reserved : 28;
} aether_x6100ctrl_bb_frame_flags_t;

typedef struct AETHER_X6100CTRL_PACKED
{
uint32_t magic; /*!< Every frame starts with AETHER_X6100CTRL_BB_FRAME_MAGIC */
aether_x6100ctrl_fcomplex_t bb_iq_samples[AETHER_X6100CTRL_BB_FRAME_IQ_SAMPLES_COUNT];
aether_x6100ctrl_bb_frame_flags_t flags;
uint8_t reserved_1;
uint8_t tx_power;
uint8_t vswr;
uint8_t alc_level;
uint8_t vext;
uint8_t vbat;
uint8_t batcap;
uint8_t reserved_2;
uint32_t atu_params;
uint32_t reserved_3;
uint32_t reserved_4;
uint32_t reserved_5;
uint32_t reserved_6;
uint32_t crc;
} aether_x6100ctrl_bb_frame_t;
You can explore the code better if you want on this header file and this C file. This is a bit of an on-going work that's a bit rough. Since I want to move to C# and all the code is doing is writing and reading to serial, I thought I could just do away with the base C(++) library. What tools in C# do you use to define and de-serialize a struct like this? Thanks
5 Replies
james441
james4412y ago
This is for hardware control for sending/ recieving radio signals right? I've done a bit of this before for hobby stuff, not professional so take this with a big pinch of salt. You might get struggle using C# for this work. Radio sigal sending and recieving relies on quite precise timings that garbage collected languages might not work reliably for. Languages like C and C++ are really the ideal tool for this kind of job.
james441
james4412y ago
Having said that, if you want to try out C#, I'd take a look at https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter.deserialize?view=net-7.0 - it says obsolete beacuse of potential security risks, but I think it's the only choice you have, and hopefully you are reading from hardware you trust 😛
LordKalma (CT7ALW)
it's for controlling the actual radio 🙂 but yeah, thanks for the tip 🙂
james441
james4412y ago
So you want to transmit or recieve a struct that looks like the one you posted through the radio module?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server