libplctag / libplctag.NET

A .NET wrapper for libplctag.
https://libplctag.github.io/
Mozilla Public License 2.0
197 stars 49 forks source link

Exception Handling #322

Closed beena26g closed 1 year ago

beena26g commented 1 year ago

I am trying to use exception handling with tags. My scenario is to use a string array to read a list of tags. All tags need to be of the same data type. For example, my sample below will be of type TagDint . I would like to do some error handling on these tags for If the list of tags contains a different tag type or if the IPAddress is wrong for connection. I am using Task.WhenAll to run all tags at the same time, but also get exceptions and put them in result list - which would tell me what the exceptiona are. Currently the tag error I have been able to work with is ErrorNotFound which is when a TagDint does not exist in the plc. I am running into issue when I have Erroroutofbounds error or I have giving a wrong IPAddress. The code fails to recognize the exception and does not handle it correctly.

Below is my sample code

public class Readitem {
    public string tagName { get; set; }
    public bool isValid { get; set; }
    public string ErrorMessage { get; set; }
    public T Value;
}

string[] tags = new string[] {
    "Tag1001", // Correct Data Type DintTag but not correct tag name.
    "Tag2000", //valid tag
    "Tag3000", //valid tag
    //"BoolTag1000", // Data Type is DBoolTag
    "Tag5679[2,0]" //valid tag
};

var readResults = new List<Readitem>();

var allTags = new List();
for (int i = 0; i < tags.Length; i++) {
    var tag = new TagDint {
        Gateway = IPAddress,
        Path = Path,
        PlcType = PlcType,
        Protocol = Protocol,
        Timeout = TimeSpan.FromSeconds(Timeout),
        AutoSyncReadInterval = TimeSpan.FromSeconds(Timeout),
        Name = tags[i]
    };
    allTags.Add(tag);
}

var tasks = Task.WhenAll(allTags.Select(tag => {
    return Task.Run(async () => {
        try
        {
            readResults.Add(new Readitem() {
                Value = ConvertResult(await tag.ReadAsync()),
                tagName = tag.Name,
                isValid = true,
                ErrorMessage = string.Empty
            });
        }
        catch (LibPlcTagException ex)
        {
            readResults.Add(new Readitem() {
                Value = default (T),
                tagName = tag.Name,
                isValid = false,
                ErrorMessage = ex.Message.ToString()
            });
            throw ex;
        }
    });
}));

try
{
    await tasks;
}
catch (LibPlcTagException ex)
{
    //This should be already done in the above catch where the errormessage is added into the readResults.
    //return readResults;
}
finally
{
    return readResults;
}

Expected result set would show tagName = "Tag1001" isValid = false ErrorMessage = "ErrorNotFound" Value = null , tagName = "Tag2000", isValid = true ErrorMessage = "" Value = 111 , tagName = "Tag3000", isValid = true ErrorMessage = "" Value = 222 ,
tagName = "BoolTag1000" isValid = false ErrorMessage = "ErrorOutofBound" Value = null , tagName = "Tag5679[2,0]", isValid = true ErrorMessage = "" Value = 2 ,

This code works fine if I don't have erroroutofbound error - i.e. all tags are tagDint but I would like to handle situations where wrong tag types are sent to read.

Your help is appreciated.

Thank you

timyhac commented 1 year ago

Could the issue be that it's catching LibPlcTagException instead of AggregateException when awaiting tasks,

beena26g commented 1 year ago

Tried using Aggregrate but below is screen shot of error I am getting for where it is looking for Libplctag.LibPlcTagException for ErrorOutofBounds. From my sample, it is happening for BoolTag1000 (which is a wrong tag type that is created as TagDint)

Capture

Thank you.

timyhac commented 1 year ago

I just formatted your code so I could read it more easily.

To me, this is looks like a problem with incorrect async code, as opposed to an issue with libplctag.NET.

Some pointers:

timyhac commented 1 year ago

Hi @beena26g - I hope that the above has been helpful, if you would like more assistance please create an issue that takes into account my earlier comments. Thanks for using libplctag.NET