Hello everyone,
I want to know that how can I add a Unit ID in the sketch while using a Modbus TCP/IP. I read on the arduino modbus page that (As a rule of thumb, RTU communication is multipoint and therefore the ID of the unit involved in the communication needs to be specified. TCP is point to point using the IP address and therefore there is no need for an ID in the parameters.) So, the ID is not necessary but if I still want to have an Unit ID then how can I add it in the following example code?
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
// 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 will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer ethServer(502);
ModbusTCPServer modbusTCPServer;
const int ledPin = LED_BUILTIN;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet Modbus TCP Example");
// 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.");
}
// start the server
ethServer.begin();
// start the Modbus TCP server
if (!modbusTCPServer.begin()) {
Serial.println("Failed to start Modbus TCP Server!");
while (1);
}
// configure the LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// configure a single coil at address 0x00
modbusTCPServer.configureCoils(0x00, 1);
}
void loop() {
// listen for incoming clients
EthernetClient client = ethServer.available();
if (client) {
// a new client connected
Serial.println("new client");
// let the Modbus TCP accept the connection
modbusTCPServer.accept(client);
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
// update the LED
updateLED();
}
Serial.println("client disconnected");
}
}
void updateLED() {
// read the current value of the coil
int coilValue = modbusTCPServer.coilRead(0x00);
if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coild value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}
Secondly in the above example sketch, do I need to configure the CS pin by uncommenting the Ethernet.init(5); // MKR ETH shield or not? If yes, why?
Looking forward for the kind assistance. Thank you
Bilal