Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here is the CODE #2

Open
rishidamco opened this issue Sep 9, 2017 · 12 comments
Open

Here is the CODE #2

rishidamco opened this issue Sep 9, 2017 · 12 comments
Labels

Comments

@rishidamco
Copy link

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);

}


@mesta1
Copy link
Owner

mesta1 commented Sep 9, 2017

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);
}

@rishidamco
Copy link
Author

rishidamco commented Sep 9, 2017 via email

@mesta1
Copy link
Owner

mesta1 commented Sep 9, 2017

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);

@rishidamco
Copy link
Author

rishidamco commented Sep 9, 2017 via email

@rishidamco
Copy link
Author

rishidamco commented Sep 10, 2017 via email

@mesta1
Copy link
Owner

mesta1 commented Sep 11, 2017

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.

@rishidamco
Copy link
Author

rishidamco commented Sep 21, 2017 via email

@mesta1
Copy link
Owner

mesta1 commented Sep 21, 2017

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.

@rishidamco
Copy link
Author

rishidamco commented Sep 23, 2017 via email

@sajjaharikrishna
Copy link

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.

@ghost
Copy link

ghost commented Jul 9, 2018

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

@shyamsrg
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants