Now I could get it.
using System;
using System.Collections.Generic;
using System.Threading;
using LibplctagWrapper;
namespace LgxSimple
{
class Program
{
const int DataTimeout = 5000;
static void Main(string[] args)
{
var client = new Libplctag();
const string PLC_IP = "192.168.1.233";
const string PLC_PATH = "18,192.168.1.233";
const CpuType PLC_TYPE = CpuType.nx1p2; // I created this CPUType nx1p2
var tag1 = new Tag(PLC_IP, PLC_PATH, PLC_TYPE, "tag1", DataType.Int16, 1);
var tag2 = new Tag(PLC_IP, PLC_PATH, PLC_TYPE, "tag2", DataType.Float32, 1);
var tag3 = new Tag(PLC_IP, PLC_PATH, PLC_TYPE, "tag3", DataType.Int16, 1);
var tag4 = new Tag(PLC_IP, PLC_PATH, PLC_TYPE, "tag4", DataType.Int16, 1);
var tags = new List<Tag> {
tag1,
tag2,
tag3
};
var tagsBool = new List<Tag> {
tag4
};
/* create the tag(s) */
foreach (var tag in tags)
{
client.AddTag(tag);
}
foreach (var tag in tagsBool)
{
client.AddTag(tag);
}
/* let the connect succeed we hope */
foreach (var tag in tags)
{
while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING)
{
Thread.Sleep(100);
}
if (client.GetStatus(tag) != Libplctag.PLCTAG_STATUS_OK)
{
Console.WriteLine($"Error setting up '{tag.Name}' internal state. Error {client.DecodeError(client.GetStatus(tag))}\n");
return;
}
}
foreach (var tag in tagsBool)
{
while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING)
{
Thread.Sleep(100);
}
if (client.GetStatus(tag) != Libplctag.PLCTAG_STATUS_OK)
{
Console.WriteLine($"Error setting up '{tag.Name}' internal state. Error {client.DecodeError(client.GetStatus(tag))}\n");
return;
}
}
/* get the data */
int rc;
foreach (var tag in tags)
{
rc = client.ReadTag(tag, DataTimeout);
if (rc != Libplctag.PLCTAG_STATUS_OK)
{
Console.WriteLine($"ERROR: Unable to read the '{tag.Name}' data! Got error code {rc}: {client.DecodeError(client.GetStatus(tag))}\n");
return;
}
}
/* print out the data */
Console.WriteLine("\n\n{{{{{{{{{{{{{{{{{{{{{\n");
Console.WriteLine($"initial 'tag1' = { client.GetInt16Value(tag1, 0)}\n");
Console.WriteLine($"initial 'tag2' = { client.GetFloat32Value(tag2, 0)}\n");
Console.WriteLine($"initial 'tag3[0]' = { client.GetInt16Value(tag3, 0)}\n");
Console.WriteLine($"initial 'tag3[1]' = { client.GetInt16Value(tag3, 2)}\n");
Console.WriteLine($"initial 'tag4[0]' = { client.GetBitValue(tag4, 0, DataTimeout)}\n");
Console.WriteLine($"initial 'tag4[1]' = { client.GetBitValue(tag4, 1, DataTimeout)}\n");
Console.WriteLine($"initial 'tag4[2]' = { client.GetBitValue(tag4, 2, DataTimeout)}\n");
Console.WriteLine($"initial 'tag4[3]' = { client.GetBitValue(tag4, 3, DataTimeout)}\n");
Console.WriteLine("---------------------\n");
client.Dispose();
}
static void LogError(string error)
{
Console.WriteLine(error);
Console.ReadKey();
}
}
}