Search code examples
arduinoplcmodbus-tcpcodesysst

MODBUS TCP between PLC and Arduino


I am trying to exchange data between an PLC(WAGO 750-8101) and an Arduino(UNO) with PLC as master, and the Arduino as Slave, but cant seem to get a connection.

For the Arduino I have a MINI ENC28J60 as the networkmodule, and is connected to the arduino like this:

SCK - Pin 13, SO - Pin 12, SI - Pin 11, CS - Pin 10

VCC: 3.3V

For the arduino I am using these libraries:

EtherCard

Modbus-Arduino

Master Setup

    SendRequest : WagoAppPlcModbus.FbMbMasterTcp := (   xConnect := TRUE,
                                                    sHost:= '192.168.1.88',
                                                    wPort:=502,
                                                    utKeepAlive:= ( xEnable :=TRUE,
                                                                    tMaxIdleTime := T#5S,
                                                                    tInterval:= T#2S,
                                                                    udiProbes:=5),
                                                    eFrameType := eMbFrameType.ETHERNET,
                                                    tTimeOut:= T#30S);


utQuery: typMbQuery := (    bUnitId := 88,      // slave ID, 
                            bFunctionCode:=16#4, // analog input registers
                            uiReadAddress:= 0, 
                            uiReadQuantity:=2,
                            uiWriteAddress:=0,
                            uiWriteQuantity:=0,
                            awWriteData:= [124(0)]);

xTxTrigger : BOOL;

utResponse : typMbResponse;
TimerForRequest : TON := (PT := T#5S);
HeartRate: WORD;
AirQuality : WORD;

HentSensorData : BOOL;

and code in main:

//TimerForRequest(IN:=HentSensorData);

//xTxTrigger S= TimerForRequest.Q;


SendRequest(    utQuery := utQuery,
            xTrigger := HentSensorData,   //a toggle switch on HMI
            utResponse := utResponse);

HeartRate := utResponse.awData[0];

AirQuality := utResponse.awData[1];

Slave setup (arduino):

#include <EtherCard.h>
#include <ModbusIP_ENC28J60.h>
#include <Modbus.h>

const int HeartBeat = 0;
const int Oxygen = 1;

ModbusIP mb;


void setup() {

      // The media access control (ethernet hardware) address for the shield
      byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
      // The IP address for the shield
      byte ip[] = { 192, 168, 1, 88 };
      byte dns[] = { 192, 168, 1, 1 };
      byte gw[] = { 192, 168, 1, 1 };
      byte subnet[] = { 255, 255, 255, 0 };

      Serial.begin(9600);
      Serial.println("DEBUG");
      Serial.println();
      //Config Modbus IP
      mb.config(mac, ip, dns, gw, subnet);
      Serial.println("Modbus com. setup done");
      Serial.println();
      Serial.println("SLAVE SETTINGS");
      ether.printIp("IP: ",ether.myip);
      ether.printIp("DNS: ", ether.dnsip);
      //ether.printIp("GW: ",ether.gwip);    ether.printIp("Mask: ", 
                                                          ether.mymask);
      Serial.println();

      // Add SENSOR_IREG register - Use addIreg() for analog Inputs
      mb.addIreg(HeartBeat);
      Serial.print("HeartBeatSensor added at address: "); 
      Serial.println(HeartBeat);
      mb.addIreg(Oxygen);
      Serial.print("OxygenSensor added at address: "); 
      Serial.println(Oxygen);
}


void loop() {

   //Call once inside loop() - all magic here
   mb.task();
   //Testcode
   for(int i=0;i<400; i++)
   {
       mb.Ireg(HeartBeat, i);
       mb.Ireg(Oxygen,i+2000);
       Serial.println(i);
       delay(2000);
   }
}

Output (debug) slave:

Arduino debug

I am able to get connection between the PLC and a Modbus Slave simulator (and exchange data), but when I try to connect the arduino I can't seem to get a connection.

This is the error i get in the PLC:

Error in the functionblock (master)

Any suggestions? Is it possible that the problem is that I'm using a crossover-cable (even though WAGO is set to switch mode in Ethernet settings?)


Solution

  • Solved

    The problem was sending and receiving on the Arduino-side. I connected the Arduino and the PLC to a switch. I was then able to exchange data.

    My setup that didn't work:

    Ardunio <-------> PLC (WAGO)

    My setup that did work:

    Arduino <------> switch <-----> PLC.