READ BOOLEAN TAG

404 views
Skip to first unread message

Diego Omar Careaga

unread,
Oct 5, 2018, 1:31:55 PM10/5/18
to libplctag
Hi Dear,

I am trying to get the value of TAG, but i have a problem when I read the Boolean type...
Can you tell me how to read boolean values?

Best reguards,

Diego Careaga

My code in C#

//CODE

try
            {
                // creates a tag to read B3:0, 1 item, from SLC ip address 192.168.0.100
                var tag = new Tag("192.168.1.101", "1:0", CpuType.LGX, "OTRO", DataType.Int16, 1);

                using (var client = new Libplctag())
                {
                    // add the tag
                    client.AddTag(tag);
                    // check that the tag has been added, if it returns pending we have to retry
                    while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING)
                    {
                        Thread.Sleep(100);
                    }

                    // if the status is not ok, we have to handle the error
                    if (client.GetStatus(tag) != Libplctag.PLCTAG_STATUS_OK)
                    {
                        Console.WriteLine($"Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag))}\n");
                        Thread.Sleep(5000);
                        return;
                    }
                    // Execute the read
                    var result = client.ReadTag(tag, DataTimeout);
                    // Check the read operation result
                    if (result != Libplctag.PLCTAG_STATUS_OK)
                    {
                        Console.WriteLine($"ERROR: Unable to read the data! Got error code {result}: {client.DecodeError(result)}\n");
                        return;
                    }
                    // Convert the data
          
                    var TestDintArray0 = client.GetInt16Value(tag, 1* tag.ElementSize); // multiply with tag.ElementSize to keep indexes consistant with the indexes on the plc
                  
                    // print to console
                    Console.WriteLine("VALUE_TAG: " + TestDintArray0);
                }
            }
            finally
            {
                Console.ReadKey();
            }

RESULT.png

Kyle

unread,
Oct 5, 2018, 6:11:48 PM10/5/18
to libplctag
The data returned is in the form of a byte array with indexes starting at zero. You can read from it using the convenience methods to get bytes, two-byte integers, four-byte integers or four-byte floating point numbers. In this case, you appear to be asking to read the tag "OTRO" and you are telling the library that it is a 2-byte integer. You do not show any other output, so I assume that that it does not fail and you then read a 2-byte integer value from offset 2 (2 bytes into the result byte array). The code 

1 * tag.ElementSize


should be equivalent to 1 * 2, so 2.

That read is returning an error because it is out of bounds.   The error returned when reading a signed integer is the minimum value possible with that size of integer.  In this case that is -32768, which is what you are getting.  Unfortunately, to make the API easier to use and wrap, I returned the values directly rather than using pointers for the value and returning a status code.

So, try reading from offset 0 instead of 2 and see if you get something.   If that works, then you are also not reading a BOOL.

I strongly suggest you try with the tag_rw tool.  You can use this tool to build up a valid tag creation string and test out the different sizes and parameters without having to run an edit, compile, debug cycle.

Individual BOOL values are returned as single bytes with 0 for false and -1 for true.   If the BOOLs are in an array, it is very different.  In that case, the array is packed into some whole number of 32-bit words (DINT).   When you read a single boolean out of an array like that, you have to figure out which 32-bit word you want and which bit in that word.  It is a pain.   If you need help reading boolean arrays let me know.

I hope this helps!

Best,
Kyle

Greatech Integration

unread,
Sep 11, 2021, 8:06:52 PM9/11/21
to libplctag
hi im having issue getting boolean from array boolean of 32. i able to get 0-7 but unable to get after that. need help in reading boolean after index 7?

Kyle

unread,
Sep 11, 2021, 8:15:45 PM9/11/21
to libplctag
Hi, what tag string are you using?  What programming language and platform?

Can you print out the tag size after reading the tag?  If this is for an Allen-Bradley PLC, boolean arrays are bit packed but also are only allocated in terms of 32-bit (ControlLogix) or 16-bit values (PLC/5, SLC 500, MicroLogix).

Best,
Kyle

Greatech Integration

unread,
Sep 11, 2021, 8:33:41 PM9/11/21
to libplctag
hi, im using c#, yes this is for  Allen-Bradley PLC, boolean arrays  in terms of 32-bit (ControlLogix). Im try to read/write the 32 bit array boolean.

Kyle

unread,
Sep 12, 2021, 1:24:08 PM9/12/21
to libplctag
I would raise a separate conversation here or an issue in the libplctag.NET project.   If you are using a wrapper, particularly that one, Jody and Tim can help you much better than I can.  They have some special boolean decoding helper classes for this case, if I remember correctly.

If you are reading or writing using the low level C DLL functions, then you would need to check the array size when you read it.  Make sure it comes back as 4 (bytes).  Then you can use the plc_tag_get/set_bit() functions to read or write the individual bits in the tag.

Best,
Kyle

Greatech Integration

unread,
Sep 13, 2021, 4:22:37 PM9/13/21
to libplctag
Hi Kyle,

I able to read/write now. I think i miss out the element count. When creating the tag i should divide by 32 so get element count 1 and datasize 4. I able to read/write to every bit in the tag. The c# wrapper have special boolean decoding helper. after several trial. I'm able to understand how the helper working. 

Thanks

Reply all
Reply to author
Forward
0 new messages