0

I'm developing an application using Visual Studio (C#).

My interface has to send a structure to the server (written using ansi C but not by me and I can't modify it) running on Unix-like OS (vxWorks).

The structure contains integers, enums, long, double, char and structures data.

How can I send this structure from C# to C?

Searching on Google I found something called "serialization" but I didn't understand how to use it correctly on visual c # and, least of all, on ansi c (Unix-like).

Thanks.

C Structure declared in Unix-like OS (vxWorks):

enum TYPE_OP 
{
  START_INITIALIZE   = 0,
  STOP_INITIALIZE    = 1,
  IDENTIFY           = 2,        
  GET_VALUE          = 3,
  SET_VALUE          = 4,
  SET_RSE_MODE       = 5,
  GET_VALUE_DOUBLE   = 6,
  SET_VALUE_DOUBLE   = 7,
  SET_STRING         = 8,
  GET_VALUE_FLOAT    = 9,
  SET_VALUE_FLOAT    = 10,
  SET_TARGET         = 11,
  GET_TARGET         = 12,
  GET_TASK_STATUS    = 13
}; 

typedef struct 
{  
   long    addr;    //address of an element in GPIB chain (ex. 1, 13, etc.)
   long    value;   //number (ex. enumerative position)
   double  dvalue;  //a double number
} DESCR_DATA;


struct VAR_DATA   
{  
   int     len ;
   enum TYPE_OP type;   
   char         name[100]; //Name of the element in GPIB chain
   DESCR_DATA   data;
};

C# structure declared in Windows:

public enum TYPE_OP
{
    START_INITIALIZE = 0,
    STOP_INITIALIZE = 1,
    IDENTIFY = 2,
    GET_VALUE = 3,
    SET_VALUE = 4,
    SET_RSE_MODE = 5,
    GET_VALUE_DOUBLE = 6,
    SET_VALUE_DOUBLE = 7,
    SET_STRING = 8,
    GET_VALUE_FLOAT = 9,
    SET_VALUE_FLOAT = 10,
    SET_TARGET = 11,
    GET_TARGET = 12,
    GET_TASK_STATUS = 13
}

public struct DESCR_DATA
{
    public long addr;
    public long value;
    public double dvalue;
}

public struct VAR_DATA
{
    public int len;
    public TYPE_OP type;
    public string name;
    public DESCR_DATA data;
}
VAR_DATA vardata = new VAR_DATA();
    
Massimo Costanzo
  • 382
  • 3
  • 18
  • 1
    You might have a look over here: [How to convert a structure to a byte array in C#?](https://stackoverflow.com/questions/3278827/how-to-convert-a-structure-to-a-byte-array-in-c). This will show how to convert a struct to a byte array, so you can send it via TCP. – Jeroen van Langen Jun 15 '22 at 08:15
  • Use something like protobuf or json – fredrik Jun 15 '22 at 08:18
  • Some of those commands seem to have variable-length data requirement. What exactly goes into `name` and `data`? Data format is only part of your problem, you are also going to have to worry about the application's _protocol._ –  Jun 15 '22 at 08:25
  • Your main issue seems to be the C# part, as the C application is fixed. Divide the big problem into smaller ones, like using netcat or similar to understand the receiving part, then the transformation of the C# structure into bytes in general, then into the required format of the data, and finally how to transmit them. – the busybee Jun 15 '22 at 09:21
  • Now I'm using marshalling and I've only problems with double and long values. Integer and char values are received and printed correctly, but double and long variables are not. I also tried with a simple struct with only integers, char and double, but the printed value in 'C' of the double is always equal to 0 and it throws error on all the variables that follow it (not the correct values), but, if I change the position of the double and place it at the bottom, all the integers and Chars are printed correctly – Massimo Costanzo Jun 17 '22 at 11:17

0 Answers0