From 1b216d6103e52f382de5fbccc9b1a6ee48bd4244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=9E?= Date: Mon, 11 Dec 2023 02:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4siemens=E5=86=99=E5=85=A5?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Siemens/Drivers/SiemensS7Driver.cs | 41 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/NewLife.Siemens/Drivers/SiemensS7Driver.cs b/NewLife.Siemens/Drivers/SiemensS7Driver.cs index cec1c23..52d2283 100644 --- a/NewLife.Siemens/Drivers/SiemensS7Driver.cs +++ b/NewLife.Siemens/Drivers/SiemensS7Driver.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Runtime.CompilerServices; using NewLife.IoT; using NewLife.IoT.Drivers; using NewLife.IoT.ThingModels; @@ -191,8 +192,44 @@ public override Object Write(INode node, IPoint point, Object value) var db = adr.DbNumber; var startByteAdr = adr.StartByte; - if (value is not Byte[] bytes) - throw new ArgumentException("数据value不是字节数组!"); + Byte[] bytes = null; + + var typeStr = point.Type.ToLower(); + + switch (typeStr) + { + 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不是字节数组或有效类型!"); + } _plcConn.WriteBytes(dataType, db, startByteAdr, bytes);