0
include SPI.h
include Ethernet.h
include Modbus.h
include ModbusIP.h

//Modbus Registers Offsets (0-9999)
const int SENSOR_IREG = 100;
//Used Pins
const int sensorPin = A0;

//ModbusIP object
ModbusIP mb;

long ts;


// Set Port to 502
EthernetServer server = EthernetServer(502);

void setup() {
  //Assign the modbus device ID.  
  regBank.setId(1);

    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  

    byte ip[] = { 192, 168, 1, 177 };  

    mb.config(mac, ip);

    // Add SENSOR_IREG register - Use addIreg() for analog Inputs
    mb.addIreg(SENSOR_IREG);

    ts = millis();

}

void loop() {

   //Call once inside loop() - all magic here
   mb.task();


   if (millis() > ts + 2000) {  
       ts = millis();

       mb.Ireg(SENSOR_IREG, analogRead(sensorPin));
   }
}

I am trying to read data from the first analog pin through ModbusTCP using SimplyModbusTCP but I keep getting errors when I poll my Arduino. I am using an Arduino UNO with a Ethernet Shield W5100 and Modbus Library. I am request data from Ip address 192.168.1.177 Port:502 Slave:1 and Function Code:03. The error I get is "Error 66 occurred at TCP Read in SMT8.0.vi. LabVIEW: The network connection was closed by the peer."

8
  • did you test the Ethernet shield with some basic Ethernet library example?
    – Juraj
    Oct 5, 2018 at 14:34
  • Yes I did and it worked fine. I can even ping the unit through cmd.
    – Michael H.
    Oct 5, 2018 at 14:36
  • did you set the register address 100? try 101 in client too
    – Juraj
    Oct 5, 2018 at 14:39
  • Tried both, with same results
    – Michael H.
    Oct 5, 2018 at 14:40
  • Removed that and I am still getting the same result
    – Michael H.
    Oct 5, 2018 at 14:45

2 Answers 2

0

You should not create the Ethernet server. The library creates the server and your server takes the port first.

Modbus function code to read Input registers is 04.

3
  • I removed that line and still get the same results
    – Michael H.
    Oct 5, 2018 at 14:45
  • Tried function code 4 and got the same result
    – Michael H.
    Oct 5, 2018 at 14:53
  • try again register 101
    – Juraj
    Oct 5, 2018 at 14:58
0

Looks like SimplyModbus was the problem, used Modbus poll and was able to read data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.