Industrial Communications

cancel
Showing results for 
Search instead for 
Did you mean: 

Modbus communicate with PLC

Solved!
Go to solution

Hello everyone,

 

We are trying to add function code 20 to read the file record in order to read the bulk memory of PLC. We used modbus library for Labview to communicate with PLC with Modbus TCP/IP, but when I addes the function code 20 to the input cluster on the MB Ethernet Master Query Reading Input Register Palette, the progam shows error. I will be really appreciated if anybody could help out with this problem!!

Hope everybody have a nice weekend.

 

Best Regards,

 

Sophia

0 Kudos
Message 1 of 12
(9,528 Views)

Hi Sophia, 

 

If you post your code and the error I can take a quick look. However NI doesn't support modification to the Modbus library. 

 

Jesse Dennis
Engineer
INTP
0 Kudos
Message 2 of 12
(9,525 Views)

You might be interested in NI's new Modbus library. I had no the time to test it so far.

0 Kudos
Message 3 of 12
(9,490 Views)

I wasn't mentioning that because I wasn't sure when it was going live. It is a complete re-write, you should give it a try. 

 

The code is object oriented, the work to add a new code should be minimal, but you will need to understand object oriented code. 

Jesse Dennis
Engineer
INTP
0 Kudos
Message 4 of 12
(9,483 Views)

As Jesse mentioned the effort required to add a new function code is pretty minimal, but you do need to understand where to plug it in. If you are interested in discussing that, please go to ni.com/labs and click on the discussion link at the top. In that thread there is already a basic description of where all the pieces of code go.

 

I would be very excited to see you try to add additional functionality, as making that easy was one of the goals of the re-write--I'd like to understand where we failed or where we succeeded 🙂

0 Kudos
Message 5 of 12
(9,477 Views)

Hello Jesse,

 

Thank you very much for your reply. I attached my modification in this post.

 

I am really a newbie in this field and what I need is to read contiguous data in bulk memory (file record), so I set the Byte Count to a constant 7.

 

I am wondering do I need to change unit identifier to 1?

 

Sophia

 

 

Download All
Message 6 of 12
(9,473 Views)

Hey Sophia,

 

Instead of the data you are using now, you will have to provide the function with data similar to that described here, on page 32:
http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf

Specifically, 

Byte count

const 0x6

(2byte) file number 0x0000 to 0xFFFF

(2byte) record number

(2byte) record length

 

That is, it has nothing to do with the unit identifier or byte count. Think of it like this: right now, you are trying to call int func (int, int, int, int) and, instead of calling this function, you are calling int diffFunc (int, int, int, int). It sure does look the same, but the code won't actually work because the meaning of each of those ints is different.

 

The nice thing about modbus is that it is actually pretty simple. For now, why don't you take a look at the spec doc above and the existing code. Compare the behavior of func code 0x3 in the spec compared to the code. Try to understand why the code works as is, and then try to compare 0x3 to the file function code. 

 

Thanks,

Daniel

0 Kudos
Message 7 of 12
(9,459 Views)

Well, I now really wish I could edit that post. I saw that you were re-using the register count inputs and saw the 1 prepended, but for some reason totally missed the second set of values you were prepending. And now I feel dumb 🙂

 

Sorry about that.

 

What might actually be helpful in that spec is sec 7 pg 48. The modbus library simply adds 6000 to the modbus exception, which means you are getting exception 2. This means that inputs are invalid or out of range. Looking at your code this second time, that doesn't make sense--everything seems well-formed. You are asking for 20 registers from the 1st file. So to me this leads to the question, are you sure that the device supports these specific inputs and has a file "1"? Have you tested this command with another master?

 

Last but not least, and this really shouldn't be the problem, but is there any chance your slave device supports a different endianness from LabVIEW. If we are sending "1" and it sees 0000 0001 0000 0000, maybe that error does make sense. Again this shouldn't be the case since modbus defines endianness for everything else, but its worth looking at the slave's manual just to be sure.

 

Thanks,

Daniel

0 Kudos
Message 8 of 12
(9,441 Views)
Solution
Accepted by topic author sophia3

Actually...is that a coersion dot on the second input to 'build array'? It is hard to tell from the picture. If so, what is the data type of this wire?

Encoding.jpg

That should be an array of U16s, because the 'quantity" and "starting address" inputs are U16. However if the "1" value is a larger data type like U32, then labview may be upconverting the entire array which would result in a larger data set than expected and could result in error 2. Can you check on this?

Message 9 of 12
(9,427 Views)

Hey Daniel,

 

Just as you predicted, the data type for value "1" was 32 bits long int. As soon as I change the data type to U16. The program gives me 10 registers reading from bulk memory. I am very appreciated your help. Thank you very much. Wish you all the best!

=D

 

Sophia

Message 10 of 12
(9,416 Views)