Sending multiple data over TCP

Hi

First giving a an overview of the whole thing i’m going to do :

there is a PC that is running a C# program(a simple TCP listener) which is the server and

6 arduino clients(using w5100 ethernet shield ) . server is sending time and date to the clients(almost

every Second), clients displaying it in a 16x2 character LCD and sending 4 voltage values from

analog pins and if a keypad number received interrupt the display and show the numbers,then send

that whole number too. (if my explanation is not clear please let me know)

In short i should send following data structure :

typedef struct
{ 
            char ID='1' ;            // this variable specifies the device sending data
            char Personnel_Num[16]; // getting from Keypad    
   volatile int sensorValueA0;      // reading volatages using analog  pins
   volatile int sensorValueA1;
   volatile int sensorValueA2;
   volatile int sensorValueA3; 
}xferStruct_type;

xferStruct_type Struct_payload;

since microcontroller CPUs do memory alignments, reading and separating variables of this structure in PC could be hard .

a flashback of C language, i know that by using unions we can extract a variable size of different bytes to the bytes making it (separating each byte)

typedef union
{
  volatile int sensorValueA0;
  unsigned char byteVal[sizeof(int)];
  
}xferUnion_type;

xferUnion_type Union_payload;

and then send each byte to the PC :

 int sensorValue = analogRead(A0);

  Union_payload.sensorValueA0 = sensorValue; // assigning our union member to analogRead

for(int i=0 ; i < (sizeof(int)) ; ++i)
  {
      client.write(Union_payload.byteVal[i]);
  };

although reading data is easier in this method but writing union for all of my variable will make some verbose and unprofessional code !

the question is what is the best way to send multiple data to my C# program in PC ? ( a non-blocking way to do this )

by the way if user presses a key in keypad, reading that Personnel Number can interrupt reading the
voltages should i use some kind of multi-tasking techniques?

I appreciate any help and idea.

there is write(buffer, size) function

you can client.write((const byte*) &var, sizeof(var))

meaning send the bytes from this address. &var is the address of the variable var
var can be basic type like int, or struct or array (then without &)

but you must decode it right on the PC side

Juraj:
there is write(buffer, size) function

you can client.write((const byte*) &var, sizeof(var))

meaning send the bytes from this address. &var is the address of the variable var
var can be basic type like int, or struct or array (then without &)

but you must decode it right on the PC side

it seems some wrong type casting is happening !

how can i send my structure as a pack ?

compiling this code :

#include <SPI.h>
#include <Ethernet.h>

#define  PORT   23000
//-------------------------------------------------------------------------------------------------

byte mac[] = 
{
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

IPAddress ip(192, 168, 100, 20);

// Enter the IP address of the server you're connecting to:
IPAddress server(192, 168, 100, 2);

EthernetClient client;




//----------------------------------------------------------------------------------------------------
#pragma pack(1)

typedef struct
{ 
           char ID='1' ;            // this variable specifies the device sending data
           char Personnel_Num[16]; // getting from Keypad    
  volatile int sensorValueA0;      // reading volatages using analog  pins
  volatile int sensorValueA1;
  volatile int sensorValueA2;
  volatile int sensorValueA3; 
}xferStruct_type;

#pragma pack()

xferStruct_type Struct_payload;

//-----------------------------------------------------------------------------------------------------

void setup() 
{
  
   Serial.begin(9600);
   Ethernet.begin(mac, ip);
   
   if (client.connect(server, PORT)) 
   {
   Serial.println("connected");
   } 
   else 
    {
   // if you didn't get a connection to the server:
    Serial.println("connection failed");
   }
}

void loop() 
{
 Struct_payload.sensorValueA0 = analogRead(A0);
 Struct_payload.sensorValueA1 = analogRead(A1);
 Struct_payload.sensorValueA2 = analogRead(A2);
 Struct_payload.sensorValueA3 = analogRead(A3);
 delay(10);

  if (client.connected()) 
  {
     client.write( (const byte*) (Struct_payload),sizeof(Struct_payload));
  }


}

got this error :

main:68:50: error: invalid cast from type 'xferStruct_type' to type 'const byte* {aka const unsigned char*}'

       client.write( (const byte*) (Struct_payload),sizeof(Struct_payload));

                                                  ^

exit status 1
invalid cast from type 'xferStruct_type' to type 'const byte* {aka const unsigned char*}'

pass and cast the pointer to the struct.

client.write( (const byte*) &Struct_payload, sizeof(Struct_payload));