Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi everyone,
speaking with an omron PLC via EtherNetIP i wrote some poor instruction that returned me the list of tags and the template of each tag (structure, array or atomic). It was very difficult cause omron has no documentation about this protocol instead Allen Bradley for example.
Now i would like to replicate these 'structure' as a 'class' into VB.Net: i thougth about reflection ad make a class (then an onbject instance) during runtime.

Some Plc's structure has array field, for instance:
Class StructRegMT {
On as boolean;
NrItem as integer;
SetPointRegMT(64) as integer;
} end Class

How make this sample class during runtime?
Thanks all for help..

Matteo M.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Oct-15 16:33pm    
I answered, but... my I ask you — why? If I knew your goals, perhaps I could suggest more light-weight alternatives.
—SA
Member 11727760 10-Oct-15 16:11pm    
Queing the PLC, it answer with a list of tag. Each tag coul be a variable type like: atomic (boolean, integer, ecc), array or structure. In the latter case the PLC return also the "template" of the tag; it is like a list of member with the associated atomic type each.

Once i have recovered this 'list' i would like to translate into a VB.net class each returned structure. Cause in following operation i would like to 'read/write' some tag, and i should translate the frame of byte i'll receive. I should search in the frame the exact byte that represent the value...

This operation is mandatory because the PLC (the server) could change his structures and i have to ask him the list of templates.

Returning on topic, thanks for your reply, but i have some difficult to realize the above class with "typeBuilder" and the array. What's the syntax i should use to realize the SetPointRegMT(64) as integer field in a dynamic class?

1 solution

This is not just System.Reflection, this would be System.Reflection.Emit. And if you really need to construct a class, not just DynamicMethod, you will need to create the whole assembly on the fly. You will have to learn it all:
https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx[^],
http://www.codemag.com/article/0301051[^],
http://www.c-sharpcorner.com/uploadfile/puranindia/reflection-and-reflection-emit-in-C-Sharp[^],
http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570[^].

This CodeProject article can also be useful: Dynamic Type Using Reflection.Emit[^].

I must say, not only it will take learning of Emit itself, but it's really important to understand how CIL works. Also, emitted code is notoriously difficult to debug. Overall, it can be quite serious work.

—SA
 
Share this answer
 
v2
Comments
Member 11727760 12-Oct-15 2:53am    
Dear Sergey, i replied your previous comment. See above, and help me please about the syntax to use with a dynamic "array" field.
i tried this:

dim fv as FieldBuilder = tb.DefinField("ia", GetType(integer).Makearraytype(), fieldAttribute.Public)

but how to fix the size of the array "ia"? Should i pass the size via Argument while calling the constructor? in this case how to call it?
Sergey Alexandrovich Kryukov 12-Oct-15 10:19am    
Sorry, not clear...
—SA
Member 11727760 12-Oct-15 11:39am    
Excuse me Sergey if i wasn't clear enough.
The question is how to declare on runtime using emit the follower class
Class StructRegMT {
On as boolean;
NrItem as integer;
SetPointRegMT(64) as integer;
} end Class

then i have to instantiate an oject of this type, can you help also in this?
Sergey Alexandrovich Kryukov 12-Oct-15 12:25pm    
Nothing resembles Emit here. You have to emit the whole assembly and use it thought reflection. A good idea is to have some statically known interfaces and implement them in the dynamic assembly. Then you can use reflection to search the type which implements the interface and hence instantiate it using regular reflection.

This is not about "syntax". This is a lot about understanding of emitting code and CIL and semantic of this activity. To generate some code, you need to learn emit opcodes. You can look at the code sample on this MSDN page: https://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder%28v=vs.110%29.aspx.

This CodeProject article can also be used http://www.codeproject.com/Articles/121568/Dynamic-Type-Using-Reflection-Emit.

And yet, I'm still quite unsure that your PLC problem really requires such heavy-weight approach.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900