LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help setting up modbus

Solved!
Go to solution

I'm trying to set up a modbus communication.

init_ser.PNG

If I understood correctly, here I set up the slave. SlaveId, ComMdb and BaudrateMdb are given by .init file earlier in the code. 

My problem is that when I use the "ReadMdb.vi" or similar ones the output registers only contain 0. I tried to retrieve execution codes to see if I could find what was the problem but it also returns 0.

 

I'm still a beginner with LabVIEW so it might be a simple mistake but I can't figure it out.

Download All
0 Kudos
Message 1 of 6
(1,429 Views)

What is the Slave ID for your device?

 

(I think the purple modbus library assumes 1.  But it is kind of buried in there.  It really should be made accessible on the connector panel for the initialization.  If yours is not 1, then you need to use a Set Unit ID property node.)

 

What register address did you put in the control and what quantity?  (Your VI is saved with default values of 0, so I don't know what you are actually entering.)

 

However, I'd expect you'd be getting an error if either of these things were wrong.  Are you SURE you aren't getting an error?  Put an indicator on that error wire.

0 Kudos
Message 2 of 6
(1,418 Views)

My slave ID is indeed 1. 

 

As for addresses :

data_log.PNG

This is part of a VI that allows me to send the ID of a log I want to retrieve, then the slave returns an array of data.

this is an example of the addresses and quantities that are put in the control.

 

I'm not getting any errors, was this what you meant for the location of the indicators ?

main.PNGread.PNGwrite.PNG

 

EDIT: I changed "New serial slave" to "New serial master" and it seems to work a bit now, i can connect to the slave and registers aren't just 0 now but still I don't get the data i'm looking for.

I made the change after reading this thread (in french) https://forums.ni.com/t5/Discussions-au-sujet-de-NI/Protocole-ModBus-RTU-%C3%A0-l-aide-des-API-LabVI...

   

0 Kudos
Message 3 of 6
(1,404 Views)
Solution
Accepted by topic author FrougeurPro

Yes.  That is what I meant for the error indicators.

 

Let's look at register address.

In the first sequence frame, you are using your Write SubVI.  Your address is 4353.  That should correlate to a holding register of 404354 in the device manual.  Is that true?  Likewise, you are reading 61 address from 4096.  So that should correlate with 404097 in the device manual.  Is that true?

(Modbus defines registers with prefixes like 3 for input registers, 4 or holding registers, none, and 1,  maybe some others, but 4 is the most common.  Those registers are addressed starting at 1.  The underlying protocol, and the LabVIEW  implementation is 0-based and ignors the prefix.  So most manuals,if you want 400011, you drop the 4 and subtract 1, you'll be looking at address 10.)

 

In your first picture, you have a Rube Goldberg going on there in the second frame.  You are typecasting the array of U16's to a string.  Then grabbing the first 2 bytes.  Then typecasting that back to an integer again.  What is the datatype of that blue 0 constant?  It must be U16 since I don't see any coercion dots, but that can be hard to check with a picture.  So why do a integer to string to integer conversion?

 

You should put an indicator on that blue wire coming coming out of the Read subVI.  Are you getting any values in the array?

In the VISA Read, why typecast the array to a string, then use string length to compare to the quantity of registers times 2.  Just use Array Size to compare to the quantity of registers and get rid of that string conversion!

 

For the subVI's, I don't like how you have stacked sequence structures.  Replace it with a Flat Sequence Structure.  Then eliminate the flat sequence structure and use wires such as the error wire to get you data flow correct.

 

Does this device provide any software of its own?  It is often a good idea to use that to make sure the device works with the software it comes with.  It can help you figure out why your code may not be working.

Message 4 of 6
(1,384 Views)

Concerning the Rube Goldberg (love the expression btw) and the weird conversions, I'm adding a new functionnality to an old software from around 2009 that used what I think was user made VIs for the modbus communication. However, those VIs were too restrictive and didn't allow me to read diagnostic registers or exception codes for example. So I tried recreating them with NI VIs, although i mostly just rewrote the VIs the same way using different VIs since I'm a bit inexperienced with modbus and Labview.

 

In the first picture the original VI made the string typecasting within the VI, I forgot to add it so I made it outside, I know it's not efficient or anything but I'm trying to not temper too much with the code.

 

In the VISA Read I typecast the array to a string then compare it with the quantity of registers times 2 because, well because as I said above the original VI did it this way and it worked so I kept it this way.

 

Why replace stacked sequences with flat sequences ? Is there a reason other than clarity to do it ?

0 Kudos
Message 5 of 6
(1,345 Views)

Yes.  Clarity.  Stacked Sequence Structures hide code unnecessarily.  And with sequence locals needed to get wires from one frame to the next, it leads to backwards running wires.

 

Even NI has agreed stacked sequences are a bad idea and have removed them from the palettes.  If necessary, they still work and you can still find them, but they are well hidden to discourage their use.

0 Kudos
Message 6 of 6
(1,298 Views)