-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added IMarshable interface - implemented custom symbols
- Loading branch information
Showing
9 changed files
with
278 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Reflection; | ||
using SoftBeckhoff.Enums; | ||
|
||
namespace SoftBeckhoff.Extensions | ||
{ | ||
public static class TypeExtensions | ||
{ | ||
public static AdsDatatypeId ToAdsDatatypeId(this Type type) | ||
{ | ||
switch (Type.GetTypeCode(type)) | ||
{ | ||
case TypeCode.Int16: | ||
return AdsDatatypeId.ADST_INT16; | ||
case TypeCode.UInt16: | ||
return AdsDatatypeId.ADST_UINT16; | ||
case TypeCode.Int32: | ||
return AdsDatatypeId.ADST_INT32; | ||
case TypeCode.UInt32: | ||
return AdsDatatypeId.ADST_UINT32; | ||
case TypeCode.Int64: | ||
return AdsDatatypeId.ADST_INT64; | ||
case TypeCode.UInt64: | ||
return AdsDatatypeId.ADST_UINT64; | ||
case TypeCode.Single: | ||
return AdsDatatypeId.ADST_REAL32; | ||
case TypeCode.Double: | ||
return AdsDatatypeId.ADST_REAL64; | ||
case TypeCode.String: | ||
return AdsDatatypeId.ADST_STRING; | ||
case TypeCode.Boolean: | ||
return AdsDatatypeId.ADST_UINT8; | ||
case TypeCode.Byte: | ||
return AdsDatatypeId.ADST_UINT8; | ||
default: | ||
throw new ArgumentOutOfRangeException($"Type {type} not supported"); | ||
} | ||
|
||
} | ||
|
||
public static string ToAdsDatatypeName(this Type type) | ||
{ | ||
switch (Type.GetTypeCode(type)) | ||
{ | ||
case TypeCode.Int16: | ||
return "INT"; | ||
case TypeCode.UInt16: | ||
return "UINT"; | ||
case TypeCode.Int32: | ||
return "DINT"; | ||
case TypeCode.UInt32: | ||
return "UDINT"; | ||
case TypeCode.Int64: | ||
return "LINT"; | ||
case TypeCode.UInt64: | ||
return "ULINT"; | ||
case TypeCode.Single: | ||
return "REAL"; | ||
case TypeCode.Double: | ||
return "LREAL"; | ||
case TypeCode.String: | ||
return "STRING(80)"; | ||
case TypeCode.Boolean: | ||
return "BOOL"; | ||
case TypeCode.Byte: | ||
return "BYTE"; | ||
default: | ||
throw new ArgumentOutOfRangeException($"Type {type} not supported"); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SoftBeckhoff.Interfaces | ||
{ | ||
public interface IMarshable | ||
{ | ||
public byte[] GetBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using SoftBeckhoff.Enums; | ||
using SoftBeckhoff.Extensions; | ||
using SoftBeckhoff.Interfaces; | ||
using SoftBeckhoff.Structs; | ||
|
||
namespace SoftBeckhoff.Models | ||
{ | ||
public class AdsSymbol : IMarshable | ||
{ | ||
private readonly string name; | ||
private AdsSymbolEntryHeader header; | ||
private string adsType; | ||
|
||
public AdsSymbol(string name, Type type) | ||
{ | ||
this.name = name; | ||
header = new AdsSymbolEntryHeader(type); | ||
adsType = type.ToAdsDatatypeName(); | ||
} | ||
|
||
public string Name => name; | ||
public int Offset { get; set; } | ||
public int Size => (int) header.Size; | ||
|
||
public byte[] GetBytes() | ||
{ | ||
var byteName = Encoding.ASCII.GetBytes(name); | ||
var byteType = Encoding.ASCII.GetBytes(adsType); | ||
var headerSize = header.GetSize(); | ||
var wholeSize = headerSize + byteName.Length + byteType.Length + 3; | ||
header.NameLength = (ushort) byteName.Length; | ||
header.EntryLength = (uint) wholeSize; | ||
header.IndexOffset = (uint) Offset; | ||
var buffer = new List<byte>(); | ||
buffer.AddRange(header.GetBytes()); | ||
buffer.AddRange(byteName); | ||
buffer.Add(0); | ||
buffer.AddRange(byteType); | ||
buffer.Add(0); | ||
buffer.Add(0); //comment | ||
|
||
return buffer.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using SoftBeckhoff.Enums; | ||
using SoftBeckhoff.Extensions; | ||
|
||
namespace SoftBeckhoff.Structs | ||
{ | ||
[StructLayout(LayoutKind.Sequential, Pack = 1)]public struct AdsSymbolEntryHeader | ||
{ | ||
[MarshalAs(UnmanagedType.U4)]public uint EntryLength; | ||
[MarshalAs(UnmanagedType.U4)]public uint IndexGroup; | ||
[MarshalAs(UnmanagedType.U4)]public uint IndexOffset; | ||
[MarshalAs(UnmanagedType.U4)]public uint Size; | ||
[MarshalAs(UnmanagedType.U4)]public AdsDatatypeId DataType; | ||
[MarshalAs(UnmanagedType.U2)]public AdsSymbolFlags Flags; | ||
[MarshalAs(UnmanagedType.U2)]public ushort ArrayDim; | ||
[MarshalAs(UnmanagedType.U2)]public ushort NameLength; | ||
[MarshalAs(UnmanagedType.U2)]public ushort TypeLength; | ||
[MarshalAs(UnmanagedType.U2)]public ushort CommentLength; | ||
|
||
public AdsSymbolEntryHeader(Type type) | ||
{ | ||
EntryLength = 0; | ||
IndexGroup = 61445; | ||
IndexOffset = 0; //to supply | ||
Size = type == typeof(string) ? 81 : (uint) Marshal.SizeOf(type); | ||
DataType = type.ToAdsDatatypeId(); | ||
Flags = AdsSymbolFlags.None; | ||
ArrayDim = 0; | ||
NameLength = 0; //to supply | ||
TypeLength = (ushort) type.ToAdsDatatypeName().Length; | ||
CommentLength = 0; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace SoftBeckhoff.Structs | ||
{ | ||
[StructLayout(LayoutKind.Sequential, Pack = 1)]public struct ResponseHeader | ||
{ | ||
[MarshalAs(UnmanagedType.U4)]public uint Result; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{nameof(Result)}:{Result}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace SoftBeckhoff.Structs | ||
{ | ||
[StructLayout(LayoutKind.Sequential, Pack = 1)]public struct WriteRequestData | ||
{ | ||
[MarshalAs(UnmanagedType.U4)]public uint IndexGroup; | ||
[MarshalAs(UnmanagedType.U4)]public uint Offset; | ||
[MarshalAs(UnmanagedType.U4)]public uint Length; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{nameof(IndexGroup)}:{IndexGroup}, {nameof(Offset)}:{Offset}, {nameof(Length)}:{Length}"; | ||
} | ||
} | ||
} |