WIZnet Developer Forum

Arduino SERVER TCP. Send data packet struct

Hello

I have an Arduino DUE+ IoShield A (W5500 from Wiznet). IDE 1.6. Wiznet Ethernet library

I want the Arduino to behave as a TCP Server. This server waits for a client, and sends the client an info packet.

The code is divided in two files, one the server code, and other a .h with the struct of the packet
Server.


#include "packet.h"
#include <Ethernet.h>


 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0x00, 0x08, 0xDC, 0x4D, 0xD1, 0x55 };  
//the IP address for the shield:
byte ip[] = { 10, 1, 1, 3 };    
// the router's gateway address:
byte gateway[] = { 10, 1, 1, 1 };//? Esto no esun gateway, es un nodo.
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
int Port= 5000;

EthernetServer server = EthernetServer(Port);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();

  Info.PacketID= 0x0E;
  Info.ID_track=1;
  Info.Direction=0x00;
  Info.num=10;
  Info.stateA[0]=0x00;
  Info.stateA[1]=0x00;
  Info.stateA[2]=0x00;
  Info.stateA[3]=0x00;


void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {

    server.write(Info,sizeof(Info));
  }
  
}

packet.h


struct Packet{
  byte PacketID;
  byte ID_track;
  byte Direction;
  byte num;
  byte stateA[4];

};


struct Packet Info;

It gives error in server.write() , no matching function for call… I think that it is beacuse Info is not only an array of bytes but a struct… but I’m not sure

Any help about the server and how to send a data packet structure is welcome.
Thanks

Hello, svia.

Arduino Library of W5500 chip is ethernet2.

In 1.6 you need to add ethernet2 directly.
From 1.8.0, however, you can add ethernet2 through the arduino library manager.

First, add / change the library.

And the write () function of the EthernetServer class has a fixed parameter type as shown below.

size_t EthernetServer::write(const uint8_t *buffer, size_t size)
https://github.com/adafruit/Ethernet2/blob/master/src/EthernetServer.cpp

Therefore, the custom type of the structure can not be the parameter of the function.
If you want to manage data like structs, it’s a good idea to define and manage array numbers or manage them with pointers.

Of course, if you really want to use a structure, you can use C ++ Function Overloading to change the parameters and reconstruct the function.

I hope that helps.

Best regard,
Kei

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.