Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.IO;
- namespace AnalizaSieci
- {
- public partial class Form1 : Form
- {
- private const string endPointIpv4Address = "192.168.100.100";
- private const int endPointTCPPort = 502;
- private TcpClient tcpClient;
- private NetworkStream ns;
- public Form1()
- {
- InitializeComponent();
- tcpClient = new TcpClient();
- try
- {
- tcpClient.Connect(IPAddress.Parse(endPointIpv4Address), endPointTCPPort);
- ns = tcpClient.GetStream();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Task loopTask = new Task(() =>
- {
- while (true)
- {
- Task voltageL1LNRefreshTask = new Task(() =>
- {
- this.VoltageL1TextBox.Invoke(new Action(() =>
- {
- this.VoltageL1TextBox.Text = getVoltageL1LN();
- }));
- });
- voltageL1LNRefreshTask.Start();
- Task currentL1RefreshTask = new Task(() =>
- {
- this.currentL1TextBox.Invoke(new Action(() =>
- {
- this.currentL1TextBox.Text = getCurrentL1();
- }));
- });
- currentL1RefreshTask.Start();
- Task cosPhiL1RefreshTask = new Task(() =>
- {
- this.cosPhiL1TextBox.Invoke(new Action(() =>
- {
- this.cosPhiL1TextBox.Text = getCosPhi();
- }));
- });
- cosPhiL1RefreshTask.Start();
- Task activePowerL1RefreshTask = new Task(() =>
- {
- this.activePowerL1TexBox.Invoke(new Action(() =>
- {
- this.activePowerL1TexBox.Text = getActivePowerL1();
- }));
- });
- activePowerL1RefreshTask.Start();
- this.calculatedActivePowerL1TextBox.Invoke(new Action(() =>
- {
- this.calculatedActivePowerL1TextBox.Text = getCalculatedActivePowerL1();
- }));
- }
- });
- loopTask.Start();
- }
- string getVoltageL1LN()
- {
- byte[] request =
- {
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x02, 0x00, 0x02
- };
- ns.Write(request, 0, request.Length);
- byte[] response = new byte[15];
- ns.Read(response, 0, response.Length);
- Console.WriteLine(BitConverter.ToString(response));
- byte[] voltageL1LNHex = new byte[4];
- Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
- Array.Reverse(voltageL1LNHex);
- return (BitConverter.ToUInt32(voltageL1LNHex, 0) / 1000f).ToString();
- }
- string getCurrentL1()
- {
- byte[] request =
- {
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x10, 0x00, 0x02
- };
- ns.Write(request, 0, request.Length);
- byte[] response = new byte[15];
- ns.Read(response, 0, response.Length);
- Console.WriteLine(BitConverter.ToString(response));
- byte[] voltageL1LNHex = new byte[4];
- Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
- Array.Reverse(voltageL1LNHex);
- return (BitConverter.ToUInt32(voltageL1LNHex, 0) / 1000f).ToString();
- }
- string getCosPhi()
- {
- byte[] request =
- {
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x20, 0x00, 0x02
- };
- ns.Write(request, 0, request.Length);
- byte[] response = new byte[15];
- ns.Read(response, 0, response.Length);
- Console.WriteLine(BitConverter.ToString(response));
- byte[] voltageL1LNHex = new byte[4];
- Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
- Array.Reverse(voltageL1LNHex);
- return (BitConverter.ToInt32(voltageL1LNHex, 0) / 1000f).ToString();
- }
- string getActivePowerL1()
- {
- byte[] request =
- {
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x30, 0x00, 0x02
- };
- ns.Write(request, 0, request.Length);
- byte[] response = new byte[15];
- ns.Read(response, 0, response.Length);
- Console.WriteLine(BitConverter.ToString(response));
- byte[] voltageL1LNHex = new byte[4];
- Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
- Array.Reverse(voltageL1LNHex);
- return (BitConverter.ToInt32(voltageL1LNHex, 0)).ToString();
- }
- string getCalculatedActivePowerL1()
- {
- double activePower = Convert.ToDouble(this.VoltageL1TextBox.Text) * Convert.ToDouble(this.currentL1TextBox.Text) * Convert.ToDouble(this.cosPhiL1TextBox.Text);
- return Math.Round(activePower, 2).ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement