| | |
| | | { |
| | | private Plc plc; |
| | | // 默认变量地址 |
| | | public string variable = "DB1.DBD0"; |
| | | public string variable = "DB100.DBX0.1"; |
| | | // 数据类型 |
| | | private string dataType = "String"; |
| | | |
| | | private string dataType = "Bool"; |
| | | |
| | | // 缓存连接参数 |
| | | private string ip = "127.0.0.1"; |
| | | private short rack = 0; |
| | |
| | | { |
| | | CommunicatorName = name; |
| | | CommunicatorBrand = CommunicatorBrand.SiemensS7; |
| | | |
| | | |
| | | // 初始化默认参数 |
| | | if (!CommunicatorConnections.Contains("地址")) CommunicatorConnections.Add("地址", "192.168.0.1"); |
| | | if (!CommunicatorConnections.Contains("机架号")) CommunicatorConnections.Add("机架号", "0"); |
| | |
| | | if (!CommunicatorConnections.Contains("型号")) CommunicatorConnections.Add("型号", CpuType.S71500); |
| | | if (!CommunicatorConnections.Contains("变量地址")) CommunicatorConnections.Add("变量地址", "DB1.DBD0"); |
| | | if (!CommunicatorConnections.Contains("数据类型")) CommunicatorConnections.Add("数据类型", "String"); |
| | | |
| | | |
| | | // 兼容旧配置 "端口" |
| | | if (CommunicatorConnections.Contains("端口")) |
| | | { |
| | |
| | | } |
| | | |
| | | // 设置默认心跳消息 |
| | | strHeartbeat = "HEARTBEAT"; |
| | | strHeartbeat = "HEARTBEAT"; |
| | | } |
| | | |
| | | public override bool Connect() |
| | |
| | | { |
| | | // 更新参数 |
| | | if (CommunicatorConnections.Contains("地址")) ip = CommunicatorConnections["地址"].ToString(); |
| | | |
| | | if (CommunicatorConnections.Contains("机架号")) |
| | | |
| | | if (CommunicatorConnections.Contains("机架号")) |
| | | short.TryParse(CommunicatorConnections["机架号"].ToString(), out rack); |
| | | |
| | | if (CommunicatorConnections.Contains("插槽号")) |
| | | |
| | | if (CommunicatorConnections.Contains("插槽号")) |
| | | short.TryParse(CommunicatorConnections["插槽号"].ToString(), out slot); |
| | | else if (CommunicatorConnections.Contains("端口")) |
| | | short.TryParse(CommunicatorConnections["端口"].ToString(), out slot); |
| | |
| | | else |
| | | Enum.TryParse(CommunicatorConnections["型号"].ToString(), out cpuType); |
| | | } |
| | | |
| | | |
| | | if (CommunicatorConnections.Contains("变量地址")) |
| | | { |
| | | variable = CommunicatorConnections["变量地址"].ToString(); |
| | |
| | | |
| | | // 关闭旧连接 |
| | | plc?.Close(); |
| | | |
| | | |
| | | plc = new Plc(cpuType, ip, rack, slot); |
| | | plc.Open(); |
| | | |
| | | |
| | | if (plc.IsConnected) |
| | | { |
| | | bConnected = true; |
| | |
| | | |
| | | try |
| | | { |
| | | string targetVar = variable; |
| | | variable = CommunicatorConnections.Contains("变量地址") ? CommunicatorConnections["变量地址"].ToString() : "DB100.DBX0.1"; |
| | | string targetVar = variable;//"DB100.DBX0.1"; |
| | | string strValue = message; |
| | | |
| | | // 简单的协议解析:地址:值 |
| | |
| | | string currentDataType = CommunicatorConnections.Contains("数据类型") ? CommunicatorConnections["数据类型"].ToString() : "String"; |
| | | |
| | | // 根据配置的数据类型进行转换 |
| | | try |
| | | try |
| | | { |
| | | switch (currentDataType) |
| | | { |
| | |
| | | AsyncLogHelper.Error($"Device:[{CommunicatorName}] {Msg}"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | public object ReadSignal(SignalConfig signal) |
| | | { |
| | | if (plc == null || !plc.IsConnected) |
| | | { |
| | | AsyncLogHelper.Error("PLC 未连接"); |
| | | } |
| | | |
| | | string address = BuildAddress(signal); |
| | | if (signal.DataType.Equals("Real")) |
| | | { |
| | | object result = plc.Read(address); |
| | | byte[] bytes = BitConverter.GetBytes(Convert.ToUInt32(result)); |
| | | float f = BitConverter.ToSingle(bytes, 0); |
| | | strReceiveMsg = f.ToString(); |
| | | } |
| | | else if (signal.DataType.Equals("Bool")) |
| | | { |
| | | object result = plc.Read(address); |
| | | strReceiveMsg = result.ToString(); |
| | | } |
| | | else if (signal.DataType.Equals("Int")) |
| | | { |
| | | object result = ((uint)plc.Read(address)).ConvertToInt();// ((uint)plc.Read("DB1.DBD60")).ConvertToInt(); |
| | | strReceiveMsg = result.ToString(); |
| | | } |
| | | return strReceiveMsg; |
| | | } |
| | | |
| | | public void WriteSignal(SignalConfig signal, object value) |
| | | { |
| | | if (plc == null || !plc.IsConnected) |
| | | { |
| | | AsyncLogHelper.Error("PLC 未连接"); |
| | | } |
| | | |
| | | string address = BuildAddress(signal); |
| | | plc.Write(address, value); |
| | | } |
| | | |
| | | public string BuildAddress(SignalConfig signal) |
| | | { |
| | | switch (signal.Area.ToUpper()) |
| | | { |
| | | case "DB": |
| | | { |
| | | if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | return $"DB{signal.DbNumber}.DBX{signal.ByteOffset}.{signal.BitOffset}"; |
| | | } |
| | | else |
| | | { |
| | | return $"DB{signal.DbNumber}.DBD{signal.ByteOffset}"; |
| | | } |
| | | } |
| | | case "I": |
| | | { |
| | | if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | return $"I{signal.ByteOffset}.{signal.BitOffset}"; |
| | | } |
| | | else |
| | | { |
| | | return $"IB{signal.ByteOffset}"; |
| | | } |
| | | } |
| | | case "Q": |
| | | { |
| | | if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | return $"Q{signal.ByteOffset}.{signal.BitOffset}"; |
| | | } |
| | | else |
| | | { |
| | | return $"QB{signal.ByteOffset}"; |
| | | } |
| | | } |
| | | case "M": |
| | | { |
| | | if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | return $"M{signal.ByteOffset}.{signal.BitOffset}"; |
| | | } |
| | | else |
| | | { |
| | | return $"MB{signal.ByteOffset}"; |
| | | } |
| | | } |
| | | default: |
| | | { |
| | | AsyncLogHelper.Error($"不支持的存储区: {signal.Area}"); |
| | | return $"不支持的存储区: {signal.Area}"; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 原始地址读写(用于调试器) |
| | | public object ReadRaw(string address) |
| | | { |
| | | if (plc == null || !plc.IsConnected) |
| | | { |
| | | AsyncLogHelper.Error("PLC 未连接"); |
| | | } |
| | | return plc.Read(address); |
| | | } |
| | | |
| | | public void WriteRaw(string address, object value) |
| | | { |
| | | if (plc == null || !plc.IsConnected) |
| | | { |
| | | AsyncLogHelper.Error("PLC 未连接"); |
| | | } |
| | | plc.Write(address, value); |
| | | } |
| | | |
| | | public override string ReceiveMsg() |
| | |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | public object Read(string address) |
| | | { |
| | | if (plc == null || !plc.IsConnected) |
| | | { |
| | | return null; |
| | | } |
| | | return plc.Read(address); |
| | | return plc.Read(address); |
| | | } |
| | | |
| | | |
| | | public void Write(string address, object value) |
| | | { |
| | | if (plc != null && plc.IsConnected) |