From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

modbus tcp - receiving messages from slave

I'm developing a Modbus TCP application to acquire data from an Accuvim power meter with an ethernet module... my first introduction to Modbus.  I had envisioned using the example master vi to talk to the power meter,  and to receive the desired data from it... This is where I'm at:

 

I've downloaded the Modbus Library for LabVIEW and see how the master and slave example vi's work together.  The power meter user manual defines the function commands and the data register locations, and I think I see how to input those commands into the master vi to send a query to the power meter (slave) to get the data I want.  In the master vi example (MB Ethernet Example Master.vi), I see where the "Registers to Write" event structure writes these message to the slave.  Here's the problem, while the user manual discusses the query/response format, I don't see where the example master vi has any facility for receiving message responses from the slave.  I only see where the master vi reads the slaves Input Registers. So now I'm stumped, how are messages received from the slave?  

Greg R.
0 Kudos
Message 1 of 27
(5,008 Views)

When you send a Read command to the slave, the data comes back in the registers array.  If you are dealing with boolean registers, it will come back in the boolean array.  If you are dealing with input registers or holding registers, it will come back in the numeric array.

 

If you see how the master VI reads the slaves input registers, that is what you are looking for.  What other messages are you expecting to receive from the slave?

0 Kudos
Message 2 of 27
(5,002 Views)

Some of my basic assumptions are obviously wrong...

 

Do I run both master and slave vi's when I'm communicating with the slave hardware (power meter)?

 

Do I send the command frame (in my case, read Analog Holding Registers, 03) via the "Registers to Write" on the master vi?

 

Thanks,

Greg

Greg R.
0 Kudos
Message 3 of 27
(4,996 Views)

If you are communicating with a real world device that acts as a slave, then you only run the Master Query VI on your PC.  You would only run the slave VI if you wanted you PC to act as a slave communicating with something else that is acting as a master.

 

When you are reading registers, you only need to send the command, the starting address, and the number of registers.

When you are writing to registers, you need to send all of that, and also the array that contains the data you are writing.  The number of elements in the array smust match the number of registers you are writing to.

 

0 Kudos
Message 4 of 27
(4,976 Views)

Ok, so I'm on the right page, that I run the master VI and talk to the power meter or slave. 

 

But I'm still hung up on the whole query/response discussion that I see in all the literature...,

 

Query:

03: Function Code (read Analog Output Holding Regiseter)

4042: Data Address of first register

0002: Number of registers requested

The TCP layer creates a MBAP header, attaches this in front of the above and sends it to the slave, then

 

Response:

03: Function ID echoed

04: Byte Count to follow

46: Register 1 high

40: Register 1 low

E6: Register 2 high

66: Register 2 low

Again, ethernet communication module on the meter forms a MBAP header in front.

 

I can see this functionality in your attached figure of the "Reading VI" (although I'm using the TCP version), where the read command is formatted such as to issue the query, and the slave responds by sending the data into the "Registers" array.  That makes sense. 

 

My problem is that this option is not available in the MB Ethernet Example Master.vi" utility that I'm trying to use.  Maybe I need to change the functions in the "[0] Timeout" event to do a "Read Holding Registers" instead of a "Read Input Registers". I'll try that...

 


gr

Greg R.
0 Kudos
Message 5 of 27
(4,967 Views)

All of my experience has been with serial modbus devices.  I haven't worked with any ethernet modbus devices.  So I can't comment MBAP headers or anything like that.

 

Definitely check whether you are working with holding registers or input registers.  Holding registers are code 3, and the registers are in the 40,000 series.  Input registers are code 4 and are in the 30,000 series.  You can read or write to holding reigsters, but you can only read from input registers.

 

Also, The initial digit of the register is not used in the LV modbus command.  Also, the registers are zero-based in LV, while usually begin with 1 in the slave device.  So if you want to read from register 40,007 for example.  You will put a 6 in for you starting address in the Modbus command.

 

For more details on the Modbus protocol, go to this link http://www.modbus.org/tech.php

0 Kudos
Message 6 of 27
(4,963 Views)

Hi Raven,

For the development of Modbus, Response Message is there any ways to have the message out in a hexadecimal form like the one in CAS Modbus Scanner rather than have it in an array format, also the response message is full proof meaning to say that it can tell there is an error in it???


@RavensFan wrote:

If you are communicating with a real world device that acts as a slave, then you only run the Master Query VI on your PC.  You would only run the slave VI if you wanted you PC to act as a slave communicating with something else that is acting as a master.

 

When you are reading registers, you only need to send the command, the starting address, and the number of registers.

When you are writing to registers, you need to send all of that, and also the array that contains the data you are writing.  The number of elements in the array smust match the number of registers you are writing to.

 


 

0 Kudos
Message 7 of 27
(2,749 Views)

I don't know what CAS Modbus scanner is.

 

And asking about whether a response is an array or hexadecimal form is like asking whether a story is about a bear or written in Italian.  The two concepts are unrelated.  If you want to change the display appearance of the results, you can do that.  If you want to reformat it into a string of hexadecimal characters, then you can use the functions provided in LabVIEW to do that.

0 Kudos
Message 8 of 27
(2,728 Views)

Hi Raven,

An example of the CAS Scanner is in here of the query and response message in hexadecimal. What I am trying to ask that as you can see in the second picture of the Modbus TCP/IP Response Message I am building the message which is not the right way as I did not consider an error case. Right now, Read Holding Registers, the values output it is an array format, how can I have a response message as shown in the CAS Scanner by using the Modbus Library API without building the message in my VI. Thanks

Download All
0 Kudos
Message 9 of 27
(2,717 Views)

Hi guys,

I am trying to program a Unidrive VSD by using Modbus TCP/IP. Now I am facing a problem of the Modbus TCP/IP Response Message portion. From the Modbus Library API, using Read Holding Registers, the output value is an array. How do I form a Modbus Response Message like the CAS Modbus Scanner as shown in the screenshot? Currently, I build up the response message which is not the right way as I failed to consider an error case. Pls kindly advise. Thanks.

Download All
0 Kudos
Message 10 of 27
(2,753 Views)