Skip to content

Commit

Permalink
调整写入数据字节数据判断
Browse files Browse the repository at this point in the history
  • Loading branch information
xi3892 committed Dec 18, 2023
1 parent 1b216d6 commit fa96332
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
69 changes: 31 additions & 38 deletions NewLife.Siemens/Drivers/SiemensS7Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,57 +181,49 @@ public virtual String GetAddress(IPoint point)
/// <param name="value">数值</param>
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;
Expand Down
20 changes: 13 additions & 7 deletions Test/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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
//{
Expand Down

0 comments on commit fa96332

Please sign in to comment.