12-29-2015 03:01 AM
Hi
I have built a class that shuld handle a schneider advantys modbus modules, but I have some trouble with the referene for the modbus vi's (NI Modbus Library).
I got a parrent clas and a lot of childs, one for each module.
This is where I obtain a reference for TCP master .
Here I select the child:
This is the child:
And here is the error I got from the Modbus read vi:
Is it not possible to carrie the modbus reference in the class?
regards Bjarne
12-29-2015 03:11 AM
It looks like you are ontaining reference in New TCP master, but you never pass that reference to child object in the Child selector.
12-29-2015 04:31 AM
Yes, but why?
I thought that the reference should be on the class wire?
12-29-2015 04:47 AM
When I started learning OOP few years ago, it took me a while to understand the difference between class and object.
You have the reference in Parent object, then you ignore that and take (new) Child object. Which dosen't have that reference, because it was in the parent object. Data is not magically shared between objects.
12-29-2015 04:57 AM
Should it not inherit it from the parrent?
I have tried this but with no luck
12-29-2015 07:39 AM
There should only be 1 reference, right? If that is the case, then you need to set it up once. I think I would not have that reference in an object, but in a global variable that the other classes can access.
12-29-2015 08:42 PM
12-30-2015 04:03 PM - edited 12-30-2015 04:05 PM
@Briton wrote:
Should it not inherit it from the parrent?
I have tried this but with no luck
You're still accessing the parent's data in this case. In that case structure I assume you have multiple cases with multiple objects. If you use that (try to merge different classes into one wire, either through a case structure, build array, or similar) the wire automatically gets converted up the hierarchy into the parent class. This is difficult to see so what I find helpful is:
-Hover over each segment of the wire with context help up -- this will show you where the wire changes from Child.lvclass to Parent.lvclass.
-Change the color of each class' wire (in class properties, from the project window). This makes your code look nicer and will help you see where classes change.
So anyway, labview is changing the wire type on you and so you're pulling parent.instance out and then writing back to parent.instance NOT child.instance.
Thats another important point. In LabVIEW as in other object oriented languages, children inherit the data of their parent. So by having parent.instance you automatically have child.instance without adding any data to your class (you can see this in context help). The thing that I think people get confused about and really makes classes in labview kind of a pain is that parent data is by default not accessible to children. So that child wire has a modbus instance in it, and that one is the one you want, but you aren't allowed to get at it. To get around this, you need to make an accessor method for your parent class which gives the child class access to the modbus instance. In this case (and 90% of other cases), I'd recommend just read accessors. If you aren't familiar with this, the help can walk you through it:
http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/creating_mem_vis/#member_access
01-05-2016 08:35 AM
Thanks everybody:)
I made to VI's (VI for data member access) one read and one write in the parrent class:
After that I made a "VI for override" in parrent and all child classes:
That did the trick
Regards Bjarne
07-18-2017 03:56 PM
A cleaner OO solution would be to create the specific child instance you need before creating the connection, then call the connection method from the parent class on the child class's wire.