From b5058ff140672e723f6298406010b6e0f2356530 Mon Sep 17 00:00:00 2001 From: Simone Vuotto Date: Wed, 9 Aug 2023 14:52:20 +0200 Subject: [PATCH] Fix UInt16 writing + expose length value --- McProtocol/MCProtocol.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/McProtocol/MCProtocol.cs b/McProtocol/MCProtocol.cs index 8ca7fa0..7754f5c 100644 --- a/McProtocol/MCProtocol.cs +++ b/McProtocol/MCProtocol.cs @@ -26,8 +26,8 @@ public class PLCData : PLCData { Mitsubishi.PlcDeviceType DeviceType; int Address; - int Length; - int LENGTH;//Length in bytes + public int Length { get; private set; } + public int BytesLength { get; private set; } //Length in bytes byte[] bytes; public PLCData(Mitsubishi.PlcDeviceType DeviceType, int Address, int Length) { @@ -38,41 +38,41 @@ public PLCData(Mitsubishi.PlcDeviceType DeviceType, int Address, int Length) switch (t) { case "Boolean": - this.LENGTH = (Length / 16 + (Length % 16 > 0 ? 1 : 0)) * 2; + this.BytesLength = (Length / 16 + (Length % 16 > 0 ? 1 : 0)) * 2; this.Length = Length; break; case "Int32": - this.LENGTH = 4 * Length; + this.BytesLength = 4 * Length; this.Length = Length * 2; break; case "Int16": - this.LENGTH = 2 * Length; + this.BytesLength = 2 * Length; this.Length = Length; break; case "UInt16": - this.LENGTH = 2 * Length; + this.BytesLength = 2 * Length; this.Length = Length; break; case "UInt32": - this.LENGTH = 4 * Length; + this.BytesLength = 4 * Length; this.Length = Length * 2; break; case "Single": - this.LENGTH = 4 * Length; + this.BytesLength = 4 * Length; this.Length = Length * 2; break; case "Double": - this.LENGTH = 8 * Length; + this.BytesLength = 8 * Length; this.Length = Length * 4; break; case "Char": - this.LENGTH = Length; + this.BytesLength = Length; this.Length = Length; break; default: throw new Exception("Type not supported by PLC."); } - this.bytes = new byte[this.LENGTH]; + this.bytes = new byte[this.BytesLength]; } public T this[int i] @@ -152,7 +152,7 @@ public T this[int i] case "UInt16": u.UINT = Convert.ToUInt16(value); this.bytes[i * 2] = u.a; - this.bytes[i * 2] = u.b; + this.bytes[i * 2 + 1] = u.b; return; case "Single": u.REAL = Convert.ToSingle(value);