Skip to content

Commit

Permalink
调整siemens写入驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
xi3892 committed Dec 10, 2023
1 parent d876d0c commit 1b216d6
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions NewLife.Siemens/Drivers/SiemensS7Driver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using NewLife.IoT;
using NewLife.IoT.Drivers;
using NewLife.IoT.ThingModels;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 1b216d6

Please sign in to comment.