Arduino client using Modbus TCP with a server device and Rs485 serial with other servers

can you have an Arduino master using modbus tcp with one device and Rs485 serial with others? I have the Ethernet rev2 that I can get to communicate and send data to a Redlion G308 HMI but wanted to also setup an Rs485 communication with other Arduino using MAX485 Module (RS-485 TTL to RS485 MAX485CSA) It’s taken a huge learning curve to get this far and could use some help.
Thanks

what do you mean by Modbus TCP master? Modbus TCP has server and clients.

Yes this can be done. Its tricky though. I did mitsubishi plc as tcp master and arduino as slave + arduino master for x 2 485 rtu slave devices. But not arduino as tcp slave. You can use the arduino modbus tcp example code for this.

@Arrjax

Hope this will help.

You did a log of work there but it looks like I am in a little over my head. I can't find
"#include <ModbusSerial.h>" in the library and don't have any experience with GitHub and downloading files to use in my IDE.
So far I only have the Mega 2560 with Ethernet shield as a Client, toggling coils in some tags I set up on a Redlion HMI. I guess i should start with just trying to get holding registers and see if i can read data from the HMI first.

#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library

#include <ArduinoModbus.h>
const int REG = 528; // Modbus Hreg Offset
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address of Client (will be dependent on your local network)

byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(xxx,xxx,x,xxx);
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress server(xxx,xxx,x,xxx); // update with the IP Address of your Modbus server
const int inputPin3 = 3;
int ValveOpenInput = 0;

void setup() {
   pinMode(inputPin3, INPUT);

   
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  
}

void loop() {
ValveOpenInput = digitalRead(inputPin3);

  if (!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Attempting to connect to Modbus TCP server");

  
    if (!modbusTCPClient.begin(server, 502)) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
   // client connected
      Serial.println("Modbus TCP Client connected");
    }
  } else {
    
  }
   //write to the coil at address 0x00 the value of 0x01
  if (ValveOpenInput == 1 ) {
      (!modbusTCPClient.coilWrite(0x00, 0x01));
    delay(1);
  } else {
      (!modbusTCPClient.coilWrite(0x00, 0x00));
    delay(1);
  }
}

Yes you are correct, Modbus Client.

So far I only have the Mega 2560 with Ethernet shield as a Client, toggling coils in some tags I set up on a Redlion HMI set as a server. Haven’t figured out how to read registers or inputs in the Arduino so I can toggle bits on the HMI. I’ll look at the examples again and see if I can make heads of it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.