danomagnum / gologix

Ethernet/IP client library for Go inspired by pylogix that aims to be easy to use. Supports being a client and a class 3 / class 1 server.
MIT License
32 stars 8 forks source link

[QUESTION] How I can get the Tag variable type from the PLC? #17

Closed brunoluizkatz-NETZSCH closed 2 months ago

brunoluizkatz-NETZSCH commented 5 months ago

Hello, our system design currently lacks a mechanism to identify variable/tag types. As a result, I am unable to execute the Read(tag, &v) function with v set to the correct type before initiating communication with the Rockwell PLC.

I would like to verify if there is any existing method or recommended practice that addresses the identification of tag/variable types. Today I'm trying each type by the "most used" type we have at the PLCs and caching it to be reused.

danomagnum commented 5 months ago

It depends on if you need the variable type in the PLC or the equivalent go type.

When the reads occur from the PLC it does send the type back, but I don't know that I expose this anywhere (But I can if needed). The Read_single function will give you the value of a tag without having to know what type it is before hand.

value, err := client.Read_single("tagname", gologix.CIPTypeUnknown, 1)
if err != nil {
    log.Printf("error reading testint. %v", err)
}
// do whatever you want with the value
log.Printf("tag has value %v of type %T", value, value)
// outputs "tag has value 65 of type int32"

This will work for elementary tags (DINT, INT, BOOL, etc...) If you try to read a UDT/structure you'll get a byte array returned (I think - I will test this when I get a chance) and you'll have to parse that out yourself. If you try to read an array tag you'll only get the number of items you specify.

There is also a function to read all the tags and their types out of the controller, but it may have some bugs in it. Look at the "ReadAllTags" example for info on how to use that. By looking at the KnownTag structs for each tag it finds you can see what type it is, what size the array is if it is an array tag, etc...

danomagnum commented 2 months ago

Closing for inactivity.