Hello everybody.
I'm trying to connect an Arduino Uno with a W5100 shield with a SIEMENS PAC2200 (a flowmeter device) which works in MODBUS TCP/IP.
I'm using also the example of ArduinoModbus library (Ethernet Modbus TCP Client Toggle), but trying to read the correct data I need (the first one, for example):
In theory I'm corretly connected to the device (the serial shows the message Modbus TCP Client connected), but when I try to read the data from the PAC2200, the value of modbusTCPClient.requestFrom(0x00,0x04,0x01,0x02) is always "0".
If anyone could give me a clue about what is going on, I will be very grateful.
Enclosed I include the 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}; // MAC de la tarjeta W5100 que es genérica
IPAddress ip(192, 168, 1, 47); // IP de la tarjeta W5100 que es la cliente
EthernetClient ethClient; //Strating client
ModbusTCPClient modbusTCPClient(ethClient);
IPAddress server(192, 168, 1, 100); // update with the IP Address of your Modbus server
float Total_kWh_Pos0;
float Total_kWh_Pos1;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
}
Serial.println("Inicio setup");
pinMode(4, OUTPUT);
digitalWrite(4, HIGH); // To disable slave select for SD card; depricated.
// Starting shield Ethernet
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
}
}
// Check if cable is onnected
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// Check if server is connected
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 {
Serial.println("Modbus TCP Client connected");
}
}
}
void loop() {
Serial.println("Starting the reading");
delay(1000);
Serial.println(modbusTCPClient.requestFrom(0x00,0x04,0x13,0x02) );
Total_kWh_Pos0 = modbusTCPClient.read();
Total_kWh_Pos1 = modbusTCPClient.read();
Serial.println(Total_kWh_Pos0);
Serial.println(Total_kWh_Pos1);
// wait for 3 second
delay(3000);
}escribe o pega el código aquí