libplctag.NET icon indicating copy to clipboard operation
libplctag.NET copied to clipboard

Access Violation When writing Bools

Open tneon1 opened this issue 4 years ago • 6 comments

I have been using libplctag.net 1.0.4 and am currently upgrading to the latest version. I noticed that the bool tags would not read and implemented the suggested fix in a older posting using the following class.

Tag Name: PV.Buttons.Goto.CAMWizard Ip Address: 192.168.1.3 Path: "1,0" PLC Type: ControlLogix Protocol: ab_eip timeout: 1000 ms

public class CustomBoolPlcMapper : IPlcMapper {

    public int? ElementSize => 1;

    public PlcType PlcType { get; set; }
    public int[] ArrayDimensions { get; set; }

    public int? GetElementCount() => 1;

    bool IPlcMapper<bool>.Decode(Tag tag) => tag.GetUInt8(0) == 1;

    void IPlcMapper<bool>.Encode(Tag tag, bool value) => tag.SetUInt8(0, value == true ? (byte)1 : (byte)0);
}

I added this to my C# projects name space and the bool tags read, but when I write a bool I get a Access Violation error intermittently. My project is a console project that loops to a poll function to read and write to the PLC. I have attached the full code as well. I am not sure if I am implementing this incorrectly.

thank You for any help you can provide Tom Violation Error.docx

tneon1 avatar Dec 15 '21 13:12 tneon1

Here is an update, so I pulled the CustomBoolMapper class form the projects program.cs and created a separate class in the project called CustomeBoolMapper.cs and doing this seems to correct the access violation issues..

using System; using System.Collections.Generic; using System.Linq; using System.Text; using libplctag; using libplctag.DataTypes;

namespace WindowsFormsApp7 { public class CustomBoolPlcMapper : IPlcMapper {

    public int? ElementSize => 1;

    public PlcType PlcType { get; set; }
    public int[] ArrayDimensions { get; set; }

    public int? GetElementCount() => 1;

    bool IPlcMapper<bool>.Decode(Tag tag) => tag.GetUInt8(0) == 1;

    void IPlcMapper<bool>.Encode(Tag tag, bool value) => tag.SetUInt8(0, value == true ? (byte)1 : (byte)0);
}

}

tneon1 avatar Dec 20 '21 13:12 tneon1

I would be surprised if moving a class to another file had an impact on this - are you able to see if accessing other kinds of tags in addition to this bool tag makes a difference.

timyhac avatar Dec 21 '21 10:12 timyhac

Can you trying running your code with the latest stable version (1.0.13)? There shouldn't be any breaking changes between that and the version you're on and I vaguely remember some bool improvements sometime in the recent past...

jkoplo avatar Dec 22 '21 05:12 jkoplo

@tneon1 an update to the core library has been released which may have some affect on this issue and has been released with libplctag.NativeImport v1.0.30 - can you try to reproduce the issue after updating?

timyhac avatar Jan 10 '22 10:01 timyhac

Sorry for the delay. I will try the latest version and see if the issue goes away

Thank You

tneon1 avatar Jan 13 '22 18:01 tneon1

@tneon1 - Is this resolved?

jkoplo avatar May 28 '22 00:05 jkoplo