PLCHandler.dll C# Wrapper

ebert

Level-1
Beiträge
2
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hi,

ich arbeite mit einem Wago 750-880/881 und einen ARM Microcontroller auf dem Windows CE 6.0 läuft als HMI.
Zum programmieren des Benutzerinterface nutze ich C# .net Compact Framework.
Um mir den ganzen Zugriff auf die Steuerung zu vereinfachen würde ich gerne die Codesys PLCHandler.dll einsetzen. Diese ist allerdings in C++ geschrieben, daher muss ich mir Wrapper schreiben, worauf ich natürlich nicht so große Lust habe
icon_wink.gif
. Hat sich hier vielleicht schonmal einer die Mühe gemacht und einen C# Wrapper für den PLCHandler geschrieben
icon_biggrin.gif
?

Gruß und Danke,
Michael
 
public class CodesysV3
{
private const string NATIVEDLL = "PLCHandler";

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PLCHandlerInitByFile([In]uint a1, [In]string a2, [In]string a3);//a1 = 0,a2 = ./driver/PLC%x.ini,a3 = ./driver/PLCHandlerDemoC.log

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerConnect([In,Out]UIntPtr a1, [In]uint a2, [In]UIntPtr a3, [In]int a4);//a2 = 10000,a3 = 0,a4 = 1

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerGetState([In]UIntPtr a1);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerDisconnect([In]UIntPtr a1);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerSetFinalShutDown([In]UIntPtr a1);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerSyncWriteVarsToPlc([In]UIntPtr a1, [In]string[] a2, [In]uint a3, [In]IntPtr a4);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerSyncWriteVarsToPlc2([In]UIntPtr a1, [In]string[] a2, [In]uint a3, [In]IntPtr a4, [In] uint[] a5);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PLCHandlerSyncReadVarsFromPlc([In]UIntPtr a1, [In]string[] a2, [In]uint a3, [Out] out IntPtr a4, [Out] out uint a5);

[DllImport(NATIVEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int PLCHandlerSyncReadVarsFromPlcReleaseValues([In]UIntPtr a1,[In] UIntPtr a2);

}

class Program
{
static void Main(string[] args)
{
int res = 0;
UIntPtr ulPLCHandler = CodesysV3.PLCHandlerInitByFile(0, "PLC7f000001.ini", "PLCHandlerDemo.log");
if (ulPLCHandler != UIntPtr.Zero)
{
res = CodesysV3.PLCHandlerConnect(ulPLCHandler, 0x2710u, UIntPtr.Zero, 1);
if (res != 0)
{
if (CodesysV3.PLCHandlerGetState(ulPLCHandler) == 2)
Console.WriteLine("**** No symbols on PLC ****");
else
Console.WriteLine("**** No connection to plc ****");
}
else
{
Console.WriteLine("PLC connected successfully");
Console.WriteLine("Press any key Write value to PLC");
Console.ReadKey();

string tagName = "Application.GVL.var3";


uint tagCount = 1;

short value = 3000;
byte[] valueByteArray = BitConverter.GetBytes(value)/*new byte[] { value}*//*new byte[4]*/;
//Buffer.BlockCopy(Encoding.ASCII.GetBytes(value), 0, valueByteArray, 0, 2);
IntPtr valuesPtr = Marshal.AllocHGlobal(8);
IntPtr valuePtr = Marshal.AllocHGlobal(valueByteArray.Length);
Marshal.Copy(valueByteArray, 0, valuePtr, valueByteArray.Length);
Marshal.WriteIntPtr(valuesPtr, valuePtr);

uint a5 = (uint)valueByteArray.Length;

res = CodesysV3.PLCHandlerSyncWriteVarsToPlc2(ulPLCHandler, new string[] { tagName }, tagCount, valuesPtr, new uint[] { a5 });
if (res == 0)
Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")}] Write value {value} to PLC successfully");
else
Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")}] Write value {value} to PLC faild");
Marshal.FreeHGlobal(valuePtr);
Marshal.FreeHGlobal(valuesPtr);
//while (true)
//{
UIntPtr varListHandler = CodesysV3.PLCHandlerSyncReadVarsFromPlc(ulPLCHandler, new string[] { tagName}, tagCount, out IntPtr readValuePtr, out uint ulNumOfValues);
if (varListHandler != UIntPtr.Zero)
{
byte[] byteArray = new byte[100 + a5];
Marshal.Copy(readValuePtr, byteArray, 0, byteArray.Length);
uint ulTimeStamp = BitConverter.ToUInt32(byteArray, 0);
byte bQuality = byteArray[4];
short shortValue = BitConverter.ToInt16(byteArray, 5);
Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")}] ulTimeStamp = {ulTimeStamp} bQuality = {bQuality} shortValue = {shortValue}");
//res = CodesysV3.PLCHandlerSyncReadVarsFromPlcReleaseValues(ulPLCHandler, varListHandler);
Marshal.FreeHGlobal(readValuePtr);
}
//}
}
}
Console.WriteLine("Press any key Disconnect PLC");
Console.ReadKey();
res = CodesysV3.PLCHandlerDisconnect(ulPLCHandler);
res = CodesysV3.PLCHandlerSetFinalShutDown(ulPLCHandler);
Console.WriteLine("Press any key exit app");
Console.ReadKey();
}
}
 
I found this INI file in the installation directory of a third-party software. If you need it, please leave your email address, and I will send it to you.
 
Zurück
Oben