diff --git a/Modbus.Net/ModBus.Net/BaseMachine.cs b/Modbus.Net/ModBus.Net/BaseMachine.cs index 8a9b56c..1b3941a 100644 --- a/Modbus.Net/ModBus.Net/BaseMachine.cs +++ b/Modbus.Net/ModBus.Net/BaseMachine.cs @@ -5,6 +5,12 @@ using System.Threading.Tasks; namespace ModBus.Net { + public enum MachineSetDataType + { + Address, + CommunicationTag + } + public abstract class BaseMachine : IMachineProperty { public int Id { get; set; } @@ -30,6 +36,12 @@ namespace ModBus.Net public AddressCombiner AddressCombiner { get; set; } + public AddressTranslator AddressTranslator + { + get { return BaseUtility.AddressTranslator; } + set { BaseUtility.AddressTranslator = value; } + } + public MachineExtend MachineExtend { get; set; } protected IEnumerable CommunicateAddresses @@ -118,6 +130,93 @@ namespace ModBus.Net } } + public bool SetDatas(MachineSetDataType setDataType, Dictionary values) + { + return AsyncHelper.RunSync(() => SetDatasAsync(setDataType, values)); + } + + public async Task SetDatasAsync(MachineSetDataType setDataType, Dictionary values) + { + try + { + if (!BaseUtility.IsConnected) + { + await BaseUtility.ConnectAsync(); + } + if (!BaseUtility.IsConnected) return false; + List addresses = new List(); + foreach (var value in values) + { + AddressUnit address = null; + switch (setDataType) + { + case MachineSetDataType.Address: + { + address = + GetAddresses.SingleOrDefault(p => AddressFormater.FormatAddress(p.Area, p.Address) == value.Key); + break; + } + case MachineSetDataType.CommunicationTag: + { + address = + GetAddresses.SingleOrDefault(p => p.CommunicationTag == value.Key); + break; + } + } + if (address == null) return false; + addresses.Add(address); + } + var communcationUnits = AddressCombiner.Combine(addresses); + foreach (var communicateAddress in communcationUnits) + { + List datasList = new List(); + var setCount = (int) + Math.Ceiling(communicateAddress.GetCount* + BigEndianValueHelper.Instance.ByteLength[ + communicateAddress.DataType.FullName]); + var allBytes = setCount; + + while (setCount > 0) + { + var address = AddressFormater.FormatAddress(communicateAddress.Area, + communicateAddress.Address + allBytes - setCount); + var addressUnit = + GetAddresses.SingleOrDefault( + p => + p.Area == communicateAddress.Area && + p.Address == communicateAddress.Address + allBytes - setCount); + Type dataType = addressUnit.DataType; + switch (setDataType) + { + case MachineSetDataType.Address: + { + var value = values.SingleOrDefault(p => p.Key == address); + await + BaseUtility.SetDatasAsync(2, 0, address, + new object[] {Convert.ChangeType(value.Value, dataType)}); + break; + } + case MachineSetDataType.CommunicationTag: + { + var value = values.SingleOrDefault(p => p.Key == addressUnit.CommunicationTag); + await + BaseUtility.SetDatasAsync(2, 0, address, + new object[] {Convert.ChangeType(value.Value, dataType)}); + break; + } + } + setCount -= (int) BigEndianValueHelper.Instance.ByteLength[dataType.FullName]; + } + } + } + catch (Exception e) + { + Console.WriteLine(ConnectionToken + " " + e.Message); + return false; + } + return true; + } + public bool Connect() { return BaseUtility.Connect(); @@ -132,6 +231,12 @@ namespace ModBus.Net { return BaseUtility.Disconnect(); } + + public static Dictionary MapGetValuesToSetValues(Dictionary getValues) + { + return (from getValue in getValues + select new KeyValuePair(getValue.Key, getValue.Value.PlcValue)).ToDictionary(p=>p.Key,p=>p.Value); + } } public class BaseMachineEqualityComparer : IEqualityComparer diff --git a/Modbus.Net/ModBus.Net/TaskManager.cs b/Modbus.Net/ModBus.Net/TaskManager.cs index 5583442..ccf84fa 100644 --- a/Modbus.Net/ModBus.Net/TaskManager.cs +++ b/Modbus.Net/ModBus.Net/TaskManager.cs @@ -444,6 +444,18 @@ namespace ModBus.Net } } + public async Task SetDatasAsync(string machineToken, MachineSetDataType setDataType, + Dictionary values) + { + BaseMachine machine = null; + lock (_machines) + { + machine = _machines.FirstOrDefault(p => p.ConnectionToken == machineToken); + } + if (machine == null) return false; + return await machine.SetDatasAsync(setDataType, values); + } + public void TaskStart() { TaskStop(); diff --git a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs index 0d9ef74..a6f1a51 100644 --- a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs +++ b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using ModBus.Net; using System.Windows; +using ModBus.Net.Modbus; using ModBus.Net.Siemens; @@ -14,6 +15,7 @@ namespace NA200H.UI.WPF public partial class MainWindow : Window { private BaseUtility utility; + private BaseMachine machine; public MainWindow() { InitializeComponent(); @@ -21,16 +23,44 @@ namespace NA200H.UI.WPF private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { - //utility = new ModbusUtility(ModbusType.Tcp, "192.168.3.247"); + //GetUtilityEnter(); + GetMachineEnter(); + } + + private void GetUtilityEnter() + { + //utility = new ModbusUtility(ModbusType.Tcp, "192.168.3.12"); //utility.AddressTranslator = new AddressTranslatorNA200H(); //object[] getNum = utility.GetDatas(0x02, 0x00, "NW 1", new KeyValuePair(typeof(ushort), 4)); - utility = new SiemensUtility(SiemensType.Tcp, "192.168.3.191", SiemensMachineModel.S7_200); + utility = new SiemensUtility(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300); utility.AddressTranslator = new AddressTranslatorSiemens(); object[] getNum = utility.GetDatas(0x02, 0x00, "V 1", new KeyValuePair(typeof(ushort), 4)); ushort[] getNumUshorts = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray(getNum); SetValue(getNumUshorts); } + private void GetMachineEnter() + { + //machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", new List() + //{ + //new AddressUnit() {Id = 1, Area = "NW", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = 2, Area = "NW", Address = 3, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = 3, Area = "NW", Address = 5, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = 4, Area = "NW", Address = 7, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} + //}); + //machine.AddressTranslator = new AddressTranslatorNA200H(); + machine = new SiemensMachine(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300, new List() + { + new AddressUnit() {Id = 1, Area = "V", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 2, Area = "V", Address = 3, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 3, Area = "V", Address = 5, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = 4, Area = "V", Address = 7, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} + }); + var result = machine.GetDatas(); + var resultFormat = BaseMachine.MapGetValuesToSetValues(result); + SetValue(new ushort[4] {(ushort)resultFormat["Add1"], (ushort)resultFormat["Add2"], (ushort)resultFormat["Add3"], (ushort)resultFormat["Ans"]}); + } + private void SetValue(ushort[] getNum) { Add1.Text = getNum[0].ToString(); @@ -40,6 +70,12 @@ namespace NA200H.UI.WPF } private void Calc_OnClick(object sender, RoutedEventArgs e) + { + //SetUtilityEnter(); + SetMachineEnter(); + } + + private void SetUtilityEnter() { ushort add1 = 0, add2 = 0, add3 = 0; ushort.TryParse(Add1.Text, out add1); @@ -49,9 +85,18 @@ namespace NA200H.UI.WPF utility.SetDatas(0x02, 0x00, "V 1", new object[] { add1, add2, add3 }); Thread.Sleep(100); //object[] getNum = utility.GetDatas(0x02, 0x00, "NW 1", new KeyValuePair(typeof(ushort), 4)); - object[] getNum = utility.GetDatas(0x02, 0x00, "V 1", new KeyValuePair(typeof(ushort), 4)); - ushort[] getNumUshorts = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray(getNum); - SetValue(getNumUshorts); + GetUtilityEnter(); + } + + private void SetMachineEnter() + { + ushort add1 = 0, add2 = 0, add3 = 0; + ushort.TryParse(Add1.Text, out add1); + ushort.TryParse(Add2.Text, out add2); + ushort.TryParse(Add3.Text, out add3); + var setDic = new Dictionary{{"V 1", add1}, {"V 3", add2}, {"V 5", add3}}; + machine.SetDatas(MachineSetDataType.Address, setDic); + GetMachineEnter(); } } }