diff --git a/NewLife.Siemens/Drivers/SiemensS7Driver.cs b/NewLife.Siemens/Drivers/SiemensS7Driver.cs index 52d2283..1e8c36a 100644 --- a/NewLife.Siemens/Drivers/SiemensS7Driver.cs +++ b/NewLife.Siemens/Drivers/SiemensS7Driver.cs @@ -3,6 +3,7 @@ using NewLife.IoT; using NewLife.IoT.Drivers; using NewLife.IoT.ThingModels; +using NewLife.Log; using NewLife.Omron.Drivers; using NewLife.Serialization; using NewLife.Siemens.Models; @@ -180,57 +181,49 @@ public virtual String GetAddress(IPoint point) /// 数值 public override Object Write(INode node, IPoint point, Object value) { + using var span = Tracer?.NewSpan("write_value", new { point, value }); + var addr = GetAddress(point); if (addr.IsNullOrWhiteSpace()) return null; + span.AppendTag($"addr:{addr}"); + // 操作字节数组,不用设置bitNumber,但是解析需要带上 if (addr.IndexOf('.') == -1) addr += ".0"; - var adr = new PLCAddress(addr); + var plc_adr = new PLCAddress(addr); - var dataType = adr.DataType; - var db = adr.DbNumber; - var startByteAdr = adr.StartByte; + span.AppendTag($"plc_addr:{plc_adr}"); - Byte[] bytes = null; + var dataType = plc_adr.DataType; + var db = plc_adr.DbNumber; + var startByteAdr = plc_adr.StartByte; - var typeStr = point.Type.ToLower(); + Byte[] bytes = null; - switch (typeStr) + if (value is Byte[] v) + bytes = v; + else { - case "boolean": - case "bool": - bytes = BitConverter.GetBytes(value.ToBoolean()); - break; - case "short": - bytes = BitConverter.GetBytes(Int16.Parse(value + "")); - break; - case "int": - bytes = BitConverter.GetBytes(value.ToInt()); - break; - case "float": - bytes = BitConverter.GetBytes(Single.Parse(value + "")); - break; - case "byte": - bytes = BitConverter.GetBytes(Byte.Parse(value + "")); - break; - case "long": - bytes = BitConverter.GetBytes(Int64.Parse(value + "")); - break; - case "double": - bytes = BitConverter.GetBytes(value.ToDouble()); - break; - case "time": - bytes = BitConverter.GetBytes(value.ToDateTime().Ticks); - break; - case "string": - case "text": - bytes = (value + "").GetBytes(); - break; - default: - throw new ArgumentException("数据value不是字节数组或有效类型!"); + var typeStr = point.Type.ToLower(); + + bytes = typeStr switch + { + "boolean" or "bool" => BitConverter.GetBytes(value.ToBoolean()), + "short" => BitConverter.GetBytes(Int16.Parse(value + "")), + "int" => BitConverter.GetBytes(value.ToInt()), + "float" => BitConverter.GetBytes(Single.Parse(value + "")), + "byte" => BitConverter.GetBytes(Byte.Parse(value + "")), + "long" => BitConverter.GetBytes(Int64.Parse(value + "")), + "double" => BitConverter.GetBytes(value.ToDouble()), + "time" => BitConverter.GetBytes(value.ToDateTime().Ticks), + "string" or "text" => (value + "").GetBytes(), + _ => throw new ArgumentException("数据value不是字节数组或有效类型!"), + }; } + span.AppendTag($"转换完成bytes:{bytes.ToHex()}"); + _plcConn.WriteBytes(dataType, db, startByteAdr, bytes); return null; diff --git a/Test/Program.cs b/Test/Program.cs index 3f55efd..e68066a 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using NewLife; +using NewLife.IoT; using NewLife.IoT.Drivers; using NewLife.IoT.ThingModels; using NewLife.Omron.Drivers; @@ -10,6 +11,17 @@ Console.WriteLine("服务端地址默认为:127.0.0.1:102,保持默认请回车开始连接,否则请输入服务端地址:"); var address = Console.ReadLine(); +var point = new Point +{ + Name = "污泥泵停止时间", + Address = "DB1.DBD60", // "M100", + Type = "long", + Length = 4 //data.Length +}; + +var i = point.GetNetType(); + + if (address == null || address == "") address = "127.0.0.1:102"; var driver = new SiemensS7Driver(); @@ -39,13 +51,7 @@ str = Console.ReadLine(); } -var point = new Point -{ - Name = "test", - Address = "DB1.DBX1134.0", // "M100", - Type = "Int32", - Length = 2 //data.Length -}; + //var point2 = new Point //{