libplctag-csharp icon indicating copy to clipboard operation
libplctag-csharp copied to clipboard

Here is the CODE

Open rishidamco opened this issue 7 years ago • 12 comments

This is the I am calling from client:


    const int DataTimeout = 5000;
    static void Main(string[] args)
    {
        var plcClient = new PlcTag();
        string ipAddress = "101.60.131.238";
        string path = "1,0";
        string name = "F8:01";
        int elementSize = 4;
        int elementCount = 1;
        int debugLevel = 1;
        string error = null;
        Tag tag = new Tag(ipAddress, path, CpuType.PLC5, name, elementSize, elementCount, debugLevel);
        var rc = plcClient.ReadTag(tag, DataTimeout);
        if (rc != PlcTag.PLCTAG_STATUS_OK)
            error = plcClient.DecodeError(rc);
        /* read the data */
        for (int i = 0; i < tag.ElementCount; i++)
        {
            float value = plcClient.GetFloat32Value(tag, (i * tag.ElementSize));
        }
    }

The ReadTag method is giving error: An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll }


public class PlcTag: IDisposable { private readonly Dictionary<string, IntPtr> _tags; public PlcTag() { _tags = new Dictionary<string, IntPtr>(); } public int ReadTag(Tag tag, int timeout) { var result = plc_tag_read(_tags[tag.UniqueKey], timeout); return result; } [DllImport("plctag.dll", EntryPoint = "plc_tag_read", CallingConvention = CallingConvention.Cdecl)] static extern int plc_tag_read(IntPtr tag, int timeout);

}


rishidamco avatar Sep 09 '17 18:09 rishidamco

You have to call AddTag before using it. See the example

client.AddTag(tag);

/* let the connect succeed we hope */
while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING)
{
    Thread.Sleep(100);
}

mesta1 avatar Sep 09 '17 18:09 mesta1

Thank for the update, now I added the code and getting error saying PLCTAG-ERROR-TIMEOUT

Same tag works file when I call directly using PLC tag rw program.

Sent from my iPhone

On Sep 9, 2017, at 2:31 PM, Michele Cattafesta [email protected] wrote:

You have to call AddTag before using it. See the example ''' client.AddTag(tag);

/* let the connect succeed we hope */ while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } '''

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

rishidamco avatar Sep 09 '17 19:09 rishidamco

You should use this Tag constructor and adapt it to Plc5 var tag = new Tag("192.168.0.100", CpuType.SLC, "B3:0", DataType.Int16, 1);

mesta1 avatar Sep 09 '17 19:09 mesta1

Still getting the same error.

Sent from my iPhone

On Sep 9, 2017, at 3:17 PM, Michele Cattafesta [email protected] wrote:

You should use this Tag constructor. var tag = new Tag("192.168.0.100", CpuType.SLC, "B3:0", DataType.Int16, 1);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

rishidamco avatar Sep 09 '17 19:09 rishidamco

Any update I am connecting PLC 5.

Sent from my iPhone

On Sep 9, 2017, at 3:17 PM, Michele Cattafesta [email protected] wrote:

You should use this Tag constructor. var tag = new Tag("192.168.0.100", CpuType.SLC, "B3:0", DataType.Int16, 1);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

rishidamco avatar Sep 10 '17 15:09 rishidamco

Can you post the updated code ? Also if you write string name = "F8:1"; instead of "F8:01" does it work ? I suspect there is a problem with the tag declaration.

mesta1 avatar Sep 11 '17 22:09 mesta1

Hi there,

Finally I was able to resolve all my network issues etc and able to get the data out.

One question, how we read string data.

Regards,

Rishi

Sent from my iPhone

On Sep 10, 2017, at 11:36 AM, [email protected] wrote:

Any update I am connecting PLC 5.

Sent from my iPhone

On Sep 9, 2017, at 3:17 PM, Michele Cattafesta [email protected] wrote:

You should use this Tag constructor. var tag = new Tag("192.168.0.100", CpuType.SLC, "B3:0", DataType.Int16, 1);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

rishidamco avatar Sep 21 '17 19:09 rishidamco

I forgot to push the example. Check it here: https://github.com/mesta1/libplctag-csharp/blob/master/Slc500Strings/Program.cs You can read maximum 1 string per request, so don't try to increase the element count, it will not work.

mesta1 avatar Sep 21 '17 20:09 mesta1

Thanks for the update, it's working fine for me now.

Any idea if I can get all the available tags list out from PLC?

Regards,

Rishi

On Thu, Sep 21, 2017 at 4:25 PM, Michele Cattafesta < [email protected]> wrote:

I forgot to push the example. Check it here: https://github.com/mesta1/libplctag-csharp/blob/master/ Slc500Strings/Program.cs You can read maximum 1 string per request, so don't try to increase the element count, it will not work.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mesta1/libplctag-csharp/issues/2#issuecomment-331271238, or mute the thread https://github.com/notifications/unsubscribe-auth/AcQv8CYPlGx1ELdNKme6hVJD3g3DBoVrks5sksYugaJpZM4PSE82 .

rishidamco avatar Sep 23 '17 01:09 rishidamco

Hi, i am trying to communicate with AB Micro820. It showing error in reading Tag. Created Tag as follows: Tag tag = new Tag(IpAddress,"1,0", CpuType.LGX, Address, DataType.Int32, 1); ERROR: Unable to read the data! Got error code -33: PLCTAG_ERR_REMOTE_ERR please provide solution.

Thanks.

sajjaharikrishna avatar Jun 05 '18 12:06 sajjaharikrishna

I just wanted to say thank you for your youtube video and having this be open source. This is super helpful.

ghost avatar Jul 09 '18 00:07 ghost

Hi, Thanks For Your Efforts I have tried one of your example of slc string. but its not working i am using MicroLogix1400 and c# to read string values from plc but in my case status is OK but cant read the tag So it will very help full if i get to know that how to read string data type Thanks In Advance

shyamsrg avatar Oct 17 '19 12:10 shyamsrg