| | |
| | | } |
| | | } |
| | | |
| | | 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() |
| | | { |
| | | if (plc == null || !plc.IsConnected) return string.Empty; |