Skip to content

Commit

Permalink
New implementation 2.0.10 libplctag
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Jan 22, 2019
1 parent 6e6d80a commit 404f27c
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 222 deletions.
27 changes: 23 additions & 4 deletions src/Corsinvest.AllenBradley.PLC.Api.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class Test12

public class Program
{


private static void PrintChange(string @event, ResultOperation result)
{
Console.Out.WriteLine($"{@event} {result.Timestamp} Changed: {result.Tag.Name} {result.StatusCode}");
Expand All @@ -39,11 +37,30 @@ public static void Main(string[] args)
{
using (var controller = new Controller("10.155.128.192", "1, 0", CPUType.LGX))
{
controller.Timeout = 1000;
// controller.DebugLevel=3;
Console.Out.WriteLine("Ping " + controller.Ping(true));
var grp = controller.CreateGroup();

var tagBPLC1 = grp.CreateTagInt32("TKP_PLC_B_P1");
tagBPLC1.Read();
var tag12 = grp.CreateTagInt32("TKP_PLC_D_P1[10]");

var tagBPLC1 = grp.CreateTagInt32("TKP_PLC_B_P1");
tagBPLC1.Read();

var tagOvenEnabled = grp.CreateTagInt32("TKP_PLC_B_OVEN");
var oven = tagOvenEnabled.Read();
Console.Out.WriteLine(oven.Tag.Value);

System.Threading.Thread.Sleep(800);

Console.Out.WriteLine("pippo");

oven = tagOvenEnabled.Read();
Console.Out.WriteLine(oven.Tag.Value);
Console.Out.WriteLine(oven.Tag.ValueManager.GetBits()[0]);
Console.Out.WriteLine(oven.Tag.ValueManager.GetBitsArray()[0]);
Console.Out.WriteLine(oven.Tag.ValueManager.GetBitsString());


// var tagBPC1 = grp.CreateTagInt32("TKP_PC_B_P1");
// var tagBarcode = grp.CreateTagString("TKP_PLC_S_P1");
Expand All @@ -57,6 +74,8 @@ public static void Main(string[] args)
tag.Changed += TagChanged;
var aa = tag.Read();

Console.Out.WriteLine(aa);

var tag1 = grp.CreateTagType<Test12>("Test");
tag.Changed += TagChanged;

Expand Down
30 changes: 29 additions & 1 deletion src/Corsinvest.AllenBradley.PLC.Api/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,32 @@ public Controller(string ipAddress, string path, CPUType cpuType)
public bool FailOperationRaiseException { get; set; } = false;

/// <summary>
/// Communication timeout
/// Automatic Write when using value.
/// </summary>
/// <value></value>
public bool AutoReadValue { get; set; } = false;

/// <summary>
/// Automatic Write when using value.
/// </summary>
/// <value></value>
public bool AutoWriteValue { get; set; } = false;

/// <summary>
/// Communication timeout millisec.
/// </summary>
/// <value></value>
public int Timeout { get; set; } = 5000;

/// <summary>
/// Optional allows the selection of varying levels of debugging output.
/// 1 shows only the more urgent problems.
/// 5 shows almost every action within the library and will generate a very large amount of output.
/// Generally 3 or 4 is most useful when debugging.
/// </summary>
/// <value></value>
public int DebugLevel { get; set; } = 0;

/// <summary>
/// Groups
/// </summary>
Expand All @@ -59,6 +80,13 @@ public Controller(string ipAddress, string path, CPUType cpuType)
/// <returns></returns>
public IReadOnlyList<ITag> Tags { get { return _tagGroups.SelectMany(a => a.Tags).Distinct().ToList().AsReadOnly(); } }

/// <summary>
/// Verify if exists tag with name and return.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ITag TagExists(string name) { return Tags.Where(a => a.Name == name).FirstOrDefault(); }

/// <summary>
/// IP address of the gateway for this protocol. Could be the IP address of the PLC you want to access.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.7</Version>
<Version>0.1.0</Version>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface ITag : IDisposable
/// Handle creation Tag
/// </summary>
/// <value></value>
IntPtr Handle { get; }
int Handle { get; }

/// <summary>
/// Controller reference.
Expand All @@ -48,6 +48,12 @@ public interface ITag : IDisposable
/// </summary>
int Length { get; }

/// <summary>
/// Type value.
/// </summary>
/// <value></value>
Type TypeValue { get; }

/// <summary>
/// Old value tag.
/// </summary>
Expand All @@ -66,6 +72,12 @@ public interface ITag : IDisposable
/// <value></value>
bool IsChangedValue { get; }

/// <summary>
/// Indicate if Tag is in read only.async Write raise exception.
/// </summary>
/// <value></value>
bool ReadOnly { get; set; }

/// <summary>
/// Value manager
/// </summary>
Expand Down Expand Up @@ -95,6 +107,12 @@ public interface ITag : IDisposable
/// </summary>
bool IsWrite { get; }

/// <summary>
/// Abort any outstanding IO to the PLC. <see cref="StatusCodeOperation"/>
/// </summary>
/// <returns></returns>
int Abort();

/// <summary>
/// Get size tag.
/// </summary>
Expand Down
65 changes: 43 additions & 22 deletions src/Corsinvest.AllenBradley.PLC.Api/NativeMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,93 @@ internal static class NativeMethod
private const string DLL_NAME = "plctag";

[DllImport(DLL_NAME, EntryPoint = "plc_tag_create", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr plc_tag_create([MarshalAs(UnmanagedType.LPStr)] string lpString);
internal static extern int plc_tag_create([MarshalAs(UnmanagedType.LPStr)] string lpString, int timeout);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_destroy", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_destroy(IntPtr tag);
internal static extern int plc_tag_destroy(int tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_abort", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_abort(int tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_status", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_status(IntPtr tag);
internal static extern int plc_tag_status(int tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_size", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_get_size(IntPtr tag);
internal static extern int plc_tag_get_size(int tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_decode_error", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr plc_tag_decode_error(int error);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_read", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_read(IntPtr tag, int timeout);
internal static extern int plc_tag_read(int tag, int timeout);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_write", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_write(IntPtr tag, int timeout);
internal static extern int plc_tag_write(int tag, int timeout);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_uint16", CallingConvention = CallingConvention.Cdecl)]
internal static extern ushort plc_tag_get_uint16(IntPtr tag, int offset);
internal static extern ushort plc_tag_get_uint16(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_int16", CallingConvention = CallingConvention.Cdecl)]
internal static extern short plc_tag_get_int16(IntPtr tag, int offset);
internal static extern short plc_tag_get_int16(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_uint16", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_uint16(IntPtr tag, int offset, ushort val);
internal static extern int plc_tag_set_uint16(int tag, int offset, ushort val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int16", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_int16(IntPtr tag, int offset, short val);
internal static extern int plc_tag_set_int16(int tag, int offset, short val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_uint8", CallingConvention = CallingConvention.Cdecl)]
internal static extern byte plc_tag_get_uint8(IntPtr tag, int offset);
internal static extern byte plc_tag_get_uint8(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_int8", CallingConvention = CallingConvention.Cdecl)]
internal static extern sbyte plc_tag_get_int8(IntPtr tag, int offset);
internal static extern sbyte plc_tag_get_int8(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_uint8", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_uint8(IntPtr tag, int offset, byte val);
internal static extern int plc_tag_set_uint8(int tag, int offset, byte val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int8", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_int8(IntPtr tag, int offset, sbyte val);
internal static extern int plc_tag_set_int8(int tag, int offset, sbyte val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_float32", CallingConvention = CallingConvention.Cdecl)]
internal static extern float plc_tag_get_float32(IntPtr tag, int offset);
internal static extern float plc_tag_get_float32(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_float32", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_float32(IntPtr tag, int offset, float val);
internal static extern int plc_tag_set_float32(int tag, int offset, float val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_float64", CallingConvention = CallingConvention.Cdecl)]
internal static extern double plc_tag_get_float64(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_float64", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_float64(int tag, int offset, double val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_uint32", CallingConvention = CallingConvention.Cdecl)]
internal static extern uint plc_tag_get_uint32(IntPtr tag, int offset);
internal static extern uint plc_tag_get_uint32(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_int32", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_get_int32(IntPtr tag, int offset);
internal static extern int plc_tag_get_int32(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_uint32", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_uint32(IntPtr tag, int offset, uint val);
internal static extern int plc_tag_set_uint32(int tag, int offset, uint val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int32", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_int32(IntPtr tag, int offset, int val);
internal static extern int plc_tag_set_int32(int tag, int offset, int val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_uint64", CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong plc_tag_get_uint64(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_int64", CallingConvention = CallingConvention.Cdecl)]
internal static extern long plc_tag_get_int64(int tag, int offset);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_uint64", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_uint64(int tag, int offset, ulong val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int64", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_set_int64(int tag, int offset, long val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_lock", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_lock(IntPtr tag);
internal static extern int plc_tag_lock(int tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_unlock", CallingConvention = CallingConvention.Cdecl)]
internal static extern int plc_tag_unlock(IntPtr tag);
internal static extern int plc_tag_unlock(int tag);
}
}
13 changes: 13 additions & 0 deletions src/Corsinvest.AllenBradley.PLC.Api/ResultOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,18 @@ public static ResultOperation Reduce(IEnumerable<ResultOperation> results)
results.Sum(a => a.ExecutionTime),
results.Sum(a => a.StatusCode) != 0 ? results.Max(a => a.StatusCode) : 0);
}

/// <summary>
/// Information result.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $@"Tag Name: {Tag.Name}
Tag Value: {Tag.Value}
Timestamp: {Timestamp}
ExecutionTime: {ExecutionTime}
StatusCode: {StatusCode}";
}
}
}
Loading

0 comments on commit 404f27c

Please sign in to comment.