Modbus TCP master library

Watcher:
Thanks Juraj!

I am having some troubles testing your code though. Maybe I dont understand it very well.

eg. what is this ?

static NetClient modbus;

Already removed the two library includes and added SPI.h and Ethernet.h

PS: Hay this is cool.. It looks like we are all 3 online now!

OK, now removed NetClient above and added a global

EthernetClient modbus;

and also some Global vars for various vars that are missing and it now compiles..

Still not sure how to use it though :slight_smile:

you must know the modbus registers of your system and understand Modbus TCP protocol.
for example readInverter request two registers from address 40083 described in inverter specs and uses them to calculate the inverter AC output

YEs, I understood that... My inverter is an SMA one... going through the modbus spec now.

Where does the inverter IP go ?

Watcher:
Where does the inverter IP go ?

it is there in the snippet as SymoAddress

YEs got it ..Thanks you...

Sorry to have to ask all this.... but just to be sure :

modbusRequest(METER_UID, 40490, 1, regs);

METER_UID is the inverter's modbus address (in my case 126)
40490 is the register number
1 is the number of registrers to read
regs is where to store the result, in this case of length 1 Int (2 bytes)

Is the above correct ?

Cant get the inverter to respond for some reason...

uid is a device address. in my case the inverter has address 1 and smart meter has address METER_UID. on Fronius I needed to enable the Modbus TCP (check box in web panel)

Thanks ..

Could you also please explain this packet frame :

byte request[] = {0, 1, 0, 0, 0, 6, uid, 3, (byte) (addr / 256), (byte) (addr % 256), 0, len};

in particular the first 5 bytes ...

Transaction identifier 2 bytes For synchronization between messages of server and client

Protocol identifier 2 bytes 0 for Modbus/TCP

Length field 2 bytes Number of remaining bytes in this frame

Unit identifier 1 byte Slave address (255 if not used)

Function code 1 byte Function codes as in other variants

Data bytes n byted Data as response or commands

ok...

I must be doing something wrong...

I ve tried to read a few registers of length 1 int and they all give back the value of 4 while the res variable returns -10..

Not sure what it means...

did you check if Modbus TCP is enabled on inverter?

Yes I have enabled it.

Does the -10 result in the res variable mean anything to you ?

Also is this

if (modbus.read() == -1)

universal for all modbus devices or specific to yours ?

yes. it has a define on top of the snippet. connect didn't success.

Do you have Java skills? I have a java version you could try from a computer

EDIT: I put it here for future readers. The problem was resolved in other thread. It had nothing to do with the sketch or modbus protocol. Two devices in network had the IP address of the inverter.

Nah.... no Java here :slight_smile:

try on computer to connect with telnet client to ip of the inverter on port 502

doesnt connect either...but maybe its nit designed to respond to telnet requests.

I can ping the inverter ok though

telnet 502 should connect.

I found this. Do not know if it applies.

Procedure:

  1. Start Sunny Explorer on the computer and create a Speedwire system (see Sunny Explorer user manual).
  2. Log into the Speedwire system as Installer.
  3. Select the SMA inverter to be configured in the system tree.
  4. Select the tab Settings.
  5. Select the parameter group External Communication.
  6. Select [Edit].
    :ballot_box_with_check: You will see the categories TCP Server and UDP Server under the parameter group Modbus.
  7. To activate the TCP server, make the following settings in the group Modbus > TCP Server:
    • In the Activated drop-down list, select the entry Yes.
    • If necessary, change the port in the Port field (default setting: 502).

Juraj:
telnet 502 should connect.

I found this. Do not know if it applies.

Procedure:

  1. Start Sunny Explorer on the computer and create a Speedwire system (see Sunny Explorer user manual).
  2. Log into the Speedwire system as Installer.
  3. Select the SMA inverter to be configured in the system tree.
  4. Select the tab Settings.
  5. Select the parameter group External Communication.
  6. Select [Edit].
    :ballot_box_with_check: You will see the categories TCP Server and UDP Server under the parameter group Modbus.
  7. To activate the TCP server, make the following settings in the group Modbus > TCP Server:
    • In the Activated drop-down list, select the entry Yes.
    • If necessary, change the port in the Port field (default setting: 502).

Thats correct. Thats exactly what i did to enable modbus on both tcp and udp.

Update:

Telnet connects on port 502!

Arduino still returns -10 !

Tried to use the same sketch and tried to connect to another Telnet server at port 23 and still doesnt connect. Must be something wrong with the connect function...
Will try again tomorrow..

does the ethernet shield connect to nerwork? got IP address?

Good morning!

yes.. I can also ping the Ethernet shield from the computer