Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Echo TCP Server on Arduino based PLC


Introduction

On this post is showed how to create a server TCP on an Arduino based PLC. 


Requirements

Ethernet or 20 I/Os PLC:      Ethernet PLC     20 I/Os PLC    

Industrial Shields boards:   Industrial Shields Boards


Description

Once the server is running, any client can connect to the server. On this example it is used an M-Duino to generate the server. The example of TCP client showed before could be one of the clients.


Example

Next it is showed the Arduino IDE code:

// use Ethernet.h if you have a M-Duino V7 version
#include <Ethernet2.h>

// mac address for M-Duino
byte mac[] = { 0xBE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Ip address for M-Duino
byte ip[] = { 192, 168, 1, 100 };
int tcp_port = 5566;

EthernetServer server = EthernetServer(5566);

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

  // start server for listenign for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client.available()) {
    // read bytes from the incoming client and write them back
    // to the same client connected to the server
    client.write(client.read());
  }
}

 

Once the sketch is running on Arduino based PLC any client can connect with this echo server.

For more information see on Arduino web site the TCP functions Ethernet / Ethernet 2 library