using System; using System.IO; using System.Net.Sockets; using NModbus; using Savronik.Scada.Backbone.Core.Client; using Newtonsoft.Json; using static modbustest.Configuration; using System.Threading.Tasks; using System.Threading; namespace modbustest { class Program { static Manager m; static RootObject rootObject; private static ushort[] previousValues; private static Timer _timer; static void Main(string[] args) { var settingsstring = File.ReadAllText("settings.json"); rootObject = JsonConvert.DeserializeObject(settingsstring); ConnectionInfo ci = new ConnectionInfo { Ip = rootObject.MqttInfo.CiIp, Port = rootObject.MqttInfo.CiPort, Username = rootObject.MqttInfo.CiUserName, Password = rootObject.MqttInfo.CiPassword, Pool = rootObject.MqttInfo.CiPool, WorkerName = rootObject.MqttInfo.CiWorkerName }; m = new Manager(ci); m.ConnectionStatusChanged += M_ConnectionStatusChanged; m.TagChanged += M_TagChanged; m.SubscribeWithQos(rootObject.MqttInfo.MgrTopic, QualityOfServiceLevel.ExactlyOnce); m.Connect(true); previousValues = new ushort[rootObject.Devices.Count]; Array.Fill(previousValues, ushort.MinValue); _timer = new Timer(async _ => await GetData(), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); Console.ReadLine(); /* string ipAddress = "192.168.7.18"; int port = 502; ushort startAddress = 0; ushort numOfPoints = 10; try { using TcpClient client = new TcpClient(ipAddress, port); var factory = new ModbusFactory(); var modbusMaster = factory.CreateMaster(client); ushort[] readData = modbusMaster.ReadHoldingRegisters(1, startAddress, numOfPoints); Console.WriteLine("dataVal:"); for (int i = 0; i < readData.Length; i++) { Console.WriteLine(i + ". => " + readData[i]); } //modbusMaster.WriteSingleRegister(1, 1, 1234); } catch (Exception ex) { Console.WriteLine($"Hata: {ex.Message}"); }*/ } static async Task GetData() { for (int i = 0; i < rootObject.Devices.Count; i++) { try { var device = rootObject.Devices[i]; using TcpClient client = new TcpClient(device.Details.MbusIp, device.Details.MbusPort); var factory = new ModbusFactory(); var modbusMaster = factory.CreateMaster(client); ushort[] readData = modbusMaster.ReadHoldingRegisters((byte)device.Details.MbusSlaveAddress, (ushort)device.Details.MbusStartAddress, (ushort)device.Details.MbusNumOfPoints); if (readData.Length > 0) { ushort currentValue = readData[0]; if (currentValue != previousValues[i]) { //Console.WriteLine($"Reg {device.Details.MbusStartAddress} değeri değişti: Eski: {previousValues[i]}, Yeni: {currentValue} ip adresi:{device.Details.MbusIp}"); previousValues[i] = currentValue; ScadaTag tag = new ScadaTag { Target = device.Details.Target, Timestamp = DateTime.Now, Value = MapValue(readData[0], 0, 32500, 0, 100) }; m.ChangeTag(tag, QualityOfServiceLevel.ExactlyOnce, true); } } } catch (Exception ex) { Console.WriteLine($"Hata: {ex.Message}"); } } } private static void M_TagChanged(object sender, TagChangedEventArgs e) { if (e.Tag.TagInfo.HasFlag(ScadaTag.Info.WaitingCommand)) { e.Tag.TagInfo &= ~ScadaTag.Info.WaitingCommand; e.Tag.Timestamp = DateTime.Now; m.ChangeTag(e.Tag, QualityOfServiceLevel.ExactlyOnce, true); foreach (var device in rootObject.Devices) { if (device.Key.ToString().Equals(e.Tag.Target.ToString())) { OpenSendModbusConnection(device.Details.MbusIp, device.Details.MbusPort, device.Details.MbusStartAddress, device.Details.MbusNumOfPoints, device.Details.MbusSlaveAddress, e.Tag.Value.ToString()); } } } } private static void M_ConnectionStatusChanged(object sender, ConnectionStatusChangedEventArgs e) { } static int MapValue(int value, int fromLow, int fromHigh, int toLow, int toHigh) { return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; } private static bool OpenSendModbusConnection(string pModbusIP, int pModbusPort, int pStartAdres, int pNumberofPoints, int pSlaveAddress, string pValue) { var ret_val = false; try { using TcpClient client = new TcpClient(pModbusIP, pModbusPort); var factory = new ModbusFactory(); var modbusMaster = factory.CreateMaster(client); if (int.TryParse(pValue, out int value)) { var tempValue = MapValue(int.Parse(pValue), 0, 100, 0, 32500); ushort[] registers = new ushort[pNumberofPoints]; for (int k = 0; k < pNumberofPoints; k++) { registers[k] = (ushort)tempValue; } modbusMaster.WriteMultipleRegisters((byte)pSlaveAddress, (ushort)pStartAdres, registers); //modbusMaster.WriteSingleRegister((byte)int.Parse(pSlaveAddress), (ushort)int.Parse(pStartAdres), (ushort)tempValue); ret_val = true; } else ret_val = false; } catch (Exception ex) { Console.WriteLine($"Hata: {ex.Message}"); ret_val = false; } return ret_val; } } }